finish enough barebones odClient to attempt connection (move to seperate pacakge so doesnt get gomobile.bind

This commit is contained in:
Dan Ballard 2017-09-07 08:11:23 -07:00
parent 5085a1d08c
commit 1fdbd0f7f7
3 changed files with 102 additions and 76 deletions

97
ODClient/odClient.go Normal file
View File

@ -0,0 +1,97 @@
package ODClient
import (
"github.com/s-rah/go-ricochet/connection"
"time"
"log"
"github.com/s-rah/go-ricochet/channels"
"github.com/s-rah/go-ricochet/utils"
"github.com/s-rah/go-ricochet"
)
type ODClient struct {
connection.AutoConnectionHandler
recvMessages chan string
sendMessages chan string
deviceName string
deviceLevel int
batteryLevel string
}
func (odClient *ODClient) Connect(privateKeyData string, serverAddr string) error {
log.Println("ODCLient.Connect()")
privateKey, err := utils.ParsePrivateKey([]byte(privateKeyData))
if err != nil {
log.Fatal("error parsing private key: %v", err)
}
odClient.Init(privateKey, serverAddr)
odClient.RegisterChannelHandler("im.ricochet.contact.request", func() channels.Handler {
contact := new(channels.ContactRequestChannel)
contact.Handler = odClient
return contact
})
odClient.RegisterChannelHandler("im.ricochet.chat", func() channels.Handler {
chat := new(channels.ChatChannel)
chat.Handler = odClient
return chat
})
odClient.recvMessages = make(chan string)
odClient.sendMessages = make(chan string)
log.Println("ODClient connecting...")
conn, err := goricochet.Open(serverAddr)
if err != nil {
log.Println("Error connecting %v", err)
return err
}
log.Println("ODCleint connected!")
_, err = connection.HandleOutboundConnection(conn).ProcessAuthAsClient(privateKey)
if err != nil {
log.Println("Error handling auth: %v", err)
return err
}
log.Println("ODClient: Authenticated!")
return nil
}
/************* Chat Channel Handler ********/
// ChatMessage passes the response to recvMessages.
func (odc *ODClient) ChatMessage(messageID uint32, when time.Time, message string) bool {
log.Printf("Received Message: %s", message)
odc.recvMessages <- message
return true
}
// ChatMessageAck does nothing.
func (odc *ODClient) ChatMessageAck(messageID uint32) {
}
/************* Contact Channel Handler ********/
// GetContactDetails is purposely empty
func (odc *ODClient) GetContactDetails() (string, string) {
return "", ""
}
// ContactRequest denies any contact request.
func (odc *ODClient) ContactRequest(name string, message string) string {
return "Rejected"
}
// ContactRequestRejected purposly does nothing.
func (odc *ODClient) ContactRequestRejected() {
}
// ContactRequestAccepted purposly does nothing.
func (odc *ODClient) ContactRequestAccepted() {
}
// ContactRequestError purposly does nothing.
func (odc *ODClient) ContactRequestError() {
}

View File

@ -8,7 +8,7 @@ import (
"time"
"github.com/yawning/bulb/utils/pkcs1"
"crypto/rsa"
"github.com/s-rah/go-ricochet/channels"
"github.com/dballard/goRicochetMobile/ODClient"
)
func GeneratePrivateKey() (string, error) {
@ -37,31 +37,12 @@ func TestNet() (ok bool, ex error) {
return true, nil
}
func ODClientConnect(privateKeyData string, serverAddr string) {
privateKey, err := utils.ParsePrivateKey([]byte(privateKeyData))
if err != nil {
log.Fatal("error parsing private key: %v", err)
}
odClient := new(ODClient)
odClient.Init(privateKey, serverAddr)
odClient.RegisterChannelHandler("im.ricochet.contact.request", func() channels.Handler {
contact := new(channels.ContactRequestChannel)
contact.Handler = odClient
return contact
})
odClient.RegisterChannelHandler("im.ricochet.chat", func() channels.Handler {
chat := new(channels.ChatChannel)
chat.Handler = odClient
return chat
})
func ODClientConnect(privateKey string, serverAddr string) error {
odClient := new(ODClient.ODClient)
err := odClient.Connect(privateKey, serverAddr)
return err
}
/******** Testing by standing up an echobot ******/
func EchoBot(privateKeyData string) {

View File

@ -1,52 +0,0 @@
package goRicochetMobile
import (
"github.com/s-rah/go-ricochet/connection"
"time"
"log"
)
type ODClient struct {
connection.AutoConnectionHandler
messages chan string
deviceName string
deviceLevel int
batteryLevel string
}
/************* Chat Channel Handler ********/
// ChatMessage passes the response to messages.
func (odc *ODClient) ChatMessage(messageID uint32, when time.Time, message string) bool {
log.Printf("Received Message: %s", message)
odc.messages <- message
return true
}
// ChatMessageAck does nothing.
func (odc *ODClient) ChatMessageAck(messageID uint32) {
}
/************* Contact Channel Handler ********/
// GetContactDetails is purposely empty
func (odc *ODClient) GetContactDetails() (string, string) {
return "", ""
}
// ContactRequest denies any contact request.
func (odc *ODClient) ContactRequest(name string, message string) string {
return "Rejected"
}
// ContactRequestRejected purposly does nothing.
func (odc *ODClient) ContactRequestRejected() {
}
// ContactRequestAccepted purposly does nothing.
func (odc *ODClient) ContactRequestAccepted() {
}
// ContactRequestError purposly does nothing.
func (odc *ODClient) ContactRequestError() {
}