use reader for incoming Messages

This commit is contained in:
Marcelo Pires 2018-09-05 14:56:32 +02:00
parent 735a044485
commit 6ae3cf9df8

View File

@ -8,6 +8,7 @@ import (
"log" "log"
"strconv" "strconv"
"strings" "strings"
"sync"
"sync/atomic" "sync/atomic"
) )
@ -29,7 +30,9 @@ type Websocket struct {
conn *websocket.Conn conn *websocket.Conn
clientID string clientID string
msgID *uint64 msgID *uint64
subs map[string]chan struct{}
subsMu sync.Mutex //todo sync.Map
subs map[string]chan *message.Message
} }
var _ transport.Transport = (*Websocket)(nil) var _ transport.Transport = (*Websocket)(nil)
@ -41,16 +44,48 @@ func (w *Websocket) Init(options *transport.Options) error {
) )
w.TransportOpts = options w.TransportOpts = options
w.msgID = &msgID w.msgID = &msgID
w.subs = map[string]chan struct{}{} w.subs = map[string]chan *message.Message{}
w.conn, _, err = websocket.DefaultDialer.Dial(options.Url, nil) w.conn, _, err = websocket.DefaultDialer.Dial(options.Url, nil)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
func (w *Websocket) readWorker() error {
var payload []message.Message
for {
err := w.conn.ReadJSON(&payload)
if err != nil {
return err
}
//dispatch
msg := payload[0]
if strings.HasPrefix(msg.Channel, "/meta") {
continue //todo update introspect message and update state
}
w.subsMu.Lock()
subscription := w.subs[msg.Channel]
w.subsMu.Unlock()
subscription <- &msg
}
}
func (w *Websocket) Name() string { func (w *Websocket) Name() string {
return transportName return transportName
} }
func (w *Websocket) sendMessage(m *message.Message) error {
var payload []message.Message
payload = append(payload, *m)
if Debug {
log.Println("sending request", debugJson(payload))
}
return w.conn.WriteJSON(payload)
}
func (w *Websocket) nextMsgID() string { func (w *Websocket) nextMsgID() string {
return strconv.Itoa(int(atomic.AddUint64(w.msgID, 1))) return strconv.Itoa(int(atomic.AddUint64(w.msgID, 1)))
} }
@ -59,16 +94,14 @@ func (w *Websocket) Options() *transport.Options {
return w.TransportOpts return w.TransportOpts
} }
func (w *Websocket) Handshake() (err error) { func (w *Websocket) Handshake() (err error) {
var payload []message.Message
payload = append(payload, message.Message{ m := message.Message{
Channel: string(transport.Handshake), Channel: string(transport.Handshake),
Version: "1.0", //todo const Version: "1.0", //todo const
SupportedConnectionTypes: []string{transportName}, SupportedConnectionTypes: []string{transportName},
})
if Debug {
log.Println("handshake request", debugJson(payload))
} }
if err = w.conn.WriteJSON(payload); err != nil { err = w.sendMessage(&m)
if err != nil {
return err return err
} }
@ -84,23 +117,20 @@ func (w *Websocket) Handshake() (err error) {
if resp.GetError() != nil { if resp.GetError() != nil {
return err return err
} }
log.Println(debugJson(resp))
w.clientID = resp.ClientId w.clientID = resp.ClientId
return nil return nil
} }
func (w *Websocket) Connect() error { func (w *Websocket) Connect() error {
var payload []message.Message m := message.Message{
payload = append(payload, message.Message{
Channel: string(transport.Connect), Channel: string(transport.Connect),
ClientId: w.clientID, ClientId: w.clientID,
ConnectionType: transportName, ConnectionType: transportName,
Id: w.nextMsgID(), Id: w.nextMsgID(),
})
if Debug {
log.Println("connect request", debugJson(payload))
} }
//todo verify if extensions are applied on connect,verify if hs is complete //todo verify if extensions are applied on connect,verify if hs is complete
return w.conn.WriteJSON(payload) return w.sendMessage(&m)
} }
func (w *Websocket) Subscribe(subscription string, onMessage func(message *message.Message)) error { func (w *Websocket) Subscribe(subscription string, onMessage func(message *message.Message)) error {
@ -114,53 +144,19 @@ func (w *Websocket) Subscribe(subscription string, onMessage func(message *messa
w.TransportOpts.OutExt(m) w.TransportOpts.OutExt(m)
} }
var payload []message.Message if err := w.sendMessage(m); err != nil {
payload = append(payload, *m)
if Debug {
log.Println("subscribe request", debugJson(payload))
}
err := w.conn.WriteJSON(payload)
if err != nil {
return err return err
} }
var hsResps []message.Message //todo validate
if err = w.conn.ReadJSON(&hsResps); err != nil {
return err
}
if Debug {
log.Println("subscribe response", debugJson(hsResps))
}
subResp := hsResps[0] inMsgCh := make(chan *message.Message, 0)
if subResp.GetError() != nil {
return err
}
if !subResp.Successful {
//report err just for sanity
}
unsubsCh := make(chan struct{}, 0)
w.subs[subscription] = unsubsCh w.subs[subscription] = inMsgCh
for { var inMsg *message.Message
select { for inMsg = range inMsgCh {
case <-unsubsCh: onMessage(inMsg)
return nil
default:
}
//todo guard unsusribe
var payload []message.Message
err := w.conn.ReadJSON(&payload)
if err != nil {
return err
}
//hack
msg := payload[0]
if strings.HasPrefix(msg.Channel, "/meta") {
continue //todo update introspect message and update state
}
onMessage(&msg)
} }
return nil return nil
} }