diff --git a/cmd/xmpp_component/xmpp_component.go b/cmd/xmpp_component/xmpp_component.go
index ea97c1f..f08dbab 100644
--- a/cmd/xmpp_component/xmpp_component.go
+++ b/cmd/xmpp_component/xmpp_component.go
@@ -3,5 +3,6 @@ package main
import "fluux.io/xmpp"
func main() {
- xmpp.Open("mqtt.localhost")
+ component := xmpp.Component{Host: "mqtt.localhost", Secret: "mypass"}
+ component.Connect("localhost:8888")
}
diff --git a/component.go b/component.go
index 64a3e57..55b4c48 100644
--- a/component.go
+++ b/component.go
@@ -28,12 +28,8 @@ type Component struct {
decoder *xml.Decoder
}
-type Handshake struct {
- XMLName xml.Name `xml:"jabber:component:accept handshake"`
-}
-
-// Handshake generates an authentication token based on StreamID and shared secret.
-func (c *Component) Handshake(streamId string) string {
+// handshake generates an authentication token based on StreamID and shared secret.
+func (c *Component) handshake(streamId string) string {
// 1. Concatenate the Stream ID received from the server with the shared secret.
concatStr := streamId + c.Secret
@@ -50,55 +46,51 @@ func (c *Component) Handshake(streamId string) string {
}
// TODO Helper to prepare connection string
-func Open(connStr string) error {
- c := Component{Host: connStr, Secret: "mypass"}
-
+func (c *Component) Connect(connStr string) error {
var conn net.Conn
var err error
- if conn, err = net.DialTimeout("tcp", "localhost:8888", time.Duration(5)*time.Second); err != nil {
+ if conn, err = net.DialTimeout("tcp", connStr, time.Duration(5)*time.Second); err != nil {
return err
}
c.conn = conn
- // TODO send stream open and check for reply
- // Send stream open tag
- componentHost := connStr // TODO Fix me: Extract componentID + secret
- if _, err := fmt.Fprintf(conn, componentStreamOpen, componentHost, NSComponent, NSStream); err != nil {
- fmt.Println("cannot send stream open.")
- return err
+ // 1. Send stream open tag
+ if _, err := fmt.Fprintf(conn, componentStreamOpen, c.Host, NSComponent, NSStream); err != nil {
+ return errors.New("cannot send stream open " + err.Error())
}
c.decoder = xml.NewDecoder(conn)
- // Initialize xml decoder and extract streamID from reply
+ // 2. Initialize xml decoder and extract streamID from reply
streamId, err := initDecoder(c.decoder)
if err != nil {
- fmt.Println("cannot init decoder")
- return err
+ return errors.New("cannot init decoder " + err.Error())
}
- fmt.Println("StreamID = ", streamId)
-
- // Authentication
- if _, err := fmt.Fprintf(conn, "%s", c.Handshake(streamId)); err != nil {
- fmt.Println("cannot send stream open.")
- return err
+ // 3. Authentication
+ if _, err := fmt.Fprintf(conn, "%s", c.handshake(streamId)); err != nil {
+ return errors.New("cannot send handshake " + err.Error())
}
- // Next message should be either success or failure.
+ // 4. Check server response for authentication
name, val, err := next(c.decoder)
if err != nil {
- fmt.Println(err)
return err
}
switch v := val.(type) {
case *StreamError:
- fmt.Printf("error: %s", v.Error.Local)
+ return errors.New("handshake failed " + v.Error.Local)
case *Handshake:
- fmt.Println("Component connected")
+ return nil
default:
return errors.New("unexpected packet, got " + name.Local + " in " + name.Space)
}
-
- return nil
+ panic("unreachable")
+}
+
+// ============================================================================
+// XMPP packets struct
+
+type Handshake struct {
+ XMLName xml.Name `xml:"jabber:component:accept handshake"`
}
diff --git a/component_test.go b/component_test.go
index 925222c..76e3849 100644
--- a/component_test.go
+++ b/component_test.go
@@ -11,7 +11,7 @@ func TestHandshake(t *testing.T) {
streamID := "1263952298440005243"
expected := "c77e2ef0109fbbc5161e83b51629cd1353495332"
- result := c.Handshake(streamID)
+ result := c.handshake(streamID)
if result != expected {
t.Errorf("incorrect handshake calculation '%s' != '%s'", result, expected)
}