@Override
 public void encodeToWire(Buffer buffer, String entries) {
   byte[] encoded = entries.getBytes(CharsetUtil.UTF_8);
   buffer.appendInt(encoded.length);
   Buffer buff = Buffer.buffer(encoded);
   buffer.appendBuffer(buff);
 }
Beispiel #2
0
 @Override
 protected void readBody(int pos, Buffer readBuff) {
   boolean isNull = readBuff.getByte(pos) == (byte) 0;
   if (!isNull) {
     body = readBuff.getInt(++pos);
   }
 }
 private Buffer getAnalogValueCallbackThresholdDefault() {
   Buffer buffer = Buffer.buffer();
   buffer.appendBytes(Utils.getCharRandomValue(1));
   buffer.appendBytes(Utils.get2ByteURandomValue(1));
   buffer.appendBytes(Utils.get2ByteURandomValue(1));
   return buffer;
 }
 void handleChunk(Buffer data) {
   synchronized (conn) {
     if (paused) {
       if (pausedChunk == null) {
         pausedChunk = data.copy();
       } else {
         pausedChunk.appendBuffer(data);
       }
     } else {
       request.dataReceived();
       if (pausedChunk != null) {
         data = pausedChunk.appendBuffer(data);
         pausedChunk = null;
       }
       bytesRead += data.length();
       if (dataHandler != null) {
         try {
           dataHandler.handle(data);
         } catch (Throwable t) {
           handleException(t);
         }
       }
     }
   }
 }
 @Override
 public String decodeFromWire(int pos, Buffer buffer) {
   int length = buffer.getInt(pos);
   pos += 4;
   byte[] bytes = buffer.getBytes(pos, pos + length);
   return new String(bytes, CharsetUtil.UTF_8);
 }
Beispiel #6
0
 private void testWriteMessage(int size, WebsocketVersion version) {
   String path = "/some/path";
   byte[] expected = TestUtils.randomByteArray(size);
   server =
       vertx
           .createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT))
           .websocketHandler(
               ws -> {
                 ws.writeMessage(Buffer.buffer(expected));
                 ws.close();
               });
   server.listen(
       ar -> {
         assertTrue(ar.succeeded());
         client.connectWebsocket(
             HttpTestBase.DEFAULT_HTTP_PORT,
             HttpTestBase.DEFAULT_HTTP_HOST,
             path,
             null,
             version,
             ws -> {
               Buffer actual = Buffer.buffer();
               ws.handler(actual::appendBuffer);
               ws.closeHandler(
                   v -> {
                     assertArrayEquals(expected, actual.getBytes());
                     testComplete();
                   });
             });
       });
   await();
 }
Beispiel #7
0
 public Buffer encodeToWire() {
   int length = 1024; // TODO make this configurable
   Buffer buffer = Buffer.buffer(length);
   buffer.appendInt(0);
   buffer.appendByte(WIRE_PROTOCOL_VERSION);
   byte systemCodecID = messageCodec.systemCodecID();
   buffer.appendByte(systemCodecID);
   if (systemCodecID == -1) {
     // User codec
     writeString(buffer, messageCodec.name());
   }
   buffer.appendByte(send ? (byte) 0 : (byte) 1);
   writeString(buffer, address);
   if (replyAddress != null) {
     writeString(buffer, replyAddress);
   } else {
     buffer.appendInt(0);
   }
   buffer.appendInt(sender.port);
   writeString(buffer, sender.host);
   encodeHeaders(buffer);
   writeBody(buffer);
   buffer.setInt(0, buffer.length() - 4);
   return buffer;
 }
 @Test
 public void testFileSystemReadFile() {
   assertTrue(vertx.fileSystem().existsBlocking("afile.html"));
   assertFalse(vertx.fileSystem().propsBlocking("afile.html").isDirectory());
   Buffer buffer = vertx.fileSystem().readFileBlocking("afile.html");
   assertNotNull(buffer);
   assertTrue(buffer.toString().startsWith("<html><body>afile</body></html>"));
 }
 @Override
 public User decodeFromWire(int pos, Buffer buffer) {
   int length = buffer.getInt(pos);
   pos += 4;
   byte[] encoded = buffer.getBytes(pos, pos + length);
   String str = new String(encoded, CharsetUtil.UTF_8);
   return JsonUtils.decode(str, User.class);
 }
Beispiel #10
0
 @Override
 protected void writeBody(Buffer buff) {
   if (body == null) {
     buff.appendByte((byte) 0);
   } else {
     buff.appendByte((byte) 1);
     buff.appendInt(body);
   }
 }
