示例#1
0
// LookupUserHashes makes query on the grid to get availabilty information for given UserHash values.
func (t *telegraph) lookupUserHashes(userHashes []string) (*LookupResponse, error) {
	defer t.waitGroup.Done()

	var p ProtocolMessage
	var l protocol.LookupUserHashesMsg
	var wg sync.WaitGroup
	var lr lookupResponse

	// Send LookupUserHashesMsg to all the connected Newton nodes and wait for response.
	hosts := t.pool.GetActiveConnections()
	if len(hosts) == 0 {
		return nil, ErrNoActiveConnection
	}
	for _, h := range hosts {
		mID, c, err := t.messageStore.NewMessage()
		if err != nil {
			return nil, err
		}
		l = protocol.LookupUserHashesMsg{
			Action:     protocol.LookupUserHashes,
			Version:    protocol.Version,
			UserHashes: userHashes,
			MessageID:  mID,
		}

		m, err := pool.MsgToByte(l)
		if err != nil {
			return nil, err
		}
		p = ProtocolMessage{
			Data:       m,
			Connection: h,
		}
		t.protocolOutput <- p
		// Listen for the response
		wg.Add(1)
		go lr.waitForResponse(&wg, h, c)
	}
	// Wait for all gorutines to complete. It will takes 1 second at max.
	wg.Wait()

	// Prepare a response message and return it
	r := &LookupResponse{
		UserClientItems: lr.UserClientItems,
		Pending:         lr.Pending,
		Timeouts:        lr.Timeouts,
	}
	return r, nil
}
示例#2
0
func (d *dispatcher) prepareMessage(userHash string, clientID uint32, messageType int, data []byte) ([]byte, error) {
	sm := protocol.SendMessageMsg{
		Action:      protocol.SendMessage,
		Version:     protocol.Version,
		UserHash:    userHash,
		MessageType: websocket.BinaryMessage,
		ClientID:    clientID,
		Body:        hex.EncodeToString(data),
	}
	m, err := pool.MsgToByte(sm)
	if err != nil {
		return nil, err
	}
	return m, nil
}