@Override
 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
   AbstractEvent event = (AbstractEvent) msg;
   EventOperator.instance().onEvent(event, false);
   logger.debug("Event Fired: " + event.toString());
   ReferenceCountUtil.release(msg);
 }
 @Override
 public void updateResponse(HttpServerResponse<O> response, Throwable error) {
   response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
   response.getHeaders().set(HttpHeaders.Names.CONTENT_TYPE, "text/html");
   ByteBuf buffer =
       response.getChannelHandlerContext().alloc().buffer(1024); // 1KB initial length.
   PrintStream printStream = null;
   try {
     printStream = new PrintStream(new ByteBufOutputStream(buffer));
     error.printStackTrace(printStream);
     String errorPage =
         ERROR_HTML_TEMPLATE.replace(
             STACKTRACE_TEMPLATE_VARIABLE, buffer.toString(Charset.defaultCharset()));
     response.writeString(errorPage);
   } finally {
     ReferenceCountUtil.release(buffer);
     if (null != printStream) {
       try {
         printStream.flush();
         printStream.close();
       } catch (Exception e) {
         logger.error(
             "Error closing stream for generating error response stacktrace. This is harmless.",
             e);
       }
     }
   }
 }
    @Override
    public void next(Object value) {
      if (value == null) {
        fail(new NullPointerException("value is null"));
        return;
      }
      if (done) {
        Exceptions.onNextDropped(value);
        return;
      }
      if (caughtUp && actual != null) {
        try {
          actual.onNext(value);
        } finally {
          ch.read();
          ReferenceCountUtil.release(value);
        }

      } else {
        Queue<Object> q = queue;
        if (q == null) {
          q = QueueSupplier.unbounded().get();
          queue = q;
        }
        q.offer(value);
        if (drain()) {
          caughtUp = true;
        }
      }
    }
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof Http2Settings) {
      // Expected
    } else if (msg instanceof FullHttpResponse) {
      FullHttpResponse response = (FullHttpResponse) msg;

      final Invocation invocation = waitsHolder.poll(response);

      if (invocation != null) {
        final ServiceInvocationContext iCtx = invocation.invocationContext();
        final SerializationFormat serializationFormat = iCtx.scheme().serializationFormat();
        try {
          final Promise<FullHttpResponse> resultPromise = invocation.resultPromise();
          if (HttpStatusClass.SUCCESS == response.status().codeClass()
              // No serialization indicates a raw HTTP protocol which should
              // have error responses returned.
              || serializationFormat == SerializationFormat.NONE) {
            iCtx.resolvePromise(resultPromise, response.retain());
          } else {
            iCtx.rejectPromise(
                resultPromise,
                new InvalidResponseException("HTTP Response code: " + response.status()));
          }
        } finally {
          ReferenceCountUtil.release(msg);
        }
      } else {
        // if invocation not found, we just bypass message to next
        ctx.fireChannelRead(msg);
      }

      if (!isMultiplex
          && HttpHeaderValues.CLOSE.contentEqualsIgnoreCase(
              response.headers().get(HttpHeaderNames.CONNECTION))) {
        ctx.close();
      }
    } else {
      try {
        throw new IllegalStateException("unexpected message type: " + msg);
      } finally {
        ReferenceCountUtil.release(msg);
      }
    }
  }
 /**
  * Verifies that no content has been sent as part of the response or readable bytes is equivalent
  * to 0
  *
  * @param contents the content of the response.
  */
 private void assertNoContent(Queue<HttpObject> contents) {
   boolean endMarkerFound = false;
   for (HttpObject object : contents) {
     assertFalse(
         "There should have been no more data after the end marker was found", endMarkerFound);
     HttpContent content = (HttpContent) object;
     assertEquals("No content expected ", 0, content.content().readableBytes());
     endMarkerFound = object instanceof LastHttpContent;
     ReferenceCountUtil.release(content);
   }
   assertTrue("There should have been an end marker", endMarkerFound);
 }
 @Override
 public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
   ByteBuf in = (ByteBuf) msg;
   try {
     while (in.isReadable()) { // (1)
       System.out.print((char) in.readByte());
       System.out.flush();
     }
   } finally {
     ReferenceCountUtil.release(msg); // (2)
   }
 }
 /**
  * Discards all the content in {@code contents}.
  *
  * @param contents the content to discard.
  * @param expectedDiscardCount the number of {@link HttpObject}s that are expected to discarded.
  */
 private void discardContent(Queue<HttpObject> contents, int expectedDiscardCount) {
   assertEquals(
       "Objects that will be discarded differ from expected",
       expectedDiscardCount,
       contents.size());
   boolean endMarkerFound = false;
   for (HttpObject object : contents) {
     assertFalse(
         "There should have been no more data after the end marker was found", endMarkerFound);
     endMarkerFound = object instanceof LastHttpContent;
     ReferenceCountUtil.release(object);
   }
   assertTrue("There should have been an end marker", endMarkerFound);
 }
 @Override
 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
   ByteBuf in = (ByteBuf) msg;
   try {
     // System.out.println(in.toString());
     while (in.isReadable()) {
       System.out.print((char) in.readByte());
       System.out.flush();
     }
   } finally {
     // same as in.release();
     ReferenceCountUtil.release(msg);
   }
 }
  @Override
  public ByteBuf serializeResponseAsBinary(
      final ResponseMessage responseMessage, final ByteBufAllocator allocator)
      throws SerializationException {
    ByteBuf encodedMessage = null;
    try {
      final Kryo kryo = kryoThreadLocal.get();
      try (final OutputStream baos = new ByteArrayOutputStream()) {
        final Output output = new Output(baos);

        // request id - if present
        kryo.writeObjectOrNull(
            output,
            responseMessage.getRequestId() != null ? responseMessage.getRequestId() : null,
            UUID.class);

        // status
        output.writeShort(responseMessage.getStatus().getCode().getValue());
        output.writeString(responseMessage.getStatus().getMessage());
        kryo.writeClassAndObject(output, responseMessage.getStatus().getAttributes());

        // result
        kryo.writeClassAndObject(
            output,
            serializeToString
                ? serializeResultToString(responseMessage)
                : responseMessage.getResult().getData());
        kryo.writeClassAndObject(output, responseMessage.getResult().getMeta());

        final long size = output.total();
        if (size > Integer.MAX_VALUE)
          throw new SerializationException(
              String.format("Message size of %s exceeds allocatable space", size));

        encodedMessage = allocator.buffer((int) output.total());
        encodedMessage.writeBytes(output.toBytes());
      }

      return encodedMessage;
    } catch (Exception ex) {
      if (encodedMessage != null) ReferenceCountUtil.release(encodedMessage);

      logger.warn(
          "Response [{}] could not be serialized by {}.",
          responseMessage.toString(),
          GryoMessageSerializerV1d0.class.getName());
      throw new SerializationException(ex);
    }
  }
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {
      ByteBuf recvBuf = Unpooled.copiedBuffer((ByteBuf) msg);
      byte[] buf = new byte[recvBuf.capacity()];
      recvBuf.readBytes(buf);

      String request = new String(buf, CharsetUtil.UTF_8);
      System.out.println("받는 Data : " + request);

      broadCast(ctx, request);
    } finally {
      ReferenceCountUtil.release(msg);
    }
  }
