Пример #1
0
// SendProtocolMessage sends plain protocol messages to the connected Newton nodes.
func (d *dispatcher) SendProtocolMessage(data []byte, cn []byte) error {
	host, _, err := pool.ParseConnection(cn)
	if err != nil {
		return err
	}
	conn := d.Pool.Get(host)
	if conn != nil {
		conn.Output <- data
		return nil
	}
	return ErrHostNotFound
}
Пример #2
0
// waitForResponse is a function that waits for the response of lookup request. It raises timeout after some time.
func (l *lookupResponse) waitForResponse(wg *sync.WaitGroup, conn []byte, input chan store.ProtocolInput) {
	ticker := time.NewTicker(1000 * time.Millisecond)
	defer func() {
		ticker.Stop()
		wg.Done()
	}()

	select {
	case msg := <-input:
		l.Lock()
		if msg["Items"] != nil {
			// JSON to interface. It's too bad, I know.
			items := msg["Items"].(interface{}).([]interface{})
			for _, item := range items {
				t := item.(map[string]interface{})
				i := []protocol.UserClientItem{}
				ui := protocol.UserClientItems{
					UserHash: t["UserHash"].(string),
					Clients:  i,
				}
				for _, c := range t["Clients"].([]interface{}) {
					cl := c.(map[string]interface{})
					cID := cl["ClientID"].(float64)
					client := protocol.UserClientItem{
						ClientID: uint32(cID),
						Addr:     cl["Addr"].(string),
					}
					ui.Clients = append(ui.Clients, client)
				}
				l.UserClientItems = append(l.UserClientItems, ui)
			}
		}
		if msg["Pending"] != nil {
			pending := msg["Pending"].([]interface{})
			for _, h := range pending {
				l.Pending = append(l.Pending, h.(string))
			}
		}
		l.Unlock()
	case <-ticker.C:
		l.Lock()
		host, _, _ := pool.ParseConnection(conn)
		l.Timeouts = append(l.Timeouts, host)
		l.Unlock()
		return
	}

}
Пример #3
0
// SendCryptoMessage sends the given data to the host through WebSocket connection if it's possible. Otherwise it makes a HTTP POST to send data.
func (d *dispatcher) SendCryptoMessage(data []byte, userHash string, cn []byte) {
	host, clientID, err := pool.ParseConnection(cn)
	if err != nil {
		d.log.Error("Error while parsing connection")
		return
	}
	conn := d.Pool.Get(host)
	if conn != nil {
		d.log.Info("Send message through WebSocket")
		msg, err := d.prepareMessage(userHash, clientID, websocket.BinaryMessage, data)
		if err != nil {
			d.log.Error("Error while preparing binary message packet")
			return
		}
		conn.Output <- msg
	} else {
		d.log.Info("Send message via HTTP POST request.")
		d.sendWithHTTP(data, userHash, host, clientID)
	}
}