~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/performance/raw/socket/SocketCallbackServer.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* JBoss, Home of Professional Open Source
 
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
 
4
* by the @authors tag. See the copyright.txt in the distribution for a
 
5
* full listing of individual contributors.
 
6
*
 
7
* This is free software; you can redistribute it and/or modify it
 
8
* under the terms of the GNU Lesser General Public License as
 
9
* published by the Free Software Foundation; either version 2.1 of
 
10
* the License, or (at your option) any later version.
 
11
*
 
12
* This software is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
* Lesser General Public License for more details.
 
16
*
 
17
* You should have received a copy of the GNU Lesser General Public
 
18
* License along with this software; if not, write to the Free
 
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
21
*/
 
22
 
 
23
package org.jboss.test.remoting.performance.raw.socket;
 
24
 
 
25
import java.util.concurrent.Latch;
 
26
import org.jboss.remoting.callback.Callback;
 
27
import org.jboss.remoting.callback.HandleCallbackException;
 
28
import org.jboss.test.remoting.TestUtil;
 
29
import org.jboss.test.remoting.performance.synchronous.PerformanceCallbackKeeper;
 
30
 
 
31
import java.io.BufferedInputStream;
 
32
import java.io.BufferedOutputStream;
 
33
import java.io.IOException;
 
34
import java.io.ObjectInputStream;
 
35
import java.io.ObjectOutputStream;
 
36
import java.net.InetAddress;
 
37
import java.net.ServerSocket;
 
38
import java.net.Socket;
 
39
 
 
40
/**
 
41
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
 
42
 */
 
43
public class SocketCallbackServer implements PerformanceCallbackKeeper
 
44
{
 
45
   private String serverBindAddress = "localhost";
 
46
   private int serverBindPort = 6710;
 
47
   private InetAddress bindAddress;
 
48
   private ServerSocket serverSocket;
 
49
   private int backlog = 2;
 
50
 
 
51
   private String sessionId;
 
52
   private Latch lock;
 
53
   private int numberOfCallsProcessed = 0;
 
54
   private int numberofDuplicates = 0;
 
55
 
 
56
   public SocketCallbackServer(String host, String sessionId, Latch lock)
 
57
   {
 
58
      this.serverBindAddress = host;
 
59
      this.sessionId = sessionId;
 
60
      this.lock = lock;
 
61
   }
 
62
 
 
63
   public int getNumberOfCallsProcessed()
 
64
   {
 
65
      return numberOfCallsProcessed;
 
66
   }
 
67
 
 
68
   public int getNumberOfDuplicates()
 
69
   {
 
70
      return numberofDuplicates;
 
71
   }
 
72
 
 
73
   public int getBindPort()
 
74
   {
 
75
      return serverBindPort;
 
76
   }
 
77
 
 
78
   public void start() throws Exception
 
79
   {
 
80
      calculateBindPort();
 
81
 
 
82
      bindAddress = InetAddress.getByName(serverBindAddress);
 
83
      serverSocket = new ServerSocket(serverBindPort, backlog, bindAddress);
 
84
      System.out.println("started SocketCallbackServer on port " + serverBindPort);
 
85
      // this was done inline since TestCase already has a void parameter run() method
 
86
      // so could not create a run() method for the Runnable implementation.
 
87
      for(int x = 0; x < 1; x++)
 
88
      {
 
89
         new Thread()
 
90
         {
 
91
            public void run()
 
92
            {
 
93
               try
 
94
               {
 
95
                  startServer();
 
96
               }
 
97
               catch(Exception e)
 
98
               {
 
99
                  e.printStackTrace();
 
100
               }
 
101
            }
 
102
         }.start();
 
103
      }
 
104
 
 
105
   }
 
106
 
 
107
   private void calculateBindPort()
 
108
   {
 
109
      serverBindPort = TestUtil.getRandomPort();
 
110
   }
 
111
 
 
112
   private void startServer()
 
113
   {
 
114
      try
 
115
      {
 
116
         Socket socket = serverSocket.accept();
 
117
 
 
118
         BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
 
119
         BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
 
120
 
 
121
         ObjectOutputStream oos = new ObjectOutputStream(bos);
 
122
         oos.flush();
 
123
         ObjectInputStream objInputStream = new ObjectInputStream(bis);
 
124
 
 
125
         processRequest(objInputStream, oos);
 
126
 
 
127
      }
 
128
      catch(Exception e)
 
129
      {
 
130
         e.printStackTrace();
 
131
      }
 
132
   }
 
133
 
 
134
 
 
135
   private void processRequest(ObjectInputStream objInputStream, ObjectOutputStream oos)
 
136
         throws IOException, ClassNotFoundException, HandleCallbackException
 
137
   {
 
138
      System.out.println("SocketCallbackServer::procesRequest() called " + serverBindPort);
 
139
      Object obj = objInputStream.readObject();
 
140
      try
 
141
      {
 
142
         objInputStream.readObject();
 
143
 
 
144
         oos.writeObject(Boolean.TRUE);
 
145
         oos.reset();
 
146
         oos.writeObject(Boolean.TRUE);
 
147
         oos.flush();
 
148
         oos.reset();
 
149
      }
 
150
      catch(IOException e)
 
151
      {
 
152
         e.printStackTrace();
 
153
      }
 
154
      catch(ClassNotFoundException e)
 
155
      {
 
156
         e.printStackTrace();
 
157
      }
 
158
 
 
159
      Callback callback = (Callback) obj;
 
160
      handleCallback(callback);
 
161
 
 
162
   }
 
163
 
 
164
 
 
165
   public void handleCallback(Callback callback) throws HandleCallbackException
 
166
   {
 
167
      System.out.println("SocketCallbackServer::handleCallback() called " + serverBindPort);
 
168
      Object ret = callback.getCallbackObject();
 
169
      Integer[] handledArray = (Integer[]) ret;
 
170
      Integer numOfCallsHandled = (Integer) handledArray[0];
 
171
      Integer numOfDuplicates = (Integer) handledArray[1];
 
172
      System.out.println("Server is done.  Number of calls handled: " + numOfCallsHandled);
 
173
      numberOfCallsProcessed = numOfCallsHandled.intValue();
 
174
      System.out.println("Number of duplicate calls: " + numOfDuplicates);
 
175
      numberofDuplicates = numOfDuplicates.intValue();
 
176
      Object obj = callback.getCallbackHandleObject();
 
177
      //String handbackObj = (String) obj;
 
178
      //System.out.println("Handback object should be " + sessionId + " and server called back with " + handbackObj);
 
179
      lock.release();
 
180
      System.out.println("SocketCallbackServer - released lock " + serverBindPort);
 
181
   }
 
182
 
 
183
 
 
184
}