Beispiel #11
0
  private void testCompress0(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper, ByteBuf data)
      throws Exception {
    EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(encoderWrapper));

    chEncoder.writeOutbound(data.copy());
    chEncoder.flush();

    EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(decoderWrapper));
    for (; ; ) {
      ByteBuf deflatedData = chEncoder.readOutbound();
      if (deflatedData == null) {
        break;
      }
      chDecoderZlib.writeInbound(deflatedData);
    }

    byte[] decompressed = new byte[data.readableBytes()];
    int offset = 0;
    for (; ; ) {
      ByteBuf buf = chDecoderZlib.readInbound();
      if (buf == null) {
        break;
      }
      int length = buf.readableBytes();
      buf.readBytes(decompressed, offset, length);
      offset += length;
      buf.release();
      if (offset == decompressed.length) {
        break;
      }
    }
    assertEquals(data, Unpooled.wrappedBuffer(decompressed));
    assertNull(chDecoderZlib.readInbound());

    // Closing an encoder channel will generate a footer.
    assertTrue(chEncoder.finish());
    for (; ; ) {
      Object msg = chEncoder.readOutbound();
      if (msg == null) {
        break;
      }
      ReferenceCountUtil.release(msg);
    }
    // But, the footer will be decoded into nothing. It's only for validation.
    assertFalse(chDecoderZlib.finish());

    data.release();
  }
 /**
  * Combines all the parts in {@code contents} into one {@link ByteBuffer}.
  *
  * @param contents the content of the response.
  * @param expectedContentLength the length of the contents in bytes.
  * @return a {@link ByteBuffer} that contains all the data in {@code contents}.
  */
 private ByteBuffer getContent(Queue<HttpObject> contents, long expectedContentLength) {
   ByteBuffer buffer = ByteBuffer.allocate((int) expectedContentLength);
   boolean endMarkerFound = false;
   for (HttpObject object : contents) {
     assertFalse(
         "There should have been no more data after the end marker was found", endMarkerFound);
     HttpContent content = (HttpContent) object;
     buffer.put(content.content().nioBuffer());
     endMarkerFound = object instanceof LastHttpContent;
     ReferenceCountUtil.release(content);
   }
   assertEquals("Content length did not match expected", expectedContentLength, buffer.position());
   assertTrue("End marker was not found", endMarkerFound);
   buffer.flip();
   return buffer;
 }
