| AVR Z-LINKŪ |

# Events

## Detailed Description

This module contains all the different event types, their interfaces and the event handler class. Five different event types are defined:

- NewAssociationIndication: Fired by the radio interface when the radio platform indicates that a peer-to-peer link has been established.
- NewDisassociationIndication: Event fired by the radio interface when the other peer in the network decided remove it self.
- NewDataIndication: Event fired whenever new data is available at the radio platform (Message received from the other peer).
- ChatWindowEvent: This event is fired if one of the buttons are pressed in the ChatWindow.
- MainFrameEvent: This event is fired if one of the buttons are pressed in the MainFrame.

The illustration below indicates how the different events are catched by the EventHandler instance. Most of the control/functional code can be found inside the event handler. As discussed for the GUI module the idea was to keep as little control code inside the GUI classes as possible.

Different events being dispatched to the handler.

## Functions
void | main::EventHandler.ChatWindowEvent (ChatWindowEvent event)
| Function that is called whenever a new ChatWindowEvent is generated by the GUI.
void | gui::ChatWindowEventListener.ChatWindowEvent (ChatWindowEvent event)
| gui::ChatWindowEvent.ChatWindowEvent (int Event, String data)
| Class constructor.
| main::EventHandler.EventHandler (MainFrame MainTab, ChatWindow ChatTab)
| Class constructor. Generates a new EventHandler.
String | gui::ChatWindowEvent.getData ()
| This function is used to extract the Data attribute of the ChatWindowEvent object.
int | radioInterface::NewDataIndication.getDataLength ()
| Method used to extract the dataLength attribute of the NewDataIndication object.
int | gui::ChatWindowEvent.getEventType ()
| This function is used to extract the EventType attribute of the ChatWindowEvent object.
String | radioInterface::NewDataIndication.getNewData ()
| Method used to extract the data attribute of the NewDataIndication object.
String | serialInterface::SerialStreamEvent.getResponse ()
| Function that returns the response associated with the SerialStreamEvent.
void | main::EventHandler.MainFrameEvent (MainFrameEvent event)
| Function that is called whenever a new MainFrameEvent is generated by the GUI.
void | gui::MainFrameEventListener.MainFrameEvent (MainFrameEvent event)
| Interface method that has to be implemented by any MainFrameEventListener class.
| radioInterface::NewAssociationIndication.NewAssociationIndication ()
| Empty class constructor. Object has no attributes.
void | main::EventHandler.NewAssociationIndicationEvent (NewAssociationIndication newAssociation)
| Function that is called whenever a new association is indicated by the radio platform.
| radioInterface::NewDataIndication.NewDataIndication (String data, int length)
| Class constructor. Generates a NewDataIndication.
void | main::EventHandler.NewDataIndicationEvent (NewDataIndication newData)
| Function that is called whenever a new data is received by the radio platform.
| radioInterface::NewDisassociationIndication.NewDisassociationIndication ()
| Empty constructor. Object has no attributes.
void | main::EventHandler.NewDisassociationIndicationEvent (NewDisassociationIndication newDisAssociation)
| Function that is called whenever a new disassociation is indicated by the radio platform.
void | main::EventHandler.sendMessage (String Message)
| Send the send command (AT+T) to the radio platform.

## Function Documentation

void main.EventHandler.ChatWindowEvent | ( | ChatWindowEvent | event | ) | `[inherited]`

Function that is called whenever a new ChatWindowEvent is generated by the GUI.

Parameters:

  | event | Object holding information about the new ChatWindowEvent.

Implements gui.ChatWindowEventListener.

Definition at line 279 of file EventHandler.java.

References gui.ChatWindow.clearMessage(), gui.ChatWindowEvent.getData(), gui.ChatWindowEvent.getEventType(), and main.EventHandler.sendMessage().

    00279                                                              {
    00280
    00281                 int eventType = event.getEventType( );
    00282                 String Message = event.getData( );
    00283
    00284                 //Send message
    00285                 if( ( eventType == ChatWindowEvent.ENTER ) || ( eventType == ChatWindowEvent.SEND ) ){
    00286
    00287                         //OK to send message if peer-to-peer connection is established.
    00288                         if( networkStarted ){
    00289
    00290                                 sendMessage( Message );
    00291                         }
    00292
    00293                         else{
    00294
    00295                                 Dialogs.showPlainMessage( Main, "Not possible to send data until connection is established.");
    00296                         }
    00297                 }
    00298
    00299                 //Clear message window.
    00300                 else{
    00301
    00302                         chat.clearMessage( );
    00303                 }
    00304         }

Here is the call graph for this function:

void gui.ChatWindowEventListener.ChatWindowEvent | ( | ChatWindowEvent | event | ) | `[inherited]`

Interface method that has to be implemented by any ChatWindowEventListener class.

Parameters:

  | event | ChatWindowButtonEvent to be handled by the listener class.

Implemented in main.EventHandler.

gui.ChatWindowEvent.ChatWindowEvent | ( | int | Event,
|  | String | data |
| ) |  |  | `[inherited]`

Class constructor.

Parameters:

  | Event | Attribute holding the event type. Either CLEAR, ENTER or SEND.
  | data | Only valid for SEND event. Holds the message that the user wishes to send.

Definition at line 41 of file ChatWindowEvent.java.

    00041                                                         {
    00042
    00043                 this.EventType = Event;
    00044                 this.Data          = data;
    00045         }

main.EventHandler.EventHandler | ( | MainFrame | MainTab,
|  | ChatWindow | ChatTab |
| ) |  |  | `[inherited]`

Class constructor. Generates a new EventHandler.

The EventHandler is responsible for handling all the different events generated by the user through the GUI and those received from the radio platfrom.

Parameters:

  | MainTab | The MainFrame part of the GUI.
  | ChatTab | The ChatWindow part of the GUI.

Definition at line 67 of file EventHandler.java.

    00067                                                                     {
    00068
    00069                 this.Main = MainTab;
    00070                 this.chat = ChatTab;
    00071                 this.radio = null;
    00072
    00073                 isConnected = false;
    00074                 isCoordinator = false;
    00075                 networkStarted = false;
    00076
    00077                 //Setup eventsystem from MainFrame, ChatWindow and RadioInterface.
    00078                 this.Main.addMainFrameEventListener( this );
    00079                 this.chat.addChatWindowEventListener( this );
    00080         }

String gui.ChatWindowEvent.getData | ( |  | ) | `[inherited]`

This function is used to extract the Data attribute of the ChatWindowEvent object.

Returns:
  Null if this is a CLEAR event, and the message to send if this is a SEND event.

Definition at line 66 of file ChatWindowEvent.java.

Referenced by main.EventHandler.ChatWindowEvent().

    00066                                 {
    00067                 return this.Data;
    00068         }

int radioInterface.NewDataIndication.getDataLength | ( |  | ) | `[inherited]`

Method used to extract the dataLength attribute of the NewDataIndication object.

Returns:
  Length of the data field.

Definition at line 60 of file NewDataIndication.java.

    00060                                    {
    00061                 return this.dataLength;
    00062         }

int gui.ChatWindowEvent.getEventType | ( |  | ) | `[inherited]`

This function is used to extract the EventType attribute of the ChatWindowEvent object.

Returns:
  Type of ChatWindowEvent that is associated with the current instance.

Definition at line 54 of file ChatWindowEvent.java.

Referenced by main.EventHandler.ChatWindowEvent().

    00054                                   {
    00055                 return this.EventType;
    00056         }

String radioInterface.NewDataIndication.getNewData | ( |  | ) | `[inherited]`

Method used to extract the data attribute of the NewDataIndication object.

Returns:
  String containing the received data.

Definition at line 48 of file NewDataIndication.java.

Referenced by main.EventHandler.NewDataIndicationEvent().

    00048                                    {
    00049                 return this.data;
    00050         }

String serialInterface.SerialStreamEvent.getResponse | ( |  | ) | `[inherited]`

Function that returns the response associated with the SerialStreamEvent.

Returns:
  Response received on the input stream.

Definition at line 48 of file SerialStreamEvent.java.

Referenced by radioInterface.RadioInterface.SerialStreamEvent().

    00048                                     {
    00049                 return this.receivedOnInpuStream;
    00050         }

void main.EventHandler.MainFrameEvent | ( | MainFrameEvent | event | ) | `[inherited]`

Function that is called whenever a new MainFrameEvent is generated by the GUI.

Parameters:

  | event | Object holding information about the event type.

TODO Add disassociation command.

Implements gui.MainFrameEventListener.

Definition at line 141 of file EventHandler.java.