Beispiel #11
0
 @Override
 public Buffer writeToBuffer() {
   String encoded = encode();
   byte[] bytes = encoded.getBytes();
   Buffer buffer = Buffer.buffer(bytes.length + 4);
   buffer.appendInt(bytes.length);
   buffer.appendBytes(bytes);
   return buffer;
 }
  private void writeToTarget(String sendPacket, String cmd) {
    logger.debug(name_log + "writeToDCU(), Command: " + cmd);

    // Writing Data to The Socket
    Buffer outBuffer = Buffer.buffer();
    outBuffer.setString(0, sendPacket);
    logger.info(name_log + "SEND[" + outBuffer.length() + "]> \n" + outBuffer.toString() + "\n");

    netSocket.write(outBuffer);
  }
Beispiel #13
0
 @Override
 public void end(Buffer chunk) {
   synchronized (getLock()) {
     checkComplete();
     checkResponseHandler();
     if (!chunked && !contentLengthSet()) {
       headers().set(CONTENT_LENGTH, String.valueOf(chunk.length()));
     }
     write(chunk.getByteBuf(), true);
   }
 }
  private Buffer getVoltageCallbackPeriod(Packet packet) {
    logger.debug("function getVoltageCallbackPeriod");
    if (packet.getResponseExpected()) {
      byte length = (byte) 8 + 4;
      byte functionId = FUNCTION_GET_VOLTAGE_CALLBACK_PERIOD;
      byte flags = (byte) 0;
      Buffer header = Utils.createHeader(uidBytes, length, functionId, packet.getOptions(), flags);
      Buffer buffer = Buffer.buffer();
      buffer.appendBuffer(header);
      buffer.appendBuffer(this.voltageCallbackPeriod);
      return buffer;
    }

    return null;
  }
  @Test
  public void writeTwoDifferentMessages() throws Exception {
    byte[] bytes = {0x1, 0x3, 0x5, 0x7};
    String strContent = "foo";

    _wsStream.write(Buffer.buffer(bytes));
    _wsStream.finishCurrentMessage();

    _wsStream.setDataFormat(DataFormat.Text);
    _wsStream.write(Buffer.buffer(strContent.getBytes(StandardCharsets.UTF_8)));
    _wsStream.finishCurrentMessage();

    verify(_socket).writeFrame(binaryFrameEq(true, bytes));
    verify(_socket).writeFrame(textFrameEq(true, strContent));
  }
  private Buffer getAnalogValueCallbackThreshold(Packet packet) {
    logger.debug("function getAnalogValueCallbackThreshold");
    if (packet.getResponseExpected()) {
      byte length = (byte) 8 + 5;
      byte functionId = FUNCTION_GET_ANALOG_VALUE_CALLBACK_THRESHOLD;
      byte flags = (byte) 0;
      Buffer header = Utils.createHeader(uidBytes, length, functionId, packet.getOptions(), flags);
      Buffer buffer = Buffer.buffer();
      buffer.appendBuffer(header);
      buffer.appendBuffer(this.analogValueCallbackThreshold);
      return buffer;
    }

    return null;
  }
 private Buffer setAnalogValueCallbackPeriod(Packet packet) {
   logger.debug("function setAnalogValueCallbackPeriod");
   if (packet.getResponseExpected()) {
     byte length = (byte) 8 + 0;
     byte functionId = FUNCTION_SET_ANALOG_VALUE_CALLBACK_PERIOD;
     byte flags = (byte) 0;
     Buffer header = Utils.createHeader(uidBytes, length, functionId, packet.getOptions(), flags);
     Buffer buffer = Buffer.buffer();
     buffer.appendBuffer(header);
     // TODO response expected bei settern
     return buffer;
   }
   this.analogValueCallbackPeriod = packet.getPayload();
   return null;
 }
 private Buffer setVoltageCallbackThreshold(Packet packet) {
   logger.debug("function setVoltageCallbackThreshold");
   if (packet.getResponseExpected()) {
     byte length = (byte) 8 + 0;
     byte functionId = FUNCTION_SET_VOLTAGE_CALLBACK_THRESHOLD;
     byte flags = (byte) 0;
     Buffer header = Utils.createHeader(uidBytes, length, functionId, packet.getOptions(), flags);
     Buffer buffer = Buffer.buffer();
     buffer.appendBuffer(header);
     // TODO response expected bei settern
     return buffer;
   }
   this.voltageCallbackThreshold = packet.getPayload();
   return null;
 }
  private Buffer getIdentity(Packet packet) {
    logger.debug("function getIdentity");
    if (packet.getResponseExpected()) {
      byte length = (byte) 8 + 25;
      byte functionId = FUNCTION_GET_IDENTITY;
      byte flags = (byte) 0;
      Buffer header = Utils.createHeader(uidBytes, length, functionId, packet.getOptions(), flags);
      Buffer buffer = Buffer.buffer();
      buffer.appendBuffer(header);
      buffer.appendBuffer(Utils.getIdentityPayload(uidString, uidBytes, DEVICE_IDENTIFIER));
      return buffer;
    }

    return null;
  }
  @Override
  public void render(
      RoutingContext context, String templateFileName, Handler<AsyncResult<Buffer>> handler) {
    try {
      Template template = cache.get(templateFileName);
      if (template == null) {
        // real compile
        synchronized (this) {
          loader.setVertx(context.vertx());
          // Compile
          template = config.getTemplate(adjustLocation(templateFileName));
        }
        cache.put(templateFileName, template);
      }

      Map<String, RoutingContext> variables = new HashMap<>(1);
      variables.put("context", context);

      try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        template.process(variables, new OutputStreamWriter(baos));
        handler.handle(Future.succeededFuture(Buffer.buffer(baos.toByteArray())));
      }

    } catch (Exception ex) {
      handler.handle(Future.failedFuture(ex));
    }
  }
  private Buffer getVoltage(Packet packet) {
    logger.debug("function getVoltage");
    if (packet.getResponseExpected()) {
      byte length = (byte) 8 + 2;
      byte functionId = FUNCTION_GET_VOLTAGE;
      byte flags = (byte) 0;
      Buffer header = Utils.createHeader(uidBytes, length, functionId, packet.getOptions(), flags);
      Buffer buffer = Buffer.buffer();
      buffer.appendBuffer(header);
      buffer.appendBytes(Utils.get2ByteURandomValue(1));

      return buffer;
    }

    return null;
  }
