Re: [xSocket-develop] How to create a blocking server using streams?
Status: Inactive
Brought to you by:
grro
|
From: Gregor R. <gre...@go...> - 2008-06-15 17:55:01
|
Hi Robert,
the problem here is that the BlockingConnection replaces the assigned
handler of the NonBlockingConection by an internal one. The internal handler
is used by the BlockingConnection to be notified about network events.
Unfortunately this is not documented. I'm going to do this.
The workaround for your code is to set the origin handler by leaving the
onData method.
class TWPHandler implements IDataHandler, IConnectionScoped {
public boolean onData(INonBlockingConnection nbc) throws IOException,
BufferUnderflowException, MaxReadSizeExceededException {
IBlockingConnection bc = new BlockingConnection(nbc);
switch (this.mode) {
case INIT:
this.init(bc);
this.mode = MESSAGE;
case MESSAGE:
int messageId = this.readMessageId(bc);
Class<?> messageType = classForId.get(messageId);
Object requestMessage = this.readRequestMessage(bc,
messageType);
// handle the message
Object replyMessage =
handlerForClass.get(messageType).onMessage(requestMessage);
this.writeReplyMessage(bc, replyMessage);
}
nbc.setHandler(this);
return true;
}
}
regards
Gregor
2008/6/15 Robert Wierschke <wie...@go...>:
> Hi,
>
> how do I create a blocking server using streams? My current try only
> accepts one "message" per connection and than hangs!
>
> I just started using xSocket and have the following problem:
>
> I already have some (un-)/marshaling mechanism that I need to use on the
> server side. This mechanism unfortunately works with streams so I used
> Channels method to create a stream from the connection object
>
> private Object readRequestMessage(IBlockingConnection bc, Class<?>
> messageType) throws IOException {
>
> // wrap an InputStream around the incomming data to feed it into
> the unmarshaller
> InputStream in = Channels.newInputStream(bc);
>
> Unmarshaller unmarshaller = new Unmarshaller(in);
>
> Object result = unmarshaller.unmarshalMessage(messageType);
>
> return result;
>
> }
>
> private void writeReplyMessage(IBlockingConnection bc, Object
> replyMessage) throws IOException {
>
> if (replyMessage == null)
>
> return; // nothing to do
>
> OutputStream out = Channels.newOutputStream(bc);
>
> StructuredDataMarsahller marshaller = new
> StructuredDataMarsahller(out);
>
> marshaller.marshal(replyMessage);
>
> }
>
> Both messages are called from IDataHandler.onData
>
> class TWPHandler implements IDataHandler, IConnectionScoped {
>
> public boolean onData(INonBlockingConnection nbc) throws IOException,
> BufferUnderflowException, MaxReadSizeExceededException {
>
> IBlockingConnection bc = new BlockingConnection(nbc);
>
> switch (this.mode) {
>
> case INIT:
>
> this.init(bc);
>
> this.mode = MESSAGE;
>
> case MESSAGE:
>
> int messageId = this.readMessageId(bc);
> Class<?> messageType = classForId.get(messageId);
>
> Object requestMessage = this.readRequestMessage(bc,
> messageType);
> // handle the message
>
> Object replyMessage =
> handlerForClass.get(messageType).onMessage(requestMessage);
>
> this.writeReplyMessage(bc, replyMessage);
>
> }
> return true;
>
> }
> }
>
> I'm also using mark support because I need to read some bytes that the
> unmarshaller need to read again.
>
> private int readMessageId(IBlockingConnection bc) throws IOException
> {
>
> int messageId;
>
> synchronized (bc) {
>
> bc.markReadPosition();
>
> // read message id
> // ...
> bc.resetToReadMark(); // so that the id can be read again by
> the unmarshaller
>
> }
>
> return messageId;
>
> }
>
> This all works fine if there is only one request and one response message
> per connection. But if I try to send another request message on the same
> connection the server hangs forever.
>
> So what am I doing wrong?
>
> thanks in advance
> robert
>
>
> -------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services for
> just about anything Open Source.
> http://sourceforge.net/services/buy/index.php
> _______________________________________________
> xSocket-develop mailing list
> xSo...@li...
> https://lists.sourceforge.net/lists/listinfo/xsocket-develop
>
>
|