Beispiel #1
0
// Reads a device ID change or message delivery request from the service. If
// an error occurs or the socket is closed, the connection will be removed
// from the router.
func readServiceRequest(c *serviceConn) (serviceState, error) {
	for ok := true; ok; {
		var request Request
		headerBytes, err := c.readWriter.Peek(2)
		if err != nil {
			return nil, err
		}
		switch common.Header(binary.BigEndian.Uint16(headerBytes)) {
		// Message delivery request from service.
		case HeaderServiceDeliver:
			request = <-c.recycleChan

		// Device ID change request from service.
		case HeaderServiceDeviceChange:
			request = new(DeviceChangeRequest)

		// Invalid request packet sent by service.
		default:
			ok = false
			continue
		}
		// Read and forward the request to the router.
		if _, err := request.ReadServiceRequest(c.readWriter); err != nil {
			return nil, err
		}
		c.Requests() <- request
	}
	return nil, nil
}
Beispiel #2
0
// Reads a message delivery or device change ACK from the service socket.
// This is a terminal state. If an error occurs or the socket is closed
// (unblocking `Read()` and `Peek()` with an `io.EOF`), the connection
// will be removed from the router.
func readServiceReply(conn *serviceConn) (next serviceState, err error) {
	defer conn.Router.RemoveServiceConn(conn, conn.service)
	for ok := true; ok; {
		var reply Reply
		headerBytes, err := conn.readWriter.Peek(2)
		if err != nil {
			return nil, err
		}

		switch common.Header(binary.BigEndian.Uint16(headerBytes)) {
		// Message delivery ACK from service.
		case HeaderServiceDeliver:
			reply = new(DeliverReply)

		// Device ID change ACK from service.
		case HeaderServiceDeviceChange:
			reply = new(DeviceChangeReply)

		// Invalid command sent by service.
		default:
			ok = false
			continue
		}

		// Read the reply and forward to the router. The router will
		// forward the ACK to the CN.
		if _, err = reply.ReadServiceReply(conn.readWriter); err != nil {
			return nil, err
		}
		conn.Replies() <- reply
	}
	return nil, nil
}
Beispiel #3
0
// Reads a device connection status change, device ID change ACK, or message
// delivery ACK from the CN. If an error occurs or the socket is closed, the
// connection will be removed from the router.
func readNodeUpdate(c *nodeConn) (nodeState, error) {
	defer c.Router.RemoveNodeConn(c)
	for ok := true; ok; {
		var reply Reply
		headerBytes, err := c.readWriter.Peek(2)
		if err != nil {
			return nil, err
		}
		switch common.Header(binary.BigEndian.Uint16(headerBytes)) {
		// Device connection status change from CN.
		case HeaderNodeConnChange:
			request := new(ConnChange)
			if _, err := request.ReadNodeRequest(c.readWriter); err != nil {
				return nil, err
			}
			c.Router.SetConnStatus(request)
			continue

		// Device ID change ACK from CN.
		case HeaderNodeDeviceChange:
			reply = new(DeviceChangeReply)

		// Message delivery ACK from CN.
		case HeaderNodeDeliver:
			reply = new(DeliverReply)

		// Invalid packet sent by CN.
		default:
			ok = false
			continue
		}
		// Read and forward the ACK to the router.
		if _, err := reply.ReadNodeReply(c.readWriter); err != nil {
			return nil, err
		}
		c.Replies() <- reply
	}
	return nil, nil
}
Beispiel #4
0
func readNodeRequest(conn *nodeConn) (next nodeState, err error) {
	defer conn.Router.RemoveNodeConn(conn)
	for ok := true; ok; {
		var request Request
		headerBytes, err := conn.readWriter.Peek(2)
		if err != nil {
			return nil, err
		}

		switch common.Header(binary.BigEndian.Uint16(headerBytes)) {
		// Message delivery request from CN.
		case HeaderNodeDeliver:
			request = <-conn.recycleChan

		// Device ID change request from CN.
		case HeaderNodeDeviceChange:
			request = &DeviceChangeRequest{
				ConnId: conn.Id(),
			}

		// Invalid packet sent by CN.
		default:
			ok = false
			continue
		}

		// Read the request and forward to the router. The router will forward
		// the message to the correct service.
		if _, err = request.ReadNodeRequest(conn.readWriter); err != nil {
			request.Recycle()
			return nil, err
		}
		conn.Requests() <- request
	}
	return nil, nil
}