From 1fdbd0f7f7e2ab4a737abf79368a5e065bd8a922 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Thu, 7 Sep 2017 08:11:23 -0700 Subject: [PATCH] finish enough barebones odClient to attempt connection (move to seperate pacakge so doesnt get gomobile.bind --- ODClient/odClient.go | 97 ++++++++++++++++++++++++++++++++++++++++++++ goRicochetMobile.go | 29 +++---------- odClient.go | 52 ------------------------ 3 files changed, 102 insertions(+), 76 deletions(-) create mode 100644 ODClient/odClient.go delete mode 100644 odClient.go diff --git a/ODClient/odClient.go b/ODClient/odClient.go new file mode 100644 index 0000000..8cd9f2c --- /dev/null +++ b/ODClient/odClient.go @@ -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() { +} \ No newline at end of file diff --git a/goRicochetMobile.go b/goRicochetMobile.go index c56078c..5a0c811 100644 --- a/goRicochetMobile.go +++ b/goRicochetMobile.go @@ -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) { diff --git a/odClient.go b/odClient.go deleted file mode 100644 index c9dd0ce..0000000 --- a/odClient.go +++ /dev/null @@ -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() { -} \ No newline at end of file