Esempio n. 1
0
func (handler *_TunnelClient) MessageReceived(context gorpc.Context, message *gorpc.Message) (*gorpc.Message, error) {

	if message.Code != gorpc.CodeTunnel {

		return message, nil
	}

	tunnel, err := gorpc.ReadTunnel(bytes.NewBuffer(message.Content))

	if err != nil {
		handler.E("backward tunnel(%s) message -- failed\n%s", tunnel.ID, err)
		return nil, err
	}

	handler.D("dispatch tunnel message to %s", tunnel.ID)

	agent, err := handler.agent(context, tunnel.ID)

	if err != nil {
		handler.E("dispatch tunnel(%s) message -- failed\n%s", tunnel.ID, err)
		return nil, nil
	}

	go agent.MessageReceived(tunnel.Message)

	return nil, nil
}
Esempio n. 2
0
func (handler *_TunnelServerHandler) MessageReceived(context gorpc.Context, message *gorpc.Message) (*gorpc.Message, error) {

	if message.Code == gorpc.CodeTunnelWhoAmI {

		handler.I("tunnel handshake ......")

		whoAmI, err := gorpc.ReadTunnelWhoAmI(bytes.NewBuffer(message.Content))

		if err != nil {
			return nil, err
		}

		handler.proxy.proxy.BindServices(handler.proxy, context.Pipeline(), whoAmI.Services)

		context.FireActive()

		return nil, nil
	}

	if message.Code != gorpc.CodeTunnel {
		return message, nil
	}

	handler.V("backward tunnel message")

	tunnel, err := gorpc.ReadTunnel(bytes.NewBuffer(message.Content))

	if err != nil {
		handler.E("backward tunnel(%s) message -- failed\n%s", tunnel.ID, err)
		return nil, err
	}

	if device, ok := handler.proxy.client(tunnel.ID); ok {

		tunnel.Message.Agent = handler.id

		err := device.SendMessage(tunnel.Message)

		if err == nil {
			handler.V("backward tunnel message -- success")
			return nil, nil
		}

		return nil, err
	}

	handler.E("backward tunnel(%s) message -- failed,device not found", tunnel.ID)

	return nil, nil
}