Full duplex communication?

Showing 1-11 of 11 messages
Full duplex communication? Rahul Somasunderam 4/4/12 12:27 PM
Hi,

I'm having a server running atmosphere inside of grails on tomcat 7. My client is not a browser, but a java based client built with AHC.

I've been successful in implementing this model:


Server starts up.
Client starts up.
<-- Client connects to server.
--> Server receives message somehow that needs to be sent to client.
Server sends message to client -->
Client receives message
Client creates response
<-- Client sends response over a new connection.

This works beautifully. I've now got a new scenario to address:

Server starts up.
Client starts up.
<-- Client connects to server.
--> Server receives message somehow that needs to be sent to client.
Server sends message to client -->
Client receives message
Client creates response
<-- Client sends response over same connection.
<-- Server responds to the source of the message synchronously

Changes are in bold.

Has anyone implemented something like this? Is it possible?
TIA.

R,
rahul
Re: Full duplex communication? jfarcand 4/4/12 12:39 PM
Salut, yes, that will works with WebSocket for sure (open a single connection, duplex call on it). It can work as well using HTTP + keep-alive (but you have to make sure your HTTP client/Javascript re-use the same connection for the client second request) => for sure that will works with long-polling. For http-streaming I don't think you can re-use the same connection.

Which client are you using?

A+

-- Jeanfrancois





TIA.

R,
rahul
--
You received this message because you are subscribed to the Google Groups "asynchttpclient" group.
To post to this group, send email to asyncht...@googlegroups.com.
To unsubscribe from this group, send email to asynchttpclie...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/asynchttpclient?hl=en.
Re: Full duplex communication? jfarcand 4/4/12 12:42 PM
Just realized you cross posted to Atmosphere and AHC :-) ... so I know the client you are using.

The answer is yes, it will be easy with AHC to re-use the same connection. Just set the number of connection to a host to 1 so you are guarantee to re-use the connection.

A+

-- Jeanfrancois
Re: Full duplex communication? Rahul Somasunderam 4/4/12 1:00 PM
Hi Jeanfrançois,

I'm using your AsyncHttpClient library with a custom handler that implements AsyncHandler<String>. Would I have to rewrite it to use Websockets?
I believe the server component will automcatically support WebSockets since it uses Atmosphere.

R,
rahul
Re: Full duplex communication? jfarcand 4/4/12 1:09 PM
Salut,


On 12-04-04 4:00 PM, Rahul Somasunderam wrote:
Hi Jeanfrançois,

I'm using your AsyncHttpClient library with a custom handler that implements AsyncHandler<String>. Would I have to rewrite it to use Websockets?
I believe the server component will automcatically support WebSockets since it uses Atmosphere.
With AHC you will need to switch handler...I'm working on an Atmosphere Java Client for the upcoming 1.0.0 release that will hides that and will negotiate with the Atmosphere server which transport is the best/optimal....but first I need to release Atmosphere 0.9 before I start working on that :-) So long answer to say that you will need to do some AHC work ... Tomcat 7 doesn't yet support Websocket so you may have to be force to use HTTP (or 7.0.26 might be, need to check).

A+

-- Jeanfrancois
Re: Full duplex communication? Rahul Somasunderam 4/4/12 2:47 PM
7.0.26 still doesn't support WebSockets.

Thanks!

R,
rahul
Re: Full duplex communication? Rahul Somasunderam 4/5/12 2:03 PM
Looks like I got lucky. I upgraded my tomcat to 7.0.27. And AHC to 1.7.2.

I'm now getting this exception:

java.lang.IllegalStateException: Invalid upgrade protocol, status should be 101 but was 501
at com.ning.http.client.websocket.WebSocketUpgradeHandler.onStatusReceived(WebSocketUpgradeHandler.java:67)
at com.ning.http.client.providers.netty.NettyAsyncHttpProvider$WebSocketProtocol.handle(NettyAsyncHttpProvider.java:2307)
at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.messageReceived(NettyAsyncHttpProvider.java:1117)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.unfoldAndFireMessageReceived(ReplayingDecoder.java:522)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:501)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:438)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:343)
at org.jboss.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:274)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:194)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
Caught: java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Invalid upgrade protocol, status should be 101 but was 501
java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Invalid upgrade protocol, status should be 101 but was 501
at com.ning.http.client.providers.netty.NettyResponseFuture.abort(NettyResponseFuture.java:297)
at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.abort(NettyAsyncHttpProvider.java:1315)
at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.exceptionCaught(NettyAsyncHttpProvider.java:1531)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:117)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:558)
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:777)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.exceptionCaught(ReplayingDecoder.java:456)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:117)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:558)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:553)
at org.jboss.netty.channel.Channels.fireExceptionCaught(Channels.java:426)
at org.jboss.netty.channel.AbstractChannelSink.exceptionCaught(AbstractChannelSink.java:47)
at org.jboss.netty.channel.DefaultChannelPipeline.notifyHandlerException(DefaultChannelPipeline.java:645)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:560)
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:777)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.unfoldAndFireMessageReceived(ReplayingDecoder.java:522)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:501)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:438)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:75)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:558)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:553)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:343)
at org.jboss.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:274)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:194)
at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
Caused by: java.lang.IllegalStateException: Invalid upgrade protocol, status should be 101 but was 501
at com.ning.http.client.websocket.WebSocketUpgradeHandler.onStatusReceived(WebSocketUpgradeHandler.java:67)
at com.ning.http.client.providers.netty.NettyAsyncHttpProvider$WebSocketProtocol.handle(NettyAsyncHttpProvider.java:2307)
at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.messageReceived(NettyAsyncHttpProvider.java:1117)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.unfoldAndFireMessageReceived(ReplayingDecoder.java:522)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:501)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:438)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:343)
at org.jboss.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:274)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:194)
730 [New I/O client worker #1-1] WARN com.ning.http.client.providers.netty.NettyAsyncHttpProvider - onError [id: 0x50c7833c, /127.0.0.1:58254 => localhost/127.0.0.1:8080] EXCEPTION: java.lang.IllegalStateException: Invalid upgrade protocol, status should be 101 but was 501
733 [New I/O client worker #1-1] ERROR com.ning.http.client.providers.netty.NettyAsyncHttpProvider - onError
java.lang.IllegalStateException: WebSocket is null
at com.ning.http.client.websocket.WebSocketUpgradeHandler.onCompleted(WebSocketUpgradeHandler.java:85)
at com.ning.http.client.providers.netty.NettyAsyncHttpProvider$WebSocketProtocol.onError(NettyAsyncHttpProvider.java:2370)
at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.exceptionCaught(NettyAsyncHttpProvider.java:1538)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:117)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:558)
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:777)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.exceptionCaught(ReplayingDecoder.java:456)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:117)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:558)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:553)
at org.jboss.netty.channel.Channels.fireExceptionCaught(Channels.java:426)
at org.jboss.netty.channel.AbstractChannelSink.exceptionCaught(AbstractChannelSink.java:47)
at org.jboss.netty.channel.DefaultChannelPipeline.notifyHandlerException(DefaultChannelPipeline.java:645)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:560)
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:777)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.unfoldAndFireMessageReceived(ReplayingDecoder.java:522)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:501)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:438)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:75)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:558)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:553)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:343)
at org.jboss.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:274)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:194)
at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)

R,
rahul


To view this discussion on the web visit https://groups.google.com/d/msg/asynchttpclient/-/Knj9jvtOld8J.

To post to this group, send email to asyncht...@googlegroups.com.
To unsubscribe from this group, send email to asynchttpclie...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/asynchttpclient?hl=en.

Re: Full duplex communication? cvuijst 4/19/12 12:15 PM
Hi  Jeanfrancois,

I tried with an embedded jetty (8.1.2.v20120308), Atmospere (0.9) and AHC (1.7.1. and 1.8.0-SNAPSHOT). The exception I got is: at com.ning.http.client.providers.netty.NettyResponseFuture: Invalid Upgrade protocol. Also, I tried the embedded Jetty without web.xml, so: 

// Servlet context handler
ServletContextHandler handler = new ServletContextHandler(
ServletContextHandler.NO_SESSIONS);
server.setHandler(handler);
// TRYING THE ATMOSHPERE HANDLER
AtmosphereServlet atmosphereServlet = new AtmosphereServlet();
ServletHolder atmosphereServletHolder = new ServletHolder (atmosphereServlet);
atmosphereServletHolder.setInitParameter(
"org.atmosphere.useWebSocket",
"true");
atmosphereServletHolder.setInitParameter(
"org.atmosphere.websocket.WebSocketProtocol",
"net.vuijst.test.WebSocketHandlerProtocol");
atmosphereServletHolder.setAsyncSupported(true);
handler.addServlet(atmosphereServletHolder, "/b/*");

Could it be that the same issue related to Tomcat also related to Jetty? I attached the Maven based Eclipse project for your convenience (see readme.txt in its root to reproduce) 

Regards,

