Unfortunately, we found this to be rather difficult to set up. The reason is that in order for the server to open connections to devices, it would have to know the address of each individual device it could possibly want to talk to. This is not a large problem if the devices were fixed to a LAN line, but that's not the case. Because the scrap devices are on wireless ethernet, it is possible that intermittent signals can cause the device to shift address location.
Thus, to alleviate this problem, the device application initiates the connection first, and keeps the connection open to allow the server to send back data.
The reason that each application on a device opens its own socket to the server is a result of how the device applications got developed. Because the applications were written before the background app, they originally had their own networking code in order to test functionality. As development continued, we decided it was too risky to re-code existing applications to funnel all of their messages to and from the background app. Thus, the result is that all applications open their own networking sockets to the server.
These compromises lead to the following problems:
While these problems are not a big obstacle for our small scale "proof-of-concept" demo, they should be addressed if this project were to be scaled up to full production quality.
Remember that each application must perform this network handshaking, and that the connection created by steps #2 and #3 stay open for the lifetime of the application.
Optional Snapshot Request:
The snapshot request is used by an application to retrieve saved data on the server. Note that the snapshot reply back from the server only goes to the application that requested it. This also assumes that the communication channel between the application and the server has been established.
Optional Update and Synchronization Broadcast
Once the user makes a modification, that modification is sent, via the applications open socket, up to the server. Once the serer has handled the request, it is broadcasted to all applications on all active devices owned by the user. The broadcast includes the originator of the update.
The server is a multi-threaded host. The primary thread listens on the TCP socket 4040. When an incoming connection is detected, the host spawns a worker thread to handle the incoming data package. The worker thread is responsible for determining the packet type, and spawning the necessary 3rd party modules for processing any application specific data.
3rd party modules are required to implement certain interfaces. These interfaces define a return value, which is what the server uses to send/broadcast back to the applications.
3rd party modules are requred to implement the interface Module.java.
IMPORTANT NOTE:
As a hack, in order to accomodate the instant messanger, the return format for the acceptUpdate method in Module.java is irregular. See the javadoc for more details.