Package org.jboss.netty.channel

Examples of org.jboss.netty.channel.Channel


   * {@inheritDoc}
   * @see org.jboss.netty.channel.ChannelUpstreamHandler#handleUpstream(org.jboss.netty.channel.ChannelHandlerContext, org.jboss.netty.channel.ChannelEvent)
   */
  @Override
  public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
    final Channel channel = e.getChannel();
    if(e instanceof MessageEvent) {
      MessageEvent messageEvent = (MessageEvent)e;
      Object message = messageEvent.getMessage();
      if(message instanceof HttpRequest) {
        HttpRequest request = (HttpRequest)message;
        int metricCount = processMetric(request);
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        response.setContent(ChannelBuffers.copiedBuffer("\n" + metricCount + "\n", CharsetUtil.UTF_8));
        response.setHeader(CONTENT_TYPE, "text/plain");
        ChannelFuture future = Channels.future(channel);
        ctx.sendDownstream(new DownstreamMessageEvent(channel, future, response, channel.getRemoteAddress()));
        future.addListener(ChannelFutureListener.CLOSE);       
      }
    }
    ctx.sendUpstream(e);
  }
View Full Code Here


   * {@inheritDoc}
   * @see org.jboss.netty.channel.ChannelDownstreamHandler#handleDownstream(org.jboss.netty.channel.ChannelHandlerContext, org.jboss.netty.channel.ChannelEvent)
   */
  @Override
  public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
    Channel channel = e.getChannel();
    if(!channel.isOpen()) return;
    if(!(e instanceof MessageEvent)) {
            ctx.sendDownstream(e);
            return;
        }
    Object message = ((MessageEvent)e).getMessage();
    if(!(message instanceof JSONObject) && !(message instanceof CharSequence)) {
            ctx.sendDownstream(e);
            return;     
    }
    WebSocketFrame frame = new TextWebSocketFrame(message.toString());   
    ctx.sendDownstream(new DownstreamMessageEvent(channel, Channels.future(channel), frame, channel.getRemoteAddress()));   

  }
View Full Code Here

                response.setContent(ChannelBuffers.wrappedBuffer(content));
                contentCache.put(resourcePath, response);
          }
        }
      }
          Channel ch = e.getChannel();

          // Write the initial line and the header.
          return ch.write(response);

     
    } finally {
      if(is!=null) try { is.close(); } catch (Exception e2) {}
      if(baos!=null) try { baos.close(); } catch (Exception e3) {}
View Full Code Here

        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        setContentLength(response, fileLength);
        setContentTypeHeader(response, file.getAbsolutePath());
        setDateAndCacheHeaders(response, file);
       
        Channel ch = e.getChannel();

        // Write the initial line and the header.
        ch.write(response);

        // Write the content.
        ChannelFuture writeFuture;
        if (ch.getPipeline().get(SslHandler.class) != null) {
            // Cannot use zero-copy with HTTPS.
            writeFuture = ch.write(new ChunkedFile(raf, 0, fileLength, 8192));
        } else {
            // No encryption - use zero-copy.
            final FileRegion region =
                new DefaultFileRegion(raf.getChannel(), 0, fileLength);
            writeFuture = ch.write(region);
            final String finalPath = path;
            writeFuture.addListener(new ChannelFutureProgressListener() {
                public void operationComplete(ChannelFuture future) {
                    region.releaseExternalResources();
                }
View Full Code Here

  }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
            throws Exception {
        Channel ch = e.getChannel();
        Throwable cause = e.getCause();
        if (cause instanceof TooLongFrameException) {
            sendError(ctx, BAD_REQUEST);
            return;
        }

        cause.printStackTrace();
        if (ch.isConnected()) {
            sendError(ctx, INTERNAL_SERVER_ERROR);
        }
    }
View Full Code Here

      // Here's the REALLY important business service at the end of the pipeline
      long newTime = (date.getTime() + random.nextInt());
      Date newDate = new Date(newTime);
      slog("Hey Guys !  I got a date ! [" + date + "] and I modified it to [" + newDate + "]");
      // Send back the reponse
      Channel channel = e.getChannel();
      ChannelFuture channelFuture = Channels.future(e.getChannel());
      ChannelEvent responseEvent = new DownstreamMessageEvent(channel, channelFuture, newDate, channel.getRemoteAddress());
      ctx.sendDownstream(responseEvent);
      // But still send it upstream because there might be another handler
      super.messageReceived(ctx, e);
    }   
View Full Code Here

   * {@inheritDoc}
   * @see org.jboss.netty.channel.ChannelDownstreamHandler#handleDownstream(org.jboss.netty.channel.ChannelHandlerContext, org.jboss.netty.channel.ChannelEvent)
   */
  @Override
  public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
    final Channel channel = e.getChannel();
    if(!channel.isOpen()) return;
    if(!(e instanceof MessageEvent)) {
            ctx.sendDownstream(e);
            return;
        }
    Object message = ((MessageEvent)e).getMessage();
    if(!(message instanceof JSONObject) && !(message instanceof CharSequence)) {
            ctx.sendDownstream(e);
            return;     
    }
   
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    response.setContent(ChannelBuffers.copiedBuffer("\n" + message.toString() + "\n", CharsetUtil.UTF_8));
    response.setHeader(CONTENT_TYPE, "application/json");
    ChannelFuture cf = Channels.future(channel);
    cf.addListener(new ChannelFutureListener(){
      public void operationComplete(ChannelFuture f) throws Exception {
        channel.close();
      }
    });
    ctx.sendDownstream(new DownstreamMessageEvent(channel, cf, response, channel.getRemoteAddress()));
  }
View Full Code Here

//        }
//      });
      // if a wait option was selected and the connect did not fail,
      // the Date can now be sent.
      clog("Connected. Sending Date");
      Channel channel = cf.getChannel();
      channel.write(new Date());
    } catch (Exception e) {
      e.printStackTrace(System.err);
    }
  }
View Full Code Here

      ChannelFuture cf = bootstrap.connect(addressToConnectTo);
      clog("Waiting for Channel Connect...");
      cf.awaitUninterruptibly();
      Date dt = new Date();
      clog("Connected. Sending Date [" + dt + "]");
      Channel channel = cf.getChannel();
      channel.write(dt);
    } catch (Exception e) {
      e.printStackTrace(System.err);
    }
  }
View Full Code Here

            setupUDPCommunication();
        }

        if (!configuration.isLazyChannelCreation()) {
            // ensure the connection can be established when we start up
            Channel channel = pool.borrowObject();
            pool.returnObject(channel);
        }
    }
View Full Code Here

TOP

Related Classes of org.jboss.netty.channel.Channel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.