Re: [xSocket-develop] xSocket (multiplexed) and serialized objects
Status: Inactive
Brought to you by:
grro
|
From: Gregor R. <gre...@go...> - 2008-05-30 06:13:24
|
Hi Jeff,
by implementing the onConnect and onDisconnect method you are able to manage
the open connections. See enclosed example. Does this answer your question?
Gregor
private static final class Test implements IDataHandler, IConnectHandler,
IDisconnectHandler {
private final HashMap<String, INonBlockingConnection> openConnections =
new HashMap<String,INonBlockingConnection>();
public boolean onConnect(INonBlockingConnection connection) throws
IOException, BufferUnderflowException, MaxReadSizeExceededException {
connection.setIdleTimeoutMillis(30 * 60 * 1000); // if not data is
be received within 30 min, the connection will be closed
synchronized (openConnections) {
openConnections.put(connection.getId(), connection);
}
return true;
}
public boolean onDisconnect(INonBlockingConnection connection) throws
IOException {
synchronized (openConnections) {
openConnections.remove(connection.getId());
}
return true;
}
public boolean onData(INonBlockingConnection connection) throws
IOException, BufferUnderflowException, MaxReadSizeExceededException {
…
data = connection.read...
...
send(…);
return true;
}
// will be called concurrently
private void send(Object message) {
HashMap<String, INonBlockingConnection> openConnectionsCopy = null;
synchronized (openConnections) {
openConnectionsCopy = (HashMap<String, INonBlockingConnection>)
openConnections.clone();
}
for (INonBlockingConnection connection :
openConnectionsCopy.values()) {
try {
synchronized (connection) { // connection methods are not
threadsafe
connection.write...
...
}
} catch (IOException ioe) {
synchronized (openConnections) {
openConnections.remove(connection.getId());
}
}
}
}
}
Hi Jeff,
by implementing the onConnect and onDisconnect method you are able to manage
the open connections. See enclosed example. Does this answers your question?
Gregor
private static final class Test implements IDataHandler,
IConnectHandler, IDisconnectHandler {
private final HashMap<String, INonBlockingConnection>
openConnections = new HashMap<String, INonBlockingConnection>();
public boolean onConnect(INonBlockingConnection connection) throws
IOException, BufferUnderflowException, MaxReadSizeExceededException {
connection.setIdleTimeoutMillis(30 * 60 * 1000); // if not data
is be received within 30 min, the connection will be closed
synchronized (openConnections) {
openConnections.put(connection.getId(), connection);
}
return true;
}
public boolean onDisconnect(INonBlockingConnection connection)
throws IOException {
synchronized (openConnections) {
openConnections.remove(connection.getId());
}
return true;
}
public boolean onData(INonBlockingConnection connection) throws
IOException, BufferUnderflowException, MaxReadSizeExceededException {
…
data = connection.read...
...
send(…);
return true;
}
// will be called concurrently
private void send(Object message) {
HashMap<String, INonBlockingConnection> openConnectionsCopy =
null;
synchronized (openConnections) {
openConnectionsCopy = (HashMap<String,
INonBlockingConnection>) openConnections.clone();
}
for (INonBlockingConnection connection :
openConnectionsCopy.values()) {
try {
synchronized (connection) { // connection methods are
not threadsafe
connection.write...
...
}
} catch (IOException ioe) {
synchronized (openConnections) {
openConnections.remove(connection.getId());
}
}
}
}
}
2008/5/29 Jeff Crane <JC...@sp...>:
> I read and implemented the answer to my first question. ConnectionUtil
> held the function and usage.
>
>
>
> Clientside:
>
>
>
> client = new NonBlockingConnection(…);
>
> ByteArrayOutputStream bos = new ByteArrayOutputStream();
>
> ObjectOutputStream out = new ObjectOutputStream(bos);
>
> out.writeObject(genericObj);
>
> out.writeObject(DELIMITER);
>
> client.write(bos.size());
>
> client.write(bos.toByteArray());
>
>
>
>
>
> Server Protocol Handler in onData(…)
>
> int length =
> ConnectionUtils.validateSufficientDatasizeByIntLengthField(connection);
>
> byte[] test = connection.readBytesByDelimiter(DELIMITER,length);
>
>
>
> ByteArrayInputStream bis = new ByteArrayInputStream(test);
>
> ObjectInputStream incomingObjstream = new ObjectInputStream(bis);
>
>
>
> Object incomingObj = null;
>
> try{
>
> incomingObj = incomingObjstream.readObject();
>
> }
>
> catch(ClassNotFoundException e){e.printStackTrace();
>
> }
>
> incomingObjstream.close();
>
>
>
> 2. What is the standard practice to index open connections for sending? I
> use my protocol handler to read the object, handle the object, throw an
> event. If the server wants to disconnect or send a message, how do I do a
> lookup? I could do a singleton HashMap or pass a reference in the event
> thrown (which doesn't help if it's interacting with another connection) or
> am I missing something in the xSocket interfaces?
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> xSocket-develop mailing list
> xSo...@li...
> https://lists.sourceforge.net/lists/listinfo/xsocket-develop
>
>
|