Package org.vertx.java.core.buffer

Examples of org.vertx.java.core.buffer.Buffer


        JsonObject jsonObject;
        byte[] data;

        // Parse the byte[] message body
        try {
            Buffer body = message.body();

            // First four bytes indicate the json string length
            int len = body.getInt(0);

            // Decode json
            int from = 4;
            byte[] jsonBytes = body.getBytes(from, from + len);
            jsonObject = new JsonObject(decode(jsonBytes));

            // Remaining bytes are the chunk to be written
            from += len;
            data = body.getBytes(from, body.length());

        } catch (RuntimeException e) {
            sendError(message, "error parsing byte[] message.  see the documentation for the correct format", e);
            return;
        }
View Full Code Here


                public void handle(Message<JsonObject> sessionMsg) {
                  final String sessionId = sessionMsg.body.getString("id");
                  sessions.put(socket.writeHandlerID, sessionId);
                  vertx.eventBus().registerHandler("session-"+sessionId, new Handler<Message<JsonObject>>(){
                    public void handle(Message<JsonObject> dataMessage) {
                      socket.writeBuffer(new Buffer(dataMessage.body.toString()));
                    }
                  });
                  vertx.eventBus().send(RabbitService.CREATE_AND_SUBSCRIBE, sessionMsg.body, new Handler<Message<JsonObject>>(){
                    public void handle(Message<JsonObject> msg) {
                      JsonObject connResponse =
                          new JsonObject().
                            putString("type", "session-ok").
                            putString("id", sessionId);
                      socket.writeBuffer(new Buffer(connResponse.toString()));
                    }
                  });
                }
              });
        }
View Full Code Here

        JsonObject jsonObject;
        byte[] data;

        // Parse the byte[] message body
        try {
            Buffer body = message.body();
            if (body.length() == 0) {
                sendError(message, "message body is empty");
                return;
            }

            // First four bytes indicate the json string length
            int len = body.getInt(0);

            // Decode json
            int from = 4;
            byte[] jsonBytes = body.getBytes(from, from + len);
            jsonObject = new JsonObject(decode(jsonBytes));

            // Remaining bytes are the chunk to be written
            from += len;
            data = body.getBytes(from, body.length());

        } catch (RuntimeException e) {
            sendError(message, "error parsing buffer message.  see the documentation for the correct format", e);
            return;
        }
View Full Code Here

  }

  @Test
  public void decodePayload(){
    String data = "5:::{\"name\":\"hello\",\"args\":[\"whiteship\",{\"first name\":\"keesun\"}]}";
    Buffer buffer = new Buffer();
    buffer.appendString("\ufffd");
    buffer.appendString("\ufffd");
    buffer.appendInt(data.length());
    buffer.appendString(data);

    Parser parser = new Parser();

    List<JsonObject> datas =  parser.decodePayload(new Buffer(data));
    assertThat(datas.size(), is(1));
  }
View Full Code Here

    }

    if (!request.method().toUpperCase().equals("POST")) {
      super.handleRequest();
    } else {
      final Buffer buffer = new Buffer(0);
      final HttpServerResponse res = request.response();
      String origin = request.headers().get("ORIGIN");
      MultiMap resHeaders = res.headers();
      resHeaders.add("Content-Length", String.valueOf(1));
      resHeaders.add("Content-Type", "text/plain; charset=UTF-8");

      if (origin != null) {
        // https://developer.mozilla.org/En/HTTP_Access_Control
        resHeaders.add("Access-Control-Allow-Origin", origin);
        resHeaders.add("Access-Control-Allow-Credentials", "true");
      }

      request.dataHandler(new Handler<Buffer>() {
        public void handle(Buffer data) {
          buffer.appendBuffer(data);
          if (buffer.length() >= manager.getSettings().getDestryBufferSize()) {
            resetBuffer(buffer);
            request.response().end();
            request.response().close();
          }
        }
View Full Code Here

    }
    d = d.replaceFirst("d=\"", "");
    d = d.substring(0, d.length() - 1);
    d = d.replace("\\\\", "\\");
    d = d.replace("\\\"", "\"");
    return new Buffer(d);
  }
View Full Code Here

      onMessage(message);
    }
  }

  private void resetBuffer(Buffer buffer) {
    buffer = new Buffer(0);
  }
View Full Code Here

                        HelloMessage handshakeMessage = fromJson(buffer.toString(), HelloMessageImpl.class);
                        if (!writeHandlerMap.containsKey(handshakeMessage.getUAID())) {
                            handshakeMessage = new HelloMessageImpl(UUIDUtil.newUAID());
                        }
                        final HelloResponse helloResponse = simplePushServer.handleHandshake(handshakeMessage);
                        sock.write(new Buffer(toJson(helloResponse)));
                        uaid = helloResponse.getUAID();
                        writeHandlerMap.put(uaid.toString(), sock.writeHandlerID());
                        lastAccessedMap.put(uaid.toString(), System.currentTimeMillis());
                        logger.info("UserAgent [" + uaid + "] handshake done");
                        break;
                    case REGISTER:
                        if (checkHandshakeCompleted(uaid)) {
                            final RegisterResponse response = simplePushServer.handleRegister(fromJson(buffer.toString(), RegisterMessageImpl.class), uaid);
                            sock.write(new Buffer(toJson(response)));
                            logger.info("UserAgent [" + uaid + "] Registered[" + response + "]");
                        }
                        break;
                    case UNREGISTER:
                        if (checkHandshakeCompleted(uaid)) {
                            final UnregisterMessage unregister = fromJson(buffer.toString(), UnregisterMessageImpl.class);
                            final UnregisterResponse response = simplePushServer.handleUnregister(unregister, uaid);
                            sock.write(new Buffer(toJson(response)));
                            logger.info("UserAgent [" + uaid + "] Unregistered[" + response.getChannelId() + "]");
                        }
                        break;
                    case ACK:
                        if (checkHandshakeCompleted(uaid)) {
                            final AckMessage ack = fromJson(buffer.toString(), AckMessageImpl.class);
                            simplePushServer.handleAcknowledgement(ack, uaid);
                            processUnacked(uaid);
                        }
                        break;
                    case PING:
                        sock.write(new Buffer(PingMessageImpl.JSON));
                        break;
                    default:
                        break;
                }
                updateAccessedTime(uaid);
View Full Code Here

                    if (unacked.isEmpty()) {
                        logger.info("Nothing to ack. Stopping periodic task");
                        vertx.cancelTimer(timerID);
                    } else {
                        logger.info("Resending " + unacked);
                        final Buffer buf = new Buffer(toJson(new NotificationMessageImpl(unacked)));
                        vertx.eventBus().send(writeHandlerMap.get(uaid.toString()), buf);
                    }
                }
            });
        }
View Full Code Here

                    final String endpointToken = request.params().get("endpoint");
                    final String payload = buffer.toString();
                    logger.info("Notification endpointToken  [" + endpointToken + "] " + payload);
                    final Notification notification = simplePushServer.handleNotification(endpointToken, payload);
                    final NotificationMessage notificationMessage = new NotificationMessageImpl(notification.ack());
                    vertx.eventBus().send(writeHandlerMap.get(notification.uaid()), new Buffer(toJson(notificationMessage)));
                    request.response().setStatusCode(200);
                    request.response().end();
                } catch (final Exception e) {
                    logger.error(e);
                    request.response().setStatusCode(400);
View Full Code Here

TOP

Related Classes of org.vertx.java.core.buffer.Buffer

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.