References radioInterface.RadioInterface.addNewAssociationIndicationListeners(), radioInterface.RadioInterface.addNewDataIndicationListeners(), radioInterface.RadioInterface.addNewDisassociationIndicationListeners(), gui.ChatWindow.appendTextToConversation(), gui.ChatWindow.clearConversation(), radioInterface.RadioInterface.disableIndications(), gui.ChatWindow.disableMessageWindow(), radioInterface.RadioInterface.enableIndications(), gui.ChatWindow.enableMessageWindow(), gui.MainFrameEvent.getEventType(), radioInterface.RadioInterface.removeNewAssociationIndicationListeners(), radioInterface.RadioInterface.removeNewDataIndicationListeners(), radioInterface.RadioInterface.removeNewDisassociationIndicationListeners(), and radioInterface.RadioInterface.terminateCommunication().

    00141                                                            {
    00142
    00143                 //Extract event type
    00144                 int eventType = event.getEventType( );
    00145
    00146                 //Connect to radio plaform.
    00147                 if( eventType == MainFrameEvent.CONNECT ){
    00148
    00149                         Main.clearDebugMesasges( );
    00150                         Main.appendDebugMessage( "EVENT: Connect" );
    00151
    00152                         //Not connected.
    00153                         if( !isConnected ){
    00154
    00155                                 Main.appendDebugMessage( "Not connected...initialize radio interface." );
    00156
    00157                                 //Try to open a new radio interface.
    00158                                 if( initializeRadio( ) ){
    00159
    00160                                         Main.appendDebugMessage( "Radio interface initialized." );
    00161
    00162                                         //See if the radio platform responds.
    00163                                         if( sendResetWithRetry( ) ){
    00164
    00165                                                 isConnected = true;
    00166                                                 networkStarted = false;
    00167
    00168                                                 //Configure radio platform with user options
    00169                                                 if( configureRadioPlatform( ) ){
    00170
    00171                                                         //Coordinator.
    00172                                                         if( isCoordinator ){
    00173
    00174                                                                 chat.clearConversation( );
    00175
    00176                                                                 Main.appendDebugMessage( "Coordinator Started." );
    00177                                                                 chat.appendTextToConversation( "Network not started yet." );
    00178                                                         }
    00179
    00180                                                         else{
    00181
    00182                                                                 Main.appendDebugMessage( "End-device Started." );
    00183                                                                 networkStarted = true;
    00184                                                                 chat.clearConversation( );
    00185                                                                 chat.enableMessageWindow( );
    00186                                                         }
    00187
    00188                                                         Main.addChatTab( chat );
    00189                                                         radio.addNewAssociationIndicationListeners( this );
    00190                                                         radio.addNewDataIndicationListeners( this );
    00191                                                         radio.addNewDisassociationIndicationListeners( this );
    00192                                                         radio.enableIndications( );
    00193                                                 }
    00194
    00195                                                 else{
    00196
    00197                                                         isConnected = false;
    00198                                                         radio.disableIndications( );
    00199                                                         radio.removeNewAssociationIndicationListeners( this );
    00200                                                         radio.removeNewDataIndicationListeners( this );
    00201                                                         radio.removeNewDisassociationIndicationListeners( this );
    00202                                                         radio.terminateCommunication( );
    00203
    00204                                                         Main.appendDebugMessage( "Network configuration and start-up failed." );
    00205                                                         Dialogs.showPlainMessage( Main, "Unable to start network." );
    00206                                                 }
    00207                                         }
    00208
    00209                                         else{
    00210
    00211                                                 radio.terminateCommunication( );
    00212
    00213                                                 Main.appendDebugMessage( "AT-Command AT+R failed. Check serial connection." );
    00214                                                 Dialogs.showPlainMessage( Main, "Please connect radio platform first." );
    00215                                         }
    00216                                 }
    00217
    00218                                 //Not possible to open interface.
    00219                                 else{
    00220
    00221                                         Main.appendDebugMessage( "Something went wrong during initialization." );
    00222                                         Dialogs.showPlainMessage( Main, "Radio Interface could not be started." +
    00223                                         "\r\nPlease try to reconnect!" );
    00224                                 }
    00225                         }
    00226
    00227                         //Already connected.
    00228                         else{
    00229
    00230                                 Main.appendDebugMessage( "Already connected." );
    00231                                 Dialogs.showPlainMessage( Main, "You are already connected." );
    00232                         }
    00233                 }
    00234
    00235                 //Disconnect from radio platform.
    00236                 else if( eventType == MainFrameEvent.DISCONNECT ){
    00237
    00238                         Main.appendDebugMessage( "EVENT: Disconnect" );
    00239
    00240                         //Not connected.
    00241                         if( isConnected == false ){
    00242
    00243                                 Main.appendDebugMessage( "You are not connected." );
    00244                                 Dialogs.showPlainMessage( Main, "You are not connected." );
    00245                         }
    00246
    00247                         //Disconnect
    00248                         else{
    00249
    00250                                 Main.appendDebugMessage( "You are connected...terminating RF link." );
    00251                                 Main.clearDebugMesasges( );
    00252
    00253                                 isConnected = false;
    00254                                 isCoordinator = false;
    00255                                 networkStarted = false;
    00256                                 radio.terminateCommunication( );
    00257
    00259                                 chat.disableMessageWindow( );
    00260                                 Main.closeChatTab( chat );
    00261                         }
    00262                 }
    00263
    00264                 //Clear debug message window.
    00265                 else{
    00266
    00267                         Main.clearDebugMesasges( );
    00268                 }
    00269         }

Here is the call graph for this function:

void gui.MainFrameEventListener.MainFrameEvent | ( | MainFrameEvent | event | ) | `[inherited]`

Interface method that has to be implemented by any MainFrameEventListener class.

Parameters:

  | event | MainFrameEvent to be handled by the listener class.

Implemented in main.EventHandler.

radioInterface.NewAssociationIndication.NewAssociationIndication | ( |  | ) | `[inherited]`

Empty class constructor. Object has no attributes.

Definition at line 28 of file NewAssociationIndication.java.

    00028                                           {
    00029         }

void main.EventHandler.NewAssociationIndicationEvent | ( | NewAssociationIndication | newAssociation | ) | `[inherited]`

Function that is called whenever a new association is indicated by the radio platform.

Parameters:

  | newAssociation | Object holding information about the new association.

Implements radioInterface.NewAssociationIndicationListener.

Definition at line 93 of file EventHandler.java.

References gui.ChatWindow.clearConversation(), and gui.ChatWindow.enableMessageWindow().

    00093                                                                                             {
    00094
    00095                 if( isCoordinator ){
    00096
    00097                         networkStarted = true;
    00098                         chat.clearConversation( );
    00099                         chat.enableMessageWindow( );
    00100                 }
    00101
    00102                 else{
    00103                         Dialogs.showPlainMessage( Main, "End-devices are not supposed to be open for association!" );
    00104                 }
    00105         }

Here is the call graph for this function:

radioInterface.NewDataIndication.NewDataIndication | ( | String | data,
|  | int | length |
| ) |  |  | `[inherited]`

Class constructor. Generates a NewDataIndication.

Parameters:

  | data | Data received from the radio platform.
  | length | Length of the received data.

Definition at line 34 of file NewDataIndication.java.

    00034                                                            {
    00035
    00036                 this.data = data;
    00037                 this.dataLength = length;
    00038         }

void main.EventHandler.NewDataIndicationEvent | ( | NewDataIndication | newData | ) | `[inherited]`

Function that is called whenever a new data is received by the radio platform.

Parameters:

  | newData | Object holding information about the received message.

Implements radioInterface.NewDataIndicationListener.

Definition at line 127 of file EventHandler.java.

References gui.ChatWindow.appendIncommingMessage(), and radioInterface.NewDataIndication.getNewData().

    00127                                                                        {
    00128
    00129                 //Append new data.
    00130                 chat.appendIncommingMessage( newData.getNewData( ) );
    00131         }

Here is the call graph for this function:

radioInterface.NewDisassociationIndication.NewDisassociationIndication | ( |  | ) | `[inherited]`

Empty constructor. Object has no attributes.

Definition at line 30 of file NewDisassociationIndication.java.

    00030                                              {
    00031         }

void main.EventHandler.NewDisassociationIndicationEvent | ( | NewDisassociationIndication | newDisAssociation | ) | `[inherited]`

Function that is called whenever a new disassociation is indicated by the radio platform.

Parameters:

  | newDisAssociation | Object holding information about the new disassociation.

Implements radioInterface.NewDisassociationIndicationListener.

Definition at line 115 of file EventHandler.java.

    00115                                                                                                      {
    00116                 // TODO Auto-generated method stub
    00117         }

void main.EventHandler.sendMessage | ( | String | Message | ) | `[inherited]`

Send the send command (AT+T) to the radio platform.

The send command will be sent to the radio, and the user will be prompted with a retry dialog if the AT+T did not return successfully.

Returns:
  True if the send command is successful. False if no correct response is received within the defined number of retries.

Definition at line 490 of file EventHandler.java.

References gui.ChatWindow.appendOutgoingMessage(), and gui.ChatWindow.clearMessage().

Referenced by main.EventHandler.ChatWindowEvent().

    00490                                                  {
    00491
    00492                 //Find length and check if possible to send...30 bytes.
    00493                 int dataLength = Message.length( );
    00494
    00495                 //Zero, do not send anything.
    00496                 if( dataLength == 0 ){
    00497
    00498                         Dialogs.showErrorMessage( Main, "Pointless to send empty message." );
    00499                 }
    00500
    00501                 //Allowed length. Send data.
    00502                 else if( dataLength < MAX_DATA_LENGTH  ){
    00503
    00504                         if( sendData( Message ) ){
    00505
    00506                                 chat.appendOutgoingMessage( Message );
    00507                                 chat.clearMessage( );
    00508                         }
    00509                 }
    00510
    00511                 //Message to long. MAX 30 symbols.
    00512                 else{
    00513
    00514                         Dialogs.showErrorMessage( Main, "Message too long." );
    00515                         chat.clearMessage( );
    00516                 }
    00517         }

Here is the call graph for this function:

@DOC_TITLE@