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);
         }
       }
     }
   }
 }
Beispiel #2
0
  private void testWSWriteStream(WebsocketVersion version) throws Exception {

    String path = "/some/path";
    String query = "foo=bar&wibble=eek";
    String uri = path + "?" + query;

    server =
        vertx
            .createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT))
            .websocketHandler(
                ws -> {
                  assertEquals(uri, ws.uri());
                  assertEquals(path, ws.path());
                  assertEquals(query, ws.query());
                  assertEquals("Upgrade", ws.headers().get("Connection"));
                  ws.handler(data -> ws.write(data));
                });

    server.listen(
        ar -> {
          assertTrue(ar.succeeded());
          int bsize = 100;
          int sends = 10;

          client.connectWebsocket(
              HttpTestBase.DEFAULT_HTTP_PORT,
              HttpTestBase.DEFAULT_HTTP_HOST,
              path + "?" + query,
              null,
              version,
              ws -> {
                final Buffer received = Buffer.buffer();
                ws.handler(
                    data -> {
                      received.appendBuffer(data);
                      if (received.length() == bsize * sends) {
                        ws.close();
                        testComplete();
                      }
                    });
                final Buffer sent = Buffer.buffer();
                for (int i = 0; i < sends; i++) {
                  Buffer buff = Buffer.buffer(TestUtils.randomByteArray(bsize));
                  ws.write(buff);
                  sent.appendBuffer(buff);
                }
              });
        });
    await();
  }
  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;
  }
  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 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;
  }
 @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 #7
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);
   }
 }
 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 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 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 #11
0
 private void testValidSubProtocol(WebsocketVersion version) throws Exception {
   String path = "/some/path";
   String subProtocol = "myprotocol";
   Buffer buff = Buffer.buffer("AAA");
   server =
       vertx
           .createHttpServer(
               new HttpServerOptions()
                   .setPort(HttpTestBase.DEFAULT_HTTP_PORT)
                   .setWebsocketSubProtocol(subProtocol))
           .websocketHandler(
               ws -> {
                 assertEquals(path, ws.path());
                 ws.writeFrame(WebSocketFrame.binaryFrame(buff, true));
               });
   server.listen(
       ar -> {
         assertTrue(ar.succeeded());
         client.connectWebsocket(
             HttpTestBase.DEFAULT_HTTP_PORT,
             HttpTestBase.DEFAULT_HTTP_HOST,
             path,
             null,
             version,
             subProtocol,
             ws -> {
               final Buffer received = Buffer.buffer();
               ws.handler(
                   data -> {
                     received.appendBuffer(data);
                     if (received.length() == buff.length()) {
                       assertEquals(buff, received);
                       ws.close();
                       testComplete();
                     }
                   });
             });
       });
   await();
 }
Beispiel #12
0
 private void testTLS(
     KS clientCert,
     TS clientTrust,
     KS serverCert,
     TS serverTrust,
     boolean requireClientAuth,
     boolean serverUsesCrl,
     boolean clientTrustAll,
     boolean clientUsesCrl,
     boolean shouldPass,
     String... enabledCipherSuites)
     throws Exception {
   HttpClientOptions options = new HttpClientOptions();
   options.setSsl(true);
   if (clientTrustAll) {
     options.setTrustAll(true);
   }
   if (clientUsesCrl) {
     options.addCrlPath(findFileOnClasspath("tls/ca/crl.pem"));
   }
   options.setTrustStoreOptions(getClientTrustOptions(clientTrust));
   options.setKeyStoreOptions(getClientCertOptions(clientCert));
   for (String suite : enabledCipherSuites) {
     options.addEnabledCipherSuite(suite);
   }
   client = vertx.createHttpClient(options);
   HttpServerOptions serverOptions = new HttpServerOptions();
   serverOptions.setSsl(true);
   serverOptions.setTrustStoreOptions(getServerTrustOptions(serverTrust));
   serverOptions.setKeyStoreOptions(getServerCertOptions(serverCert));
   if (requireClientAuth) {
     serverOptions.setClientAuthRequired(true);
   }
   if (serverUsesCrl) {
     serverOptions.addCrlPath(findFileOnClasspath("tls/ca/crl.pem"));
   }
   for (String suite : enabledCipherSuites) {
     serverOptions.addEnabledCipherSuite(suite);
   }
   server = vertx.createHttpServer(serverOptions.setPort(4043));
   server.websocketHandler(
       ws -> {
         ws.handler(ws::write);
       });
   server.listen(
       ar -> {
         assertTrue(ar.succeeded());
         client
             .websocket(4043, HttpTestBase.DEFAULT_HTTP_HOST, "/")
             .exceptionHandler(
                 t -> {
                   if (shouldPass) {
                     t.printStackTrace();
                     fail("Should not throw exception");
                   } else {
                     testComplete();
                   }
                 })
             .handler(
                 ws -> {
                   int size = 100;
                   Buffer received = Buffer.buffer();
                   ws.handler(
                       data -> {
                         received.appendBuffer(data);
                         if (received.length() == size) {
                           ws.close();
                           testComplete();
                         }
                       });
                   Buffer buff = Buffer.buffer(TestUtils.randomByteArray(size));
                   ws.writeFrame(WebSocketFrame.binaryFrame(buff, true));
                 });
       });
   await();
 }