Skip to content

Commit

Permalink
debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
ineiti committed Jan 13, 2017
1 parent ecd8d60 commit 5efe977
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
12 changes: 12 additions & 0 deletions local.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,20 @@ func NewTCPServer(port int) *Server {
h := NewServer(router, priv)
go h.Start()
for !h.Listening() {
log.Print("Waiting to listen on", port)
time.Sleep(10 * time.Millisecond)
}
for {
log.Print("Waiting for Websocket", h.ServerIdentity)
c, ce := connectToWebsocket(h.ServerIdentity, "ping", "ping")
if ce != nil {
log.Print("Waiting for Websocket", h.ServerIdentity)
time.Sleep(10 * time.Millisecond)
} else {
c.Close()
break
}
}
return h
}

Expand Down
46 changes: 29 additions & 17 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func NewWebSocket(si *network.ServerIdentity) *WebSocket {
webHost, err := getWebAddress(si, true)
log.ErrFatal(err)
w.mux = http.NewServeMux()
w.mux.Handle("/ping/", &wsHandler{"ping", nil})
w.server = &graceful.Server{
Timeout: 100 * time.Millisecond,
Server: &http.Server{
Expand Down Expand Up @@ -137,9 +138,10 @@ func (t wsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
s := t.service
log.Print(t.serviceName, r.URL.Path)
var reply []byte
path := strings.TrimPrefix(r.URL.Path, "/"+t.serviceName+"/")
log.Lvl3("Got request for", t.serviceName, path)
log.LLvl3("Got request for", t.serviceName, path)
reply, ce = s.ProcessClientRequest(path, buf)
if ce == nil {
err := ws.WriteMessage(mt, reply)
Expand Down Expand Up @@ -185,6 +187,30 @@ func NewClientKeep(s string) *Client {
}
}

func connectToWebsocket(dst *network.ServerIdentity, service, path string) (*websocket.Conn, ClientError) {
// Open connection to service.
url, err := getWebAddress(dst, false)
if err != nil {
return nil, NewClientError(err)
}
d := &websocket.Dialer{}
var conn *websocket.Conn
// Re-try to connect in case the websocket is just about to start
for a := 0; a < network.MaxRetryConnect; a++ {
log.LLvlf4("Sending to %s/%s/%s", url, service, path)
conn, _, err = d.Dial(fmt.Sprintf("ws://%s/%s/%s", url, service, path),
http.Header{"Origin": []string{"http://" + url}})
if err == nil {
break
}
time.Sleep(network.WaitRetry)
}
if err != nil {
return nil, NewClientError(err)
}
return conn, nil
}

// Send will marshal the message into a ClientRequest message and send it.
func (c *Client) Send(dst *network.ServerIdentity, path string, buf []byte) ([]byte, ClientError) {
c.Lock()
Expand All @@ -195,22 +221,8 @@ func (c *Client) Send(dst *network.ServerIdentity, path string, buf []byte) ([]b
c.Close()
}
if c.si == nil {
// Open connection to service.
url, err := getWebAddress(dst, false)
if err != nil {
return nil, NewClientError(err)
}
log.Lvlf4("Sending %x to %s/%s/%s", buf, url, c.service, path)
d := &websocket.Dialer{}
// Re-try to connect in case the websocket is just about to start
for a := 0; a < network.MaxRetryConnect; a++ {
c.conn, _, err = d.Dial(fmt.Sprintf("ws://%s/%s/%s", url, c.service, path),
http.Header{"Origin": []string{"http://" + url}})
if err == nil {
break
}
time.Sleep(network.WaitRetry)
}
var err error
c.conn, err = connectToWebsocket(dst, c.service, path)
if err != nil {
return nil, NewClientError(err)
}
Expand Down

0 comments on commit 5efe977

Please sign in to comment.