We help IT Professionals succeed at work.

How could i create a socket connection pooling for handling a custom ascii text protocol?

1,751 Views
Last Modified: 2008-07-17
I have a webservices multithreading service made in java with apache mina. It receives multiples connection and i need after that parse the webservice data and create a custom ascii protocol request to send thru tcp ip socket. What i would like to have is some way to pool the connections or have only 1 connection and pool the inputstream and outputstream for parsing the strings that contains the response with multithreading in mind.

Do you know of any documentation or library that does something like this from where i could get the ideas for this?
 
Comment
Watch Question

Commented:
I'm not sure if I understand correctly but you are interested to pool all connections and see what data do you received or do you have in some internal buffers specific to every connection.

A simple approach is to create thread safe list(or hash list) of buffers and every time when you receive something you will put also in that list. When that buffer is no longer available on your connection just remove it also from list or hash list.

Author

Commented:
I don't have a specific approach. I'm trying to hear from programmers ways to handle this objective.

The idea I have is the next:



public class Request {
    public String Id;
    public String Body;
}

public class ConnectionManager {

    ArrayBlockingQueue outputQueue = new ArrayBlockingQueue(50);
    Map inputMap = new HashMap(50);
    Socket clientSocket;
    DataInputStream clientIS;
    DataOutputStream clientOS;
   
    public ConnectionManager (String address, int port) {
         clientSocket = new Socket(address, port);
         clientIS = new DataInputStream(clientSocket.getInputStream());
         clientOS = new DataOutputStream(clientSocket.getOutputStream());
    }
   
    public String sendMessage(Request request)
    {
        outputQueue.put(request);
        inputMap.put(request.Id, "");
       
        return "";
    }
   
}

Were Im stuck is in the multithreading parts that needs to insert into the outputstream from the ArrayBlockingQueue and the thread that need to put the response coming from the inputstream to the hashMap with the corresponding id. This way I only need to create a connection manager object and then call ConnectionManager.sendMessage(Request).

I'm open to others ideas to do the same thing.

Commented:
ConnectionManager has to contain an array of sockets DataInputStream and DataOutputStream.

I don't know very well java sintax but I'm quite sure that will be very easy for you to understand and adapt.

Socket[] clientSocket = new Socket[50]();
DataInputStream[] clientIS...;
DataOutputStream[] clientOS..;
Boolean[] freeConnections = new Boolean[50]();
 
// initialize all connections
public ConnectionManager (String address, int port) 
{
  for(int i=0; i<50; i++)
  {
         clientSocket[i] = new Socket(address, port);
         clientIS[i] = new DataInputStream(clientSocket.getInputStream());
         clientOS[i] = new DataOutputStream(clientSocket.getOutputStream());
  }
}
 
 public String sendMessage(Request request) 
    {
        outputQueue.put(request);
 
        int i = AquireConnection();
        clientOS[i].write(request.body);
        string body = clientIS[i].read();
        inputMap.put(request.Id, body);
 
        return "";
    }
 
   // retrieve the index of the first free connection 
   int AquireConnection()
   {
     synchronized(this)
     {
      for(int i=0;i<50; i++)
      {
       if (freeConnections[i] == true)
       {
          // mark connection as used
          freeConnections[i] = false;
          return i;
        }
     }
    }
     // no connection available
     return -1;
   }
 
   ReleaseConnection(int i)
   {
       synchronized(this)
       {
          freeConnections[i] = true;
       }
   }

Open in new window

Author

Commented:
Is this solutions multithread safe? because multiple threads will try to comunicate and sendmessage. Another question comes about what happen if one connection is disconnected from the server, how could I recover this connection in the array?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.