mirror of https://github.com/mchr3k/org.intrace
- Add comments to the agent.server package
This commit is contained in:
parent
1aa7539066
commit
df88c16700
|
|
@ -65,9 +65,6 @@ public class Agent
|
|||
t.instrumentLoadedClasses();
|
||||
|
||||
// Start Server thread
|
||||
Thread traceServer = new Thread(new AgentServer(t));
|
||||
traceServer.setName("TraceServer");
|
||||
traceServer.setDaemon(true);
|
||||
traceServer.start();
|
||||
new AgentServer(t).start();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
package org.intrace.agent.server;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
|
|
@ -30,13 +29,13 @@ public class AgentClientConnection implements Runnable
|
|||
|
||||
/**
|
||||
* cTor
|
||||
*
|
||||
* @param agentServer
|
||||
* @param xiConnectedClient
|
||||
* @param xiTransformer
|
||||
*/
|
||||
public AgentClientConnection(AgentServer agentServer,
|
||||
Socket xiConnectedClient,
|
||||
ClassTransformer xiTransformer)
|
||||
Socket xiConnectedClient, ClassTransformer xiTransformer)
|
||||
{
|
||||
super();
|
||||
serverRef = agentServer;
|
||||
|
|
@ -45,6 +44,17 @@ public class AgentClientConnection implements Runnable
|
|||
System.out.println("## Connected to: " + xiConnectedClient.getPort());
|
||||
}
|
||||
|
||||
/**
|
||||
* Main client loop
|
||||
* <ul>
|
||||
* <li>Receive a message and send a response.
|
||||
* </ul>
|
||||
* Special Messages:
|
||||
* <ul>
|
||||
* <li>getsettings - Return a complete configuration Map
|
||||
* <li>help - Return a Set of all the supported commands
|
||||
* </ul>
|
||||
*/
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
|
|
@ -58,7 +68,7 @@ public class AgentClientConnection implements Runnable
|
|||
String message = receiveMessage();
|
||||
if (message.equals("getsettings"))
|
||||
{
|
||||
Map<String,String> settingsMap = new HashMap<String, String>();
|
||||
Map<String, String> settingsMap = new HashMap<String, String>();
|
||||
settingsMap.putAll(transformer.getSettings());
|
||||
settingsMap.putAll(AgentHelper.getSettings());
|
||||
serverRef.broadcastMessage(this, settingsMap);
|
||||
|
|
@ -105,13 +115,19 @@ public class AgentClientConnection implements Runnable
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronously receive a String message.
|
||||
*
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
private String receiveMessage() throws IOException
|
||||
{
|
||||
InputStream in = connectedClient.getInputStream();
|
||||
ObjectInputStream objIn = new ObjectInputStream(in);
|
||||
try
|
||||
{
|
||||
return (String)objIn.readObject();
|
||||
return (String) objIn.readObject();
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
|
|
@ -120,6 +136,12 @@ public class AgentClientConnection implements Runnable
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronously send an Object message.
|
||||
*
|
||||
* @param xiObject
|
||||
* @throws IOException
|
||||
*/
|
||||
public void sendMessage(Object xiObject) throws IOException
|
||||
{
|
||||
OutputStream out = connectedClient.getOutputStream();
|
||||
|
|
@ -127,4 +149,15 @@ public class AgentClientConnection implements Runnable
|
|||
objOut.writeObject(xiObject);
|
||||
objOut.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the Client connection - create a new, named, daemon thread.
|
||||
*/
|
||||
public void start(int clientNum)
|
||||
{
|
||||
Thread clientThread = new Thread(this);
|
||||
clientThread.setDaemon(true);
|
||||
clientThread.setName("AgentServer-Client" + clientNum);
|
||||
clientThread.start();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package org.intrace.agent.server;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.BindException;
|
||||
import java.net.ServerSocket;
|
||||
|
|
@ -11,29 +10,44 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
import org.intrace.agent.ClassTransformer;
|
||||
|
||||
/**
|
||||
* TCP Server used for communication with the Trace client.
|
||||
* TCP Server used for communication with Trace clients.
|
||||
*/
|
||||
public class AgentServer implements Runnable
|
||||
{
|
||||
// CTor field
|
||||
private final ClassTransformer transformer;
|
||||
|
||||
// Map of client connections
|
||||
private final Map<AgentClientConnection, Object> clientConnections = new ConcurrentHashMap<AgentClientConnection, Object>();
|
||||
|
||||
/**
|
||||
* cTor
|
||||
* cTor s
|
||||
*
|
||||
* @param xiT
|
||||
*/
|
||||
public AgentServer(ClassTransformer xiT)
|
||||
{
|
||||
transformer= xiT;
|
||||
transformer = xiT;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param connection
|
||||
* Remove this connection.
|
||||
*/
|
||||
public void removeClientConnection(AgentClientConnection connection)
|
||||
{
|
||||
clientConnections.remove(connection);
|
||||
}
|
||||
|
||||
public void broadcastMessage(AgentClientConnection requestingConn, Object message) throws IOException
|
||||
/**
|
||||
* Broadcast a message to all currently connected clients.
|
||||
*
|
||||
* @param requestingConn
|
||||
* @param message
|
||||
* @throws IOException
|
||||
*/
|
||||
public void broadcastMessage(AgentClientConnection requestingConn,
|
||||
Object message) throws IOException
|
||||
{
|
||||
IOException ex = null;
|
||||
for (AgentClientConnection clientConn : clientConnections.keySet())
|
||||
|
|
@ -44,6 +58,7 @@ public class AgentServer implements Runnable
|
|||
}
|
||||
catch (IOException ioex)
|
||||
{
|
||||
// Only remember exceptions for the connection sending the message
|
||||
if (requestingConn == clientConn)
|
||||
{
|
||||
ex = ioex;
|
||||
|
|
@ -56,13 +71,25 @@ public class AgentServer implements Runnable
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main server loop
|
||||
*/
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
// Server constants
|
||||
|
||||
// Number used for naming client threads
|
||||
int clientNum = 1;
|
||||
int numAllowedExcept = 10;
|
||||
|
||||
// Number of allowed exceptions before we give up
|
||||
int numAllowedExceptions = 10;
|
||||
|
||||
// Default listen port - we increment the port if we cannot listen on this
|
||||
// port
|
||||
int tracePort = 9123;
|
||||
while (numAllowedExcept > 0)
|
||||
|
||||
while (numAllowedExceptions > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -71,24 +98,24 @@ public class AgentServer implements Runnable
|
|||
while (true)
|
||||
{
|
||||
Socket connectedClient = serversock.accept();
|
||||
AgentClientConnection clientConnection = new AgentClientConnection(this, connectedClient, transformer);
|
||||
AgentClientConnection clientConnection = new AgentClientConnection(
|
||||
this,
|
||||
connectedClient,
|
||||
transformer);
|
||||
clientConnections.put(clientConnection, new Object());
|
||||
Thread clientThread = new Thread(clientConnection);
|
||||
clientThread.setDaemon(true);
|
||||
clientThread.setName("AgentServer-Client" + clientNum);
|
||||
clientThread.start();
|
||||
clientConnection.start(clientNum);
|
||||
clientNum++;
|
||||
}
|
||||
}
|
||||
catch (BindException e)
|
||||
{
|
||||
numAllowedExcept--;
|
||||
numAllowedExceptions--;
|
||||
System.out.println("## Unable to listen on port: " + tracePort);
|
||||
tracePort++;
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
numAllowedExcept--;
|
||||
numAllowedExceptions--;
|
||||
t.printStackTrace();
|
||||
try
|
||||
{
|
||||
|
|
@ -96,10 +123,21 @@ public class AgentServer implements Runnable
|
|||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
// Throw away
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("## Too many exceptions - server thread quitting.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the Server - create a new, named, daemon thread.
|
||||
*/
|
||||
public void start()
|
||||
{
|
||||
Thread traceServer = new Thread(this);
|
||||
traceServer.setName("TraceServer");
|
||||
traceServer.setDaemon(true);
|
||||
traceServer.start();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue