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.
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.
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.
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.
23
package org.jboss.test.remoting.performance.raw.socket;
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;
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;
41
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
43
public class SocketCallbackServer implements PerformanceCallbackKeeper
45
private String serverBindAddress = "localhost";
46
private int serverBindPort = 6710;
47
private InetAddress bindAddress;
48
private ServerSocket serverSocket;
49
private int backlog = 2;
51
private String sessionId;
53
private int numberOfCallsProcessed = 0;
54
private int numberofDuplicates = 0;
56
public SocketCallbackServer(String host, String sessionId, Latch lock)
58
this.serverBindAddress = host;
59
this.sessionId = sessionId;
63
public int getNumberOfCallsProcessed()
65
return numberOfCallsProcessed;
68
public int getNumberOfDuplicates()
70
return numberofDuplicates;
73
public int getBindPort()
75
return serverBindPort;
78
public void start() throws Exception
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++)
107
private void calculateBindPort()
109
serverBindPort = TestUtil.getRandomPort();
112
private void startServer()
116
Socket socket = serverSocket.accept();
118
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
119
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
121
ObjectOutputStream oos = new ObjectOutputStream(bos);
123
ObjectInputStream objInputStream = new ObjectInputStream(bis);
125
processRequest(objInputStream, oos);
135
private void processRequest(ObjectInputStream objInputStream, ObjectOutputStream oos)
136
throws IOException, ClassNotFoundException, HandleCallbackException
138
System.out.println("SocketCallbackServer::procesRequest() called " + serverBindPort);
139
Object obj = objInputStream.readObject();
142
objInputStream.readObject();
144
oos.writeObject(Boolean.TRUE);
146
oos.writeObject(Boolean.TRUE);
154
catch(ClassNotFoundException e)
159
Callback callback = (Callback) obj;
160
handleCallback(callback);
165
public void handleCallback(Callback callback) throws HandleCallbackException
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);
180
System.out.println("SocketCallbackServer - released lock " + serverBindPort);