summaryrefslogtreecommitdiff
path: root/src/globster/hub/hub.go
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2012-04-09 10:38:10 +0200
committerYorhel <git@yorhel.nl>2012-04-09 10:38:10 +0200
commit8289946e8a349b09fa56ef21252ba5f8edc03b47 (patch)
tree0e15520237700446c7e57251cc2b7a3fa19e54bd /src/globster/hub/hub.go
Initial commit of a WIP DC client based on Tanja
Diffstat (limited to 'src/globster/hub/hub.go')
-rw-r--r--src/globster/hub/hub.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/globster/hub/hub.go b/src/globster/hub/hub.go
new file mode 100644
index 0000000..fb7b377
--- /dev/null
+++ b/src/globster/hub/hub.go
@@ -0,0 +1,70 @@
+package hub
+
+import (
+ "globster/adc"
+ "go.blicky.net/tanja"
+ "time"
+ "net"
+)
+
+// TODO: Do the connect() and write()s in a separate goroutine.
+
+type hub struct {
+ name string
+ ses *tanja.Session
+ conn net.Conn
+}
+
+func newHub(node *tanja.Node, name string) *hub {
+ s := &hub{name:name, ses:node.Session()}
+
+ s.ses.RegRPC(s, func(s string) tanja.Tuple {
+ if s[0] != 'T' {
+ return nil
+ }
+ return tanja.Tup("hub", name, s[1:])
+ })
+
+ s.ses.Send(false, "hub", name, "Created")
+ go s.ses.Run()
+ return s
+}
+
+func (s *hub) Texists(m *tanja.Message) {
+ m.Reply(s.name)
+ m.Close()
+}
+
+func (s *hub) Tconnect(addr string) {
+ if s.conn != nil {
+ return
+ }
+ var e error
+ s.conn, e = net.DialTimeout("tcp", addr, 30*time.Second)
+ if e != nil {
+ s.ses.Send(false, "hub", s.name, "Disconnected", e.Error())
+ } else {
+ s.ses.Send(false, "hub", s.name, "Connected", s.conn.RemoteAddr().String())
+ adc.NewWriter(s.conn).Write(&adc.Message{Header:adc.SUP.SetType('I')})
+ }
+}
+
+func (s *hub) Tdisconnect(m *tanja.Message, r ...tanja.Element) {
+ if s.conn != nil {
+ s.conn.Close()
+ s.conn = nil
+ s.ses.Sendt(false, append(tanja.Tup("hub", s.name, "Disconnected"), r...))
+ }
+ // Only close the return-path after disconnect, to allow users to wait for
+ // successful disconnect.
+ m.Close()
+}
+
+func (s *hub) close() {
+ r := s.ses.Send(true, "hub", s.name, "disconnect")
+ <-r.Chan()
+ r.Close()
+
+ s.ses.Close()
+ s.ses.Send(false, "hub", s.name, "Closed")
+}