diff options
Diffstat (limited to 'connector/external')
-rw-r--r-- | connector/external/external.go | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/connector/external/external.go b/connector/external/external.go index e64ff78..26f3c40 100644 --- a/connector/external/external.go +++ b/connector/external/external.go @@ -1,6 +1,7 @@ package external import ( + "bufio" "encoding/json" "fmt" "io" @@ -179,7 +180,7 @@ func (ext *External) restartLoop(generation int) { } } -func (m *extMessageWithData) DecodeJSON(jj []byte) error { +func (m *extMessageWithData) UnmarshalJSON(jj []byte) error { var c extMessage err := json.Unmarshal(jj, &c) @@ -189,40 +190,59 @@ func (m *extMessageWithData) DecodeJSON(jj []byte) error { *m = extMessageWithData{extMessage: c} switch c.MsgType { case USER_INFO_UPDATED: - var ui UserInfo + var ui struct { + Data UserInfo `json:"data"` + } err := json.Unmarshal(jj, &ui) if err != nil { return err } - m.Data = &ui + m.Data = &ui.Data + return nil case ROOM_INFO_UPDATED: - var ri RoomInfo + var ri struct { + Data RoomInfo `json:"data"` + } err := json.Unmarshal(jj, &ri) if err != nil { return err } - m.Data = &ri + m.Data = &ri.Data + return nil case EVENT: - var ev Event + var ev struct { + Data Event `json:"data"` + } err := json.Unmarshal(jj, &ev) if err != nil { return err } - m.Data = &ev + m.Data = &ev.Data + return nil + case JOINED, LEFT, CACHE_PUT, CACHE_GET, REP_OK, REP_ERROR: + return nil + default: + return fmt.Errorf("Invalid message type for message from external program: '%s'", c.MsgType) } - return nil + } func (ext *External) recvLoop() { - reader := json.NewDecoder(ext.recv) - for { + scanner := bufio.NewScanner(ext.recv) + for scanner.Scan() { var msg extMessageWithData - err := reader.Decode(&msg) + err := json.Unmarshal(scanner.Bytes(), &msg) if err != nil { - log.Warnf("Failed to decode from %s: %s. Stopping reading.", ext.command, err) + log.Warnf("Failed to decode from %s: %s. Skipping line.", ext.command, err.Error()) + continue + } + + if scanner.Err() != nil { + log.Warnf("Failed to read from %s: %s. Stopping here.", ext.command, scanner.Err().Error()) break } + log.Debugf("GOT MESSAGE: %#v %#v", msg, msg.Data) if strings.HasPrefix(msg.MsgType, "rep_") { func() { ext.lock.Lock() @@ -233,7 +253,7 @@ func (ext *External) recvLoop() { } }() } else { - ext.handleCmd(&msg) + go ext.handleCmd(&msg) } } } @@ -328,7 +348,7 @@ func (ext *External) User() UserID { MsgType: GET_USER, }, nil) if err != nil { - log.Warnf("Unable to get user!") + log.Warnf("Unable to get user! %s", err.Error()) return "" } return rep.User |