public void serviceThread() throws Exception
{
Runnable r = new Runnable() {
public void run() {
ClientMsg message;
try
{
do
{
message = recvMsg();
if (message.getType().getValue() == ClientMsgType.SESSION)
{
//FIXME: signed/unsigned problem for large session ids
sessionId = Long.toString(message.getSession().getSessionId().getValue());
fireSessionEvent(SessionEvent.AUTHENTICATE);
}
else if ( message.getType().getValue() != ClientMsgType.CLIENTCONFIRM)
{
throw new Exception("Got unknown message from server!");
} else if (message.getClientConfirm().getResult().getValue() != MsgResult.SUCCESS)
{
if (message.getClientConfirm().getCommand().getValue() != MsgClientCommand.MSGCLIENTCOMMAND_GETMOTECONTROL)
{
moteManager.revokeMoteControl(message.getClientConfirm().getMote_id().getValue());
}
else
{
System.out.println("Control of mote ID "+message.getClientConfirm().getMote_id().getValue()+" was not granted by control server.");
// FIXME: notify that mote control was not granted
}
}
else
{
switch (message.getClientConfirm().getCommand().getValue())
{
case MsgClientCommand.MSGCLIENTCOMMAND_GETMOTECONTROL:
moteManager.grantMoteControl(message.getClientConfirm().getMote_id().getValue());
break;
case MsgClientCommand.MSGCLIENTCOMMAND_DROPMOTECONTROL:
moteManager.revokeMoteControl(message.getClientConfirm().getMote_id().getValue());
break;
case MsgClientCommand.MSGCLIENTCOMMAND_MOTEMESSAGE:
moteManager.confirm(message.getClientConfirm().getMote_id().getValue(),
message.getClientConfirm().getMoteMsg());
break;
default:
throw new Exception("Got unknown message from server!");
}
}