Beispiel #13
0
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
    ByteBuf in = (ByteBuf) msg;
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      while (in.isReadable()) { // (1)
        byte readByte = in.readByte();
        baos.write(readByte);
        // System.out.flush();

        ctx.flush();
      }
      System.out.println("Msg Read: " + baos.toString());
    } finally {
      // System.out.println("finish!");
      ReferenceCountUtil.release(msg); // (2)
    }
  }
 @Override
 protected void decode(
     final ChannelHandlerContext channelHandlerContext,
     final WebSocketFrame webSocketFrame,
     final List<Object> objects)
     throws Exception {
   try {
     if (webSocketFrame instanceof BinaryWebSocketFrame) {
       final BinaryWebSocketFrame tf = (BinaryWebSocketFrame) webSocketFrame;
       objects.add(serializer.deserializeResponse(tf.content()));
     } else {
       final TextWebSocketFrame tf = (TextWebSocketFrame) webSocketFrame;
       final MessageTextSerializer textSerializer = (MessageTextSerializer) serializer;
       objects.add(textSerializer.deserializeResponse(tf.text()));
     }
   } finally {
     ReferenceCountUtil.release(webSocketFrame);
   }
 }
 @Override
 @SuppressWarnings("unchecked")
 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
   if (matcher.match(msg)) {
     final I typedMsg = (I) msg;
     try {
       if (quotaMeter.getOneMinuteRate() > USER_QUOTA_LIMIT) {
         sendErrorResponseIfTicked(typedMsg.id);
         return;
       }
       quotaMeter.mark();
       messageReceived(ctx, handlerState, typedMsg);
     } catch (Exception e) {
       handleGeneralException(ctx, e);
     } finally {
       ThreadContext.clearMap();
       ReferenceCountUtil.release(msg);
     }
   }
 }
    @Override
    public void channelRead(@NotNull ChannelHandlerContext ctx, @NotNull Object msg) {
      try {
        bytesReceived += ((ByteBuf) msg).readableBytes();

        if (i++ % 10000 == 0) System.out.print(".");
        if (TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime) >= 10) {
          long time = System.nanoTime() - startTime;
          System.out.printf("\nThroughput was %.1f MB/s%n", 1e3 * bytesReceived / time);
          return;
        }

      } finally {
        ReferenceCountUtil.release(msg); // (2)
      }

      final ByteBuf outMsg = ctx.alloc().buffer(bufferSize); // (2)
      outMsg.writeBytes(payload);

      ctx.writeAndFlush(outMsg); // (3)
    }
  @Override
  public ByteBuf serializeRequestAsBinary(
      final RequestMessage requestMessage, final ByteBufAllocator allocator)
      throws SerializationException {
    ByteBuf encodedMessage = null;
    try {
      final Kryo kryo = kryoThreadLocal.get();
      try (final OutputStream baos = new ByteArrayOutputStream()) {
        final Output output = new Output(baos);
        final String mimeType = serializeToString ? MIME_TYPE_STRINGD : MIME_TYPE;
        output.writeByte(mimeType.length());
        output.write(mimeType.getBytes(UTF8));

        kryo.writeObject(output, requestMessage.getRequestId());
        output.writeString(requestMessage.getProcessor());
        output.writeString(requestMessage.getOp());
        kryo.writeObject(output, requestMessage.getArgs());

        final long size = output.total();
        if (size > Integer.MAX_VALUE)
          throw new SerializationException(
              String.format("Message size of %s exceeds allocatable space", size));

        encodedMessage = allocator.buffer((int) size);
        encodedMessage.writeBytes(output.toBytes());
      }

      return encodedMessage;
    } catch (Exception ex) {
      if (encodedMessage != null) ReferenceCountUtil.release(encodedMessage);

      logger.warn(
          "Request [{}] could not be serialized by {}.",
          requestMessage.toString(),
          GryoMessageSerializerV1d0.class.getName());
      throw new SerializationException(ex);
    }
  }
    boolean drain() {
      if (wip++ != 0) {
        return false;
      }

      int missed = 1;

      for (; ; ) {
        final Queue<Object> q = queue;
        final Subscriber<? super Object> a = actual;

        if (a == null) {
          return false;
        }

        long r = requested;
        long e = 0L;

        while (e != r) {
          if (isCancelled()) {
            return false;
          }

          boolean d = done;
          Object v = q != null ? q.poll() : null;
          boolean empty = v == null;

          if (d && empty) {
            cancelResource();
            if (q != null) {
              q.clear();
            }
            Throwable ex = error;
            if (ex != null) {
              a.onError(ex);
            } else {
              a.onComplete();
            }
            return false;
          }

          if (empty) {
            ch.read();
            break;
          }

          try {
            a.onNext(v);
          } finally {
            ReferenceCountUtil.release(v);
            ch.read();
          }

          e++;
        }

        if (e == r) {
          if (isCancelled()) {
            return false;
          }

          if (done && (q == null || q.isEmpty())) {
            cancelResource();
            if (q != null) {
              q.clear();
            }
            Throwable ex = error;
            if (ex != null) {
              a.onError(ex);
            } else {
              a.onComplete();
            }
            return false;
          }
        }

        if (e != 0L) {
          if (r != Long.MAX_VALUE) {
            if ((requested -= e) > 0L) {
              ch.read();
            }
          }
        }

        missed = (wip = wip - missed);
        if (missed == 0) {
          if (r == Long.MAX_VALUE) {
            ch.config().setAutoRead(true);
            ch.read();
            return true;
          }
          return false;
        }
      }
    }
