public RecordParser initialHandhakeProtocolParser() { NetSocket sock = netSocket; final RecordParser parser = RecordParser.newDelimited("\n", h -> { String cmd = h.toString(); if("START SESSION".equalsIgnoreCase(cmd)) { sock.pause(); start(); logger.info("Bridge Server - start session with " + "tenant: " + getTenant() + ", ip: " + sock.remoteAddress() + ", bridgeUUID: " + getBridgeUUID() ); sock.resume(); } else { String tenant = cmd; String tenantFromCert = new CertInfo(sock).getTenant(); if(tenantFromCert != null) tenant = tenantFromCert; setTenant(tenant); } }); return parser; }
/** * 在解析出错时,通过重新创建parser对象,将整个缓冲区重置 */ protected void reset() { parser = RecordParser.newFixed(TCP_HEADER_LENGTH, this::onParse); status = ParseStatus.TCP_HEADER; parser.handle(Buffer.buffer(0)); }
public HttpPart(Buffer bodyBuffer) { this.buffer = Buffer.buffer(); List<String> headersList = new ArrayList<>(); // We need to extract headers and content from buffer RecordParser parser = RecordParser.newDelimited("\r\n", new Handler<Buffer>() { int pos = 0; boolean startContent = false; @Override public void handle(Buffer frame) { if (frame.length() == 0) { if (pos > 0) { startContent = true; } } else { if (!startContent) { headersList.add(frame.toString().trim()); } else { buffer.appendBuffer(frame); } } pos++; } }); parser.handle(bodyBuffer); this.headers = new CaseInsensitiveHeaders(); for (String header : headersList) { int offset = header.indexOf(":"); this.headers.add(header.substring(0, offset), header.substring(offset + 1).trim()); } this.contentType = HttpUtils.extractContentType(headers); this.contentDisposition = HttpUtils.extractContentDisposition(headers); }
public RecordParser initialHandhakeProtocolParser() { if(!(webSocket instanceof ServerWebSocket)) { throw new IllegalStateException("This must be a server! websocket instance is of type '"+webSocket.getClass().getSimpleName()+"'"); } ServerWebSocket sock = (ServerWebSocket)webSocket; final RecordParser parser = RecordParser.newDelimited("\n", h -> { String cmd = h.toString(); if ("START SESSION".equalsIgnoreCase(cmd)) { sock.pause(); start(); logger.info("Bridge Server - start session with " + "tenant: " + getTenant() + ", ip: " + sock.remoteAddress() + ", bridgeUUID: " + getBridgeUUID() ); sock.resume(); } else { String tenant = cmd; String tenantFromCert = new CertInfo(sock).getTenant(); if (tenantFromCert != null) tenant = tenantFromCert; setTenant(tenant); } }); return parser; }
public GraphiteServer(Configuration configuration) { port = configuration.getGraphitePort(); recordParser = RecordParser.newDelimited("\n", this::handleRecord); }
private RuleBasedPacketParser(JsonObject config, final Handler<Buffer> output) { this.outputHandler = output; final Rule firstRule = Rule.build(config); currentRule = firstRule; innerParser = RecordParser.newFixed(currentRule.length(null), buffer -> { if (buff == null) { buff = buffer; } else { buff.appendBuffer(buffer); } if (currentRule == null) { parsedRecords.add(buff.copy()); parsedBufferLength = parsedBufferLength + buff.length(); if (parsedBufferLength == fullBufferLength) { parsedRecords.forEach( parsedRecord -> outputHandler.handle(parsedRecord)); parsedRecords.clear(); parsedBufferLength = 0; } buff = null; currentRule = firstRule; } final int nextLength = currentRule.length(buffer); if (nextLength == 0) { parsedRecords.add(buff.copy()); parsedBufferLength = parsedBufferLength + buff.length(); if (parsedBufferLength == fullBufferLength) { parsedRecords.forEach( parsedRecord -> outputHandler.handle(parsedRecord)); parsedRecords.clear(); parsedBufferLength = 0; } buff = null; currentRule = firstRule; innerParser.fixedSizeMode(currentRule.length(null)); currentRule = currentRule.nextRule(null); return; } innerParser.fixedSizeMode(nextLength); currentRule = currentRule.nextRule(buffer); }); currentRule = currentRule.nextRule(null); }
@Override public void init(JsonObject configObj, Vertx vertx) { this.configObj = configObj; this.vertx = vertx; rawParser = RecordParser.newDelimited(configObj.getString("delimiter", HTTP_DELIM), null); }
@Override public void start() throws Exception { address = MQTTSession.ADDRESS; JsonObject conf = config(); localBridgePort = conf.getInteger("local_bridge_port", 7007); idleTimeout = conf.getInteger("socket_idle_timeout", 120); ssl_cert_key = conf.getString("ssl_cert_key"); ssl_cert = conf.getString("ssl_cert"); ssl_trust = conf.getString("ssl_trust"); // [TCP -> BUS] listen TCP publish to BUS NetServerOptions opt = new NetServerOptions() .setTcpKeepAlive(true) .setIdleTimeout(idleTimeout) .setPort(localBridgePort) ; if(ssl_cert_key != null && ssl_cert != null && ssl_trust != null) { opt.setSsl(true).setClientAuth(ClientAuth.REQUIRED) .setPemKeyCertOptions(new PemKeyCertOptions() .setKeyPath(ssl_cert_key) .setCertPath(ssl_cert) ) .setPemTrustOptions(new PemTrustOptions() .addCertPath(ssl_trust) ) ; } netServer = vertx.createNetServer(opt); netServer.connectHandler(sock -> { final EventBusNetBridge ebnb = new EventBusNetBridge(sock, vertx.eventBus(), address); sock.closeHandler(aVoid -> { logger.info("Bridge Server - closed connection from client ip: " + sock.remoteAddress()); ebnb.stop(); }); sock.exceptionHandler(throwable -> { logger.error("Bridge Server - Exception: " + throwable.getMessage(), throwable); ebnb.stop(); }); logger.info("Bridge Server - new connection from client ip: " + sock.remoteAddress()); RecordParser parser = ebnb.initialHandhakeProtocolParser(); sock.handler(parser::handle); }).listen(); }
@Override public void start() throws Exception { address = MQTTSession.ADDRESS; JsonObject conf = config(); localBridgePort = conf.getInteger("local_bridge_port", 7007); idleTimeout = conf.getInteger("socket_idle_timeout", 120); ssl_cert_key = conf.getString("ssl_cert_key"); ssl_cert = conf.getString("ssl_cert"); ssl_trust = conf.getString("ssl_trust"); // [WebSocket -> BUS] listen WebSocket publish to BUS HttpServerOptions opt = new HttpServerOptions() .setTcpKeepAlive(true) .setIdleTimeout(idleTimeout) .setPort(localBridgePort) ; if(ssl_cert_key != null && ssl_cert != null && ssl_trust != null) { opt.setSsl(true).setClientAuth(ClientAuth.REQUIRED) .setPemKeyCertOptions(new PemKeyCertOptions() .setKeyPath(ssl_cert_key) .setCertPath(ssl_cert) ) .setPemTrustOptions(new PemTrustOptions() .addCertPath(ssl_trust) ) ; } netServer = vertx.createHttpServer(opt); netServer.websocketHandler(sock -> { final EventBusWebsocketBridge ebnb = new EventBusWebsocketBridge(sock, vertx.eventBus(), address); sock.closeHandler(aVoid -> { logger.info("Bridge Server - closed connection from client ip: " + sock.remoteAddress()); ebnb.stop(); }); sock.exceptionHandler(throwable -> { logger.error("Bridge Server - Exception: " + throwable.getMessage(), throwable); ebnb.stop(); }); logger.info("Bridge Server - new connection from client ip: " + sock.remoteAddress()); RecordParser parser = ebnb.initialHandhakeProtocolParser(); sock.handler(parser::handle); }).listen(); }
public void open(final Handler<AsyncResult<Client>> handler) { netClient = vertx.createNetClient(); netClient.connect(port, host, result -> { if (result.failed()) { handler.handle(Future.failedFuture(result.cause())); } else { netSocket = result.result(); final RecordParser rp = RecordParser.newFixed(4, null); rp.setOutput(new Handler<Buffer>() { boolean s = true; @Override public void handle(Buffer buffer) { //System.out.println("handle:" + buffer.length()); if (s) { int size = Integer.reverseBytes(buffer.getInt(0)); //System.out.println(size); rp.fixedSizeMode(size - 4); } else { Reply reply = new Reply(); reply.read(new BsonDecoder(buffer)); //System.out.println(reply); //System.out.println(reply.getDocuments()); Handler<AsyncResult<Reply>> callback = callbacks.remove(reply.getResponseId()); if (callback != null) { outstanding--; callback.handle(Future.succeededFuture(reply)); } else { System.err.println("no callback:" + callback + " for " + reply.getResponseId()); } rp.fixedSizeMode(4); } s = !s; } }); netSocket.closeHandler($ -> { System.err.println("close handler"); }); netSocket.exceptionHandler(e -> { System.err.println("exception " + e); }); netSocket.handler(rp); database("admin").command("ismaster", 0, 1, Utils.handler(handler, r -> { maxBsonSize = r.getInt("maxBsonObjectSize"); System.out.println("got ismaster callback"); handler.handle(Future.succeededFuture(this)); })); } }); }