Beispiel #22
0
 @Override
 public void end(String chunk, String enc) {
   synchronized (getLock()) {
     Objects.requireNonNull(enc, "no null encoding accepted");
     end(Buffer.buffer(chunk, enc));
   }
 }
Beispiel #23
0
 @Override
 public HttpClientRequestImpl write(String chunk) {
   synchronized (getLock()) {
     checkComplete();
     checkResponseHandler();
     return write(Buffer.buffer(chunk));
   }
 }
  @Test
  public void writeMultipleBytesAndClose() throws Exception {
    byte[] bytes = {0x1, 0x3, 0x5, 0x7};
    _wsStream.write(Buffer.buffer(bytes));
    _wsStream.finishCurrentMessage();

    verify(_socket).writeFrame(binaryFrameEq(true, bytes));
  }
  @Test
  public void writeOneByteAndClose() throws Exception {
    byte b = 0x13; // arbitrary byte
    _wsStream.write(Buffer.buffer(new byte[] {b}));
    _wsStream.finishCurrentMessage(); // should flush and send websocket frame

    verify(_socket).writeFrame(binaryFrameEq(true, new byte[] {b}));
  }
Beispiel #26
0
 synchronized void handleDataReceived(Buffer data) {
   checkContext();
   if (paused) {
     if (pendingData == null) {
       pendingData = data.copy();
     } else {
       pendingData.appendBuffer(data);
     }
     return;
   }
   if (pendingData != null) {
     data = pendingData.appendBuffer(data);
   }
   reportBytesRead(data.length());
   if (dataHandler != null) {
     dataHandler.handle(data);
   }
 }
Beispiel #27
0
 @Override
 public HttpClientRequestImpl write(String chunk, String enc) {
   synchronized (getLock()) {
     Objects.requireNonNull(enc, "no null encoding accepted");
     checkComplete();
     checkResponseHandler();
     return write(Buffer.buffer(chunk, enc));
   }
 }
Beispiel #28
0
 @Override
 public HttpClientRequestImpl write(Buffer chunk) {
   synchronized (getLock()) {
     checkComplete();
     checkResponseHandler();
     ByteBuf buf = chunk.getByteBuf();
     write(buf, false);
     return this;
   }
 }
  @Test
  public void writeUnicodeFrameAndClose() throws Exception {
    String unicodeStr = "\u0628\u00de";

    _wsStream.setDataFormat(DataFormat.Text);
    _wsStream.write(Buffer.buffer(unicodeStr.getBytes(StandardCharsets.UTF_8)));
    _wsStream.finishCurrentMessage();

    verify(_socket).writeFrame(textFrameEq(true, unicodeStr));
  }
Beispiel #30
0
 @Override
 public synchronized NetSocket resume() {
   if (paused) {
     paused = false;
     if (pendingData != null) {
       // Send empty buffer to trigger sending of pending data
       context.runOnContext(v -> handleDataReceived(Buffer.buffer()));
     }
     doResume();
   }
   return this;
 }