Wednesday, February 29, 2012

A little framework for TCP client-server communications (part1)

Hello world! I'm back from work and now it's time for requiem... :)

Today I wanna talk about a little framework written in Java for managing tcp/ip sockets connections.
Why I need a framework such this and what can I do with this framework ?

Remember that a good framework is very useful if can save you a lot of time/money on writing a lot of code. I think that JRR (Java Request Response) has a big potential and if you have to write an application,on every kind of device that supports internet, you should try it and see how it's simple to manage it all

Zooming out on features: with JRR you can:

  • Enstablish a Server that listens on a port; Provide your own implementation of handling client messages and server response. Also manages timeouts.
  • Create a Client that send requests to server, and handles response, exceptions, connection timeout and custom response timeout. 
P.S. If you don't provide your implementation, classes have their own.

Want to know more? Keep scrolling this post...
Some examples of application done with JRR:
  • =>A server application for Windows that controls some computer's stuffs such as volume control, processes, services. It also picks print screens and leave messages on computer.
    =>A client for Android that sends inputs to server that works and do things under client's commands.
  • =>A mouse handler for Android. With the touch input you can control mouse on PC.
    =>The relative server for PC that allows to receive commands and moves the cursor.
  • =>A client-server/client-server system (mobile/pc) for exchanging files.  
But these are only few possibilities. With this FW you can realize even chats and much more...

How it works? In a simple way..

Opens a tcp socket on a specified port and begin to listen.
It accepts connections and for everyclient connected starts a new chat thread where it receives datas and sends it to a class called RequestWorker. This class is abstract and need to be implemented. It has a method called "work" that has a input String. That method returns the response to send to the client.
At client disconnection, thread is stopped and it's removed from clientList. 
For more details and DefaultRequestWorker see the second part of this post.

Opens a tcp connection to a specified ip,port and after handshake can send requests to server. 
Call to method is: a String for invokedMethod, and a array of strings for Parameters a Callback abstract class to invoke when response arrives, an optional timeout.
Requests are prepared from an abstract class called RequestBuilder.
After request is sent to server, it's added to an array of requests.
When Response arrives, onReceive method of callback is invoked and request is removed from the array.
If timeout expires, onTimeout method of callback is invoked and request is removed from the array.
Developer can intercepts the moment when String from server arrives and obtain a Response object.
For futher details the second part of this post.

In future, during my requiem days, I will extend FW to support other network protocols such as UDP.
So don't worry, TCP is just the first step of beginning.

Here ends the part1 of this post.

Next time I will explain packages,classes, and how to customize classes behavior
Expect examples, and also a JAR :)

Goodnight to all!

No comments:

Post a Comment