Charles
Re: Full duplex communication? jfarcand 4/19/12 12:19 PM
Salut,

You need to set the AsyncSupport as described here

   https://github.com/Atmosphere/atmosphere/blob/master/modules/acceptance-tests/src/test/java/org/atmosphere/tests/websocket/JettyWebSocketSupportTest.java#L48

A+

-- Jeanfrancois
To view this discussion on the web visit https://groups.google.com/d/msg/asynchttpclient/-/tGLqRsncjEkJ.

To post to this group, send email to asyncht...@googlegroups.com.
To unsubscribe from this group, send email to asynchttpclie...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/asynchttpclient?hl=en.
Re: Full duplex communication? cvuijst 4/23/12 10:12 AM
Thanks  Jeanfrancois ,

That was very helpful; got it working now! 

-- Charles

Op donderdag 19 april 2012 21:19:20 UTC+2 schreef jfarcand het volgende:
Re: Full duplex communication? Giorgio 7/14/12 4:21 AM
Hi! Unfortunately the link https://github.com/Atmosphere/atmosphere/blob/master/modules/acceptance-tests/src/test/java/org/atmosphere/tests/websocket/JettyWebSocketSupportTest.java#L48
is not available anymore and I'm getting a pretty similar error: (java.lang.IllegalStateException: Invalid upgrade protocol, status should be 101 but was 200)

I'm using Atmosphere-jersey 0.9.7 , Jetty & jetty-websocket 7.6RC4 , sonatype/async-http-client 1.7.4 , Jersey 1.12

Thanks so much for Atmosphere+Jersey!If I'll make it work it will save me so much time and I will some awesome real-time rest api! :)

This is the stacktrace called from WebSocketTextListener.onError(Throwable t) of my HTTP POST request:

java.lang.IllegalStateException: Invalid upgrade protocol, status should be 101 but was 200

at com.ning.http.client.websocket.WebSocketUpgradeHandler.onStatusReceived(WebSocketUpgradeHandler.java:67)

at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.updateStatusAndInterrupt(NettyAsyncHttpProvider.java:1457)

at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.access$2000(NettyAsyncHttpProvider.java:137)

at com.ning.http.client.providers.netty.NettyAsyncHttpProvider$HttpProtocol.handle(NettyAsyncHttpProvider.java:2209)

at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.messageReceived(NettyAsyncHttpProvider.java:1123)

at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:75)

at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)

at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:792)

at org.jboss.netty.handler.stream.ChunkedWriteHandler.handleUpstream(ChunkedWriteHandler.java:145)

at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)

at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:792)

at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)

at org.jboss.netty.handler.codec.replay.ReplayingDecoder.unfoldAndFireMessageReceived(ReplayingDecoder.java:567)

at org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:551)

at org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:445)

at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:75)

at org.jboss.netty.handler.codec.http.HttpClientCodec.handleUpstream(HttpClientCodec.java:92)

at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)

at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)

at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)

at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)

at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:94)

at org.jboss.netty.channel.socket.nio.AbstractNioWorker.processSelectedKeys(AbstractNioWorker.java:364)

at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:238)

at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:38)

at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)

at java.lang.Thread.run(Thread.java:680)



This is the code I use to start my AtmosphereServlet:

AtmosphereServlet atmosphereServlet = new AtmosphereServlet();

ServletHolder servletHolder = new ServletHolder(atmosphereServlet);

//Atmosphere

servletHolder.setInitParameter("org.atmosphere.websocket.messageContentType" , "application/json");

servletHolder.setInitParameter("org.atmosphere.useWebSocket","true");

servletHolder.setAsyncSupported(true);

//Jersey resources

// servletHolder.setInitParameter("com.sun.jersey.config.property.packages", "com.famenu.server.resources");

servletHolder.setInitParameter("com.sun.jersey.config.property.resourceConfigClass" , "com.sun.jersey.api.core.ClassNamesResourceConfig");

servletHolder.setInitParameter("com.sun.jersey.config.property.classnames"

"RESOURCE_CLASS_1, RESOURCE_CLASS_2"); 

servletHolder.setInitParameter("com.sun.jersey.spi.container.ContainerRequestFilters" , "com.sun.jersey.api.container.filter.LoggingFilter");

servletHolder.setInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters" , "com.sun.jersey.api.container.filter.LoggingFilter");

servletHolder.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature" , "true");

ServletContextHandler servletContextHandler = new ServletContextHandler(webServer, "/rest", true, false);

servletContextHandler.addServlet(servletHolder, "/");