Ejemplo n.º 1
0
func (s Server) HandleMeta(msg protocol.Message, conn protocol.Connection) protocol.Message {
	meta_channel := msg.Channel().MetaType()

	if meta_channel == protocol.META_HANDSHAKE_CHANNEL {
		s.engine.Handshake(msg, conn)
	} else {
		client := s.GetClient(msg, conn)
		if client != nil {
			client.SetConnection(conn)

			switch meta_channel {
			case protocol.META_HANDSHAKE_CHANNEL:
				s.engine.Handshake(msg, conn)
			case protocol.META_CONNECT_CHANNEL:
				s.engine.Connect(msg, client, conn)

			case protocol.META_DISCONNECT_CHANNEL:
				s.engine.Disconnect(msg, client, conn)

			case protocol.META_SUBSCRIBE_CHANNEL:
				s.engine.SubscribeClient(msg, client)

			case protocol.META_UNKNOWN_CHANNEL:
				s.Logger().Panicf("Message with unknown meta channel received")

			}
		}
	}

	return nil
}
Ejemplo n.º 2
0
func (m Engine) responseFromRequest(request protocol.Message) protocol.Message {
	response := protocol.Message{}
	response["channel"] = request.Channel().Name()
	if reqId, ok := request["id"]; ok {
		response["id"] = reqId.(string)
	}

	return response
}
Ejemplo n.º 3
0
func (s Server) HandleMessage(msg protocol.Message, conn protocol.Connection) {
	channel := msg.Channel()
	if channel.IsMeta() {
		s.HandleMeta(msg, conn)
		// } else if channel.IsService() {
		// 	s.HandleService(msg)
	} else {
		s.HandlePublish(msg)
	}
}
Ejemplo n.º 4
0
func (s Server) GetClient(request protocol.Message, conn protocol.Connection) *protocol.Client {
	clientId := request.ClientId()
	client := s.engine.GetClient(clientId)
	if client == nil {
		s.Logger().Warnf("Message %v from unknown client %v", request.Channel(), clientId)
		response := request
		response["successful"] = false
		response["advice"] = map[string]interface{}{"reconnect": "handshake", "interval": "1000"}
		conn.Send([]protocol.Message{response})
		conn.Close()
	}
	return client
}
Ejemplo n.º 5
0
func (m Engine) Publish(request protocol.Message) {
	requestingClient := m.clients.GetClient(request.ClientId())

	if requestingClient == nil {
		m.logger.Warnf("PUBLISH from unknown client %s", request)
	} else {
		response := m.responseFromRequest(request)
		response["successful"] = true
		data := request["data"]
		channel := request.Channel()

		m.clients.GetClient(request.ClientId()).Queue(response)

		go func() {
			// Prepare msg to send to subscribers
			msg := protocol.Message{}
			msg["channel"] = channel.Name()
			msg["data"] = data
			// TODO: Missing ID

			msg.SetClientId(request.ClientId())

			// Get clients with subscriptions
			recipients := m.clients.GetClients(channel.Expand())
			m.logger.Debugf("PUBLISH from %s on %s to %d recipients", request.ClientId(), channel, len(recipients))
			// Queue messages
			for _, c := range recipients {
				m.clients.GetClient(c).Queue(msg)
			}
		}()

	}

}
Ejemplo n.º 6
0
func (m Engine) Disconnect(request protocol.Message, client *protocol.Client, conn protocol.Connection) {
	response := m.responseFromRequest(request)
	response["successful"] = true
	clientId := request.ClientId()
	m.logger.Debugf("Client %s disconnected", clientId)
}