Beispiel #19
0
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

      messageReceiveCount++;
      if (msg instanceof HttpResponse) {
        try {
          HttpResponse response = (HttpResponse) msg;

          StringBuilder sb = new StringBuilder();
          if (LOG.isDebugEnabled()) {
            sb.append("STATUS: ")
                .append(response.getStatus())
                .append(", VERSION: ")
                .append(response.getProtocolVersion())
                .append(", HEADER: ");
          }
          if (!response.headers().names().isEmpty()) {
            for (String name : response.headers().names()) {
              for (String value : response.headers().getAll(name)) {
                if (LOG.isDebugEnabled()) {
                  sb.append(name).append(" = ").append(value);
                }
                if (this.length == -1 && name.equals("Content-Length")) {
                  this.length = Long.parseLong(value);
                }
              }
            }
          }
          if (LOG.isDebugEnabled()) {
            LOG.debug(sb.toString());
          }

          if (response.getStatus().code() == HttpResponseStatus.NO_CONTENT.code()) {
            LOG.warn("There are no data corresponding to the request");
            length = 0;
            return;
          } else if (response.getStatus().code() != HttpResponseStatus.OK.code()) {
            LOG.error(response.getStatus().reasonPhrase());
            state = TajoProtos.FetcherState.FETCH_FAILED;
            return;
          }
        } catch (Exception e) {
          LOG.error(e.getMessage(), e);
        } finally {
          ReferenceCountUtil.release(msg);
        }
      }

      if (msg instanceof HttpContent) {
        try {
          HttpContent httpContent = (HttpContent) msg;
          ByteBuf content = httpContent.content();
          if (content.isReadable()) {
            content.readBytes(fc, content.readableBytes());
          }

          if (msg instanceof LastHttpContent) {
            if (raf != null) {
              fileLen = file.length();
            }

            finishTime = System.currentTimeMillis();
            if (state != TajoProtos.FetcherState.FETCH_FAILED) {
              state = TajoProtos.FetcherState.FETCH_FINISHED;
            }

            IOUtils.cleanup(LOG, fc, raf);
          }
        } catch (Exception e) {
          LOG.error(e.getMessage(), e);
        } finally {
          ReferenceCountUtil.release(msg);
        }
      }
    }
 @Override
 public void channelRead(final ChannelHandlerContext ctx, final Object inputObj) throws Exception {
   try {
     final ByteBuf input = (ByteBuf) inputObj;
     if (!input.isReadable()) {
       return;
     }
     final Channel channel = ctx.channel();
     receivedData.writeBytes(input);
     ProtocolVersion handshakeversion = ProtocolVersion.NOT_SET;
     receivedData.readerIndex(0);
     int firstbyte = receivedData.readUnsignedByte();
     switch (firstbyte) {
       case 0xFE:
         { // old ping
           try {
             if (receivedData.readableBytes() == 0) { // really old protocol probably
               scheduleTask(ctx, new Ping11ResponseTask(channel), 1000, TimeUnit.MILLISECONDS);
             } else if (receivedData.readUnsignedByte() == 1) {
               if (receivedData.readableBytes() == 0) {
                 // 1.5.2 probably
                 scheduleTask(
                     ctx, new Ping152ResponseTask(this, channel), 500, TimeUnit.MILLISECONDS);
               } else if ((receivedData.readUnsignedByte() == 0xFA)
                   && "MC|PingHost"
                       .equals(
                           new String(
                               Utils.toArray(
                                   receivedData.readBytes(receivedData.readUnsignedShort() * 2)),
                               StandardCharsets.UTF_16BE))) { // 1.6.*
                 receivedData.readUnsignedShort();
                 handshakeversion = ProtocolVersion.fromId(receivedData.readUnsignedByte());
               }
             }
           } catch (IndexOutOfBoundsException ex) {
           }
           break;
         }
       case 0x02:
         { // 1.6 or 1.5.2 handshake
           try {
             handshakeversion = ProtocolVersion.fromId(receivedData.readUnsignedByte());
           } catch (IndexOutOfBoundsException ex) {
           }
           break;
         }
       default:
         { // 1.7 or 1.8 handshake
           receivedData.readerIndex(0);
           ByteBuf data = getVarIntPrefixedData(receivedData);
           if (data != null) {
             handshakeversion = readNPHandshake(data);
           }
           break;
         }
     }
     // if we detected the protocol than we save it and process data
     if (handshakeversion != ProtocolVersion.NOT_SET) {
       setProtocol(channel, receivedData, handshakeversion);
     }
   } catch (Throwable t) {
     ctx.channel().close();
   } finally {
     ReferenceCountUtil.release(inputObj);
   }
 }