Wednesday, January 19, 2011

End Course Project 5: Message Framework

It was clear from the beginning of this project, that one of our big challenges would be connecting all the units, using Bluetooth and handling all these connections in an easy way.

We decided that we needed to implement a BT MessageFramework “BTFW” that would handle this in a convenient way for the units that required sending and received messages to other units.

The constrains for the design of this BTFW is that:

·         It must provide a Java implementation for the server side
·         It must provide a Lejos implementation for the NXT brick side
·         It must support an ASCII level protocol, with a minimum of constrains to what type of messages can be sent. Giving freedom to the application designer to send the messages required for supporting the features in the game.
·         It must very simple to use. And embed all the complexities related to sending and recieving
·         It must be thread safe.

All these goals were achieved. Basically it was implemented using the NXTConnector and wrapping this to make it safe and easy to use. There are a few differences in the implementation of the Lejos and Java side. But it is all contained in the wrapping so they are identical to use on both sides.

In order to start the framework, just write:
MessageFramework m_mfNxt = new MessageFramework();
m_mfNxt.ConnectToNXT(new NXTInfo(NXTCommFactory.BLUETOOTH, "NAME", "ADDRESS"));

To send a message, just write:
LIMessage msg = new LIMessage(LIMessageType.Command, "Hello World");
m_mfNxt.SendMessage(msg);

To listen to a message the object that want to get notified, it must implement the interface MessageListenerInterface, forcing the programmer to implement a function like below.
@Override
public void recievedNewMessage(LIMessage msg) {
   System.out.println(new String(msg.getEncodedMsg())); 
}

When this is done, the object can register as a listener at the framework.
m_mfNxt.addMessageListener(this);

This approach required us to invest quit a lot of time in the framework, but it paid back a lot, when we started playing and debugging our game. Everybody was using this one component for communication and all expertise and bugs was contained in this one component and not spread out through our application, which made it fairly simple to adjust and perfect.

Improving the BTMF has currently only on improvement we would like to make. That is going from an only ASCII protocol to a full BYTE level protocol. Currently messages are only allowed to send chars of values 0-9 and a-Z, since we are using STX, ETX and DEL for packet framing purposes. In order to allow for a full byte range supporting framework, we would need to embed a framing layer supporting Byte Stuffing also know as COBS[1].



No comments:

Post a Comment