public static Constructor<?> getClientConstructor(Class<?> svcInterface) { String client = svcInterface.getName().indexOf("Async") > 0 ? ASYNC_CLIENT_NAME : CLIENT_NAME; Class<?>[] args = svcInterface.getName().indexOf("Async") > 0 ? new Class[]{TProtocolFactory.class, TAsyncClientManager.class, TNonblockingTransport.class} : new Class[]{TProtocol.class}; Class<?> clientClass = getThriftServiceInnerClassOrNull(svcInterface.getEnclosingClass(), client, false); if (clientClass == null) { throw new ThriftRuntimeException("the client class is null"); } Constructor<?> constructor = ClassUtils.getConstructorIfAvailable(clientClass, args); if (constructor == null) { throw new ThriftRuntimeException("the clientClass constructor is null"); } return constructor; }
@Override public boolean process(TProtocol in, TProtocol out) throws TException { TMessage msg = in.readMessageBegin(); Controller<?, ?> fn = (Controller<?, ?>) this.beanFactory .getBean(msg.name); if (fn == null) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Invalid request: failed to find interface=" + msg.name + ", from: " + getInetAddress(in)); } TProtocolUtil.skip(in, TType.STRUCT); in.readMessageEnd(); TApplicationException x = new TApplicationException( TApplicationException.UNKNOWN_METHOD, "Invalid method name: '" + msg.name + "'"); out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid)); x.write(out); out.writeMessageEnd(); out.getTransport().flush(); return true; } process(msg.seqid, msg.name, in, out, fn); return true; }
protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request request) throws IOException { Invocation invocation = (Invocation) request.getData(); TProtocol protocol = newProtocol(channel.getUrl(), buffer); try { protocol.writeMessageBegin(new TMessage( invocation.getMethodName(), TMessageType.CALL, thriftSeq.getAndIncrement())); protocol.writeStructBegin(new TStruct(invocation.getMethodName() + "_args")); for(int i = 0; i < invocation.getParameterTypes().length; i++) { Class<?> type = invocation.getParameterTypes()[i]; } } catch (TException e) { throw new IOException(e.getMessage(), e); } }
private static void writeRequest(MethodMetadata method, List<Object> parameters, TProtocol protocol) throws Exception { TMessage requestMessage = new TMessage(method.getName(), CALL, SEQUENCE_ID); protocol.writeMessageBegin(requestMessage); // write the parameters ProtocolWriter writer = new ProtocolWriter(new ThriftToDriftProtocolWriter(protocol)); writer.writeStructBegin(method.getName() + "_args"); for (int i = 0; i < parameters.size(); i++) { Object value = parameters.get(i); ParameterMetadata parameter = method.getParameters().get(i); writer.writeField(parameter.getName(), parameter.getId(), parameter.getCodec(), value); } writer.writeStructEnd(); protocol.writeMessageEnd(); protocol.getTransport().flush(); }
/** * Creates a Model and a Thrift Bridge to this model. */ protected static EBMI createModelBridge(String host, String pythonExecutable, File opendaPythonPath, File modelPythonPath, String modelPythonModuleName, String modelPythonClassName, File modelRunDir) throws IOException { // start local server. int port = getFreePort(); if (host == null) { //localhost host = "127.0.0.1"; } Process process = startModelProcess(host, port, pythonExecutable, opendaPythonPath, modelPythonPath, modelPythonModuleName, modelPythonClassName, modelRunDir); // connect to server. TTransport transport = connectToCode(host, port, process); // create client. TProtocol protocol = new TBinaryProtocol(transport); BMIService.Client client = new BMIService.Client(protocol); return new ThriftBmiBridge(client, process, transport); }
@Test public void testBlockSync() throws TException { AskerServer server = new AskerServer(port, true); server.start(); try (TTransport transport = transport(port)) { transport.open(); TProtocol protocol = new TCompactProtocol(transport); final Asker.Client client = new Asker.Client(protocol); Helper helper = new Helper(collector); helper.checkEcho(client); helper.checkCount(client); helper.checkReverse(client); helper.checkUpperCast(client); helper.checkLowerCast(client); helper.checkRandom(client, 5 + random.nextInt(10)); } server.stop(); }
@Test public void testNonBlockSync() throws TException { AskerServer server = new AskerServer(port, false); server.start(); try (TTransport transport = new TFramedTransport(transport(port))) { transport.open(); TProtocol protocol = new TCompactProtocol(transport); final Asker.Client client = new Asker.Client(protocol); Helper helper = new Helper(collector); helper.checkEcho(client); helper.checkCount(client); helper.checkReverse(client); helper.checkUpperCast(client); helper.checkLowerCast(client); helper.checkRandom(client, 5 + random.nextInt(10)); } server.stop(); }
@Test public void testScribeMessage() throws Exception { TTransport transport = new TFramedTransport(new TSocket("localhost", port)); TProtocol protocol = new TBinaryProtocol(transport); Scribe.Client client = new Scribe.Client(protocol); transport.open(); LogEntry logEntry = new LogEntry("INFO", "Sending info msg to scribe source"); List<LogEntry> logEntries = new ArrayList<LogEntry>(1); logEntries.add(logEntry); client.Log(logEntries); // try to get it from Channels Transaction tx = memoryChannel.getTransaction(); tx.begin(); Event e = memoryChannel.take(); Assert.assertNotNull(e); Assert.assertEquals("Sending info msg to scribe source", new String(e.getBody())); tx.commit(); tx.close(); }
@Override public Pair<TTransport, Bmv2DeviceThriftClient> load(DeviceId deviceId) throws TTransportException { log.debug("Instantiating new client... > deviceId={}", deviceId); // Make the expensive call Bmv2Device device = Bmv2Device.of(deviceId); TTransport transport = new TSocket(device.thriftServerHost(), device.thriftServerPort()); TProtocol protocol = new TBinaryProtocol(transport); // Our BMv2 device implements multiple Thrift services, create a client for each one on the same transport. Standard.Client standardClient = new Standard.Client( new TMultiplexedProtocol(protocol, "standard")); SimpleSwitch.Client simpleSwitch = new SimpleSwitch.Client( new TMultiplexedProtocol(protocol, "simple_switch")); // Wrap clients so to automatically have synchronization and resiliency to connectivity errors Standard.Iface safeStandardClient = SafeThriftClient.wrap(standardClient, Standard.Iface.class, options); SimpleSwitch.Iface safeSimpleSwitchClient = SafeThriftClient.wrap(simpleSwitch, SimpleSwitch.Iface.class, options); Bmv2DeviceThriftClient client = new Bmv2DeviceThriftClient(deviceId, transport, safeStandardClient, safeSimpleSwitchClient); return Pair.of(transport, client); }
public static void startClient(String ip ,int port ,int timeout) throws Exception { TTransport transport = new TSocket(ip,port,timeout); TProtocol protocol = new TBinaryProtocol(transport); leafrpc.Client client = new leafrpc.Client(protocol); transport.open(); int i = 0; while(i < 2000000) { client.getID(""); ++i; } transport.close(); }
public static void startClient2(String ip ,int port ,int timeout) throws Exception { TTransport transport = new TFramedTransport(new TSocket(ip,port,timeout)); TProtocol protocol = new TBinaryProtocol(transport); leafrpc.Client client = new leafrpc.Client(protocol); transport.open(); for(int i = 0; i< 1000000; i++) { client.getID(""); if (i % 100000 == 0) { System.out.println(Thread.currentThread().getName() + " " + client.getID("")); } //ai.incrementAndGet(); } transport.close(); }
@Override public TServiceClient makeObject() throws Exception { InetSocketAddress address = serverAddressProvider.selector(); if(address==null){ new ThriftException("No provider available for remote service"); } TSocket tsocket = new TSocket(address.getHostName(), address.getPort()); TTransport transport = new TFramedTransport(tsocket); TProtocol protocol = new TBinaryProtocol(transport); TServiceClient client = this.clientFactory.getClient(protocol); transport.open(); if (callback != null) { try { callback.make(client); } catch (Exception e) { logger.warn("makeObject:{}", e); } } return client; }
/** * Encodes a thrift object into a DEFLATE-compressed binary array. * * @param tBase Object to encode. * @return Deflated, encoded object. * @throws CodingException If the object could not be encoded. */ public static byte[] deflateNonNull(TBase<?, ?> tBase) throws CodingException { requireNonNull(tBase); // NOTE: Buffering is needed here for performance. // There are actually 2 buffers in play here - the BufferedOutputStream prevents thrift from // causing a call to deflate() on every encoded primitive. The DeflaterOutputStream buffer // allows the underlying Deflater to operate on a larger chunk at a time without stopping to // copy the intermediate compressed output to outBytes. // See http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4986239 ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); TTransport transport = new TIOStreamTransport( new BufferedOutputStream( new DeflaterOutputStream(outBytes, new Deflater(DEFLATE_LEVEL), DEFLATER_BUFFER_SIZE), DEFLATER_BUFFER_SIZE)); try { TProtocol protocol = PROTOCOL_FACTORY.getProtocol(transport); tBase.write(protocol); transport.close(); // calls finish() on the underlying stream, completing the compression return outBytes.toByteArray(); } catch (TException e) { throw new CodingException("Failed to serialize: " + tBase, e); } finally { transport.close(); } }
/** * Decodes a thrift object from a DEFLATE-compressed byte array into a target type. * * @param clazz Class to instantiate and deserialize to. * @param buffer Compressed buffer to decode. * @return A populated message. * @throws CodingException If the message could not be decoded. */ public static <T extends TBase<T, ?>> T inflateNonNull(Class<T> clazz, byte[] buffer) throws CodingException { requireNonNull(clazz); requireNonNull(buffer); T tBase = newInstance(clazz); TTransport transport = new TIOStreamTransport( new InflaterInputStream(new ByteArrayInputStream(buffer))); try { TProtocol protocol = PROTOCOL_FACTORY.getProtocol(transport); tBase.read(protocol); return tBase; } catch (TException e) { throw new CodingException("Failed to deserialize: " + e, e); } finally { transport.close(); } }
/** * {@inheritDoc} */ @Override public ServerContext createContext(final TProtocol input, final TProtocol output) { final String userName = "metacat-thrift-interface"; String clientHost = null; //requestContext.getHeaderString("X-Forwarded-For"); final long requestThreadId = Thread.currentThread().getId(); final TTransport transport = input.getTransport(); if (transport instanceof TSocket) { final TSocket thriftSocket = (TSocket) transport; clientHost = thriftSocket.getSocket().getInetAddress().getHostAddress(); } final CatalogServerRequestContext context = new CatalogServerRequestContext( userName, null, clientHost, null, "hive", requestThreadId ); MetacatContextManager.setContext(context); return context; }
@Override @SuppressWarnings("unchecked") public <X extends TAsyncClient> X getClient(final Class<X> clazz) { return (X) super.clients.computeIfAbsent(ClassNameUtils.getOuterClassName(clazz), (className) -> { TProtocolFactory protocolFactory = (TProtocolFactory) tTransport -> { TProtocol protocol = new TBinaryProtocol(tTransport); return new TMultiplexedProtocol(protocol, className); }; try { return clazz.getConstructor(TProtocolFactory.class, TAsyncClientManager.class, TNonblockingTransport.class) .newInstance(protocolFactory, this.clientManager, this.transport); } catch (Throwable e) { if (e instanceof UnresolvedAddressException) { this.isOpen = false; } return null; } }); }
@Test public void thriftTest() throws TException { TSocket transport = new TSocket("127.0.0.1", 8080); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); TMultiplexedProtocol mp1 = new TMultiplexedProtocol(protocol,"helloWorld"); HelloWorld.Client client = new HelloWorld.Client(mp1); User user = new User(); user.setName("{\"proid\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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ewBaseResInfo(user); Assert.notNull(result); System.out.println(result.getMsg()); System.out.println("end>>>>>>>>>>>>>>>>"); }
@Override protected void messageReceived(ChannelHandlerContext ctx, ThriftMessage message) throws Exception { ByteBuf buffer = message.getContent(); logger.debug("msg.content:: {}", buffer); try { TNettyTransport transport = new TNettyTransport(ctx.channel(), buffer); TProtocolFactory protocolFactory = message.getProtocolFactory(); TProtocol protocol = protocolFactory.getProtocol(transport); serverDef.nettyProcessor.process(ctx, protocol, protocol, new DefaultWriterListener(message, transport, ctx, serverDef)); } catch (Throwable ex) { int refCount = buffer.refCnt(); if (refCount > 0) { buffer.release(refCount); } throw ex; } }
@SuppressWarnings("rawtypes") private void writeResult(final TProtocol out, final TMessage msg, final WriterHandler onComplete, TBase args, final TBase result) { try { onComplete.beforeWrite(msg, args, result); // if (!isOneway()) { out.writeMessageBegin(new TMessage(msg.name, TMessageType.REPLY, msg.seqid)); if (result != null) { result.write(out); } else { out.writeStructBegin(null); out.writeFieldStop(); out.writeStructEnd(); } out.writeMessageEnd(); out.getTransport().flush(); // } onComplete.afterWrite(msg, null, TMessageType.REPLY, args, result); } catch (Throwable e) { onComplete.afterWrite(msg, e, TMessageType.EXCEPTION, args, result); } }
protected ClientProcessData(GfxdTSocket socket, int connectionNumber, TProcessor proc, TTransport in, TTransport out, TProtocol inp, TProtocol outp, TServerEventHandler eventHandler) { this.clientSocket = socket; this.connectionNumber = connectionNumber; this.processor = proc; this.inputTransport = in; this.outputTransport = out; this.inputProtocol = inp; this.outputProtocol = outp; this.eventHandler = eventHandler; if (eventHandler != null) { this.connectionContext = eventHandler.createContext(inp, outp); } else { this.connectionContext = null; } this.idle = true; }
@Override public final boolean process(final TProtocol in, final TProtocol out) throws TException { final TMessage msg = in.readMessageBegin(); final ProcessFunction<LocatorServiceImpl, ?> fn = this.fnMap .get(msg.name); if (fn != null) { fn.process(msg.seqid, in, out, this.inst); // terminate connection on receiving closeConnection // direct class comparison should be the fastest way return fn.getClass() != LocatorService.Processor.closeConnection.class; } else { TProtocolUtil.skip(in, TType.STRUCT); in.readMessageEnd(); TApplicationException x = new TApplicationException( TApplicationException.UNKNOWN_METHOD, "Invalid method name: '" + msg.name + "'"); out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid)); x.write(out); out.writeMessageEnd(); out.getTransport().flush(); return true; } }
public Worker(int id, long durationMillis, String path, int globals) throws IOException { this.id = id; this.workerDuration = durationMillis; this.localPath = path + "/f" + Integer.toString(id); this.globalPath = "/f" + Integer.toString(id); this.instanceMap = new HashMap<>(); this.globals = globals; String replicaHost = replicaAddr.split(":")[0]; int replicaPort = Integer.parseInt(replicaAddr.split(":")[1]); TTransport transport = new TSocket(replicaHost, replicaPort); try { transport.open(); } catch (TTransportException e) { throw new RuntimeException(e); } TProtocol protocol = new TBinaryProtocol(transport); c = new FuseOps.Client(protocol); out = new BufferedWriter(new FileWriter(new File(logPrefix + this.id))); }
public Worker(int id, long durationMillis, String path, int globals) throws IOException { this.id = id; this.workerDuration = durationMillis; this.path = path; this.globals = globals; String replicaHost = replicaAddr.split(":")[0]; int replicaPort = Integer.parseInt(replicaAddr.split(":")[1]); TTransport transport = new TSocket(replicaHost, replicaPort); try { transport.open(); } catch (TTransportException e) { throw new RuntimeException(e); } TProtocol protocol = new TBinaryProtocol(transport); c = new FuseOps.Client(protocol); out = new BufferedWriter(new FileWriter(new File(logPrefix + this.id))); }
public Worker(int id, String path) throws IOException { this.id = id; this.path = path; this.localPath = path + "/f" + Integer.toString(id); this.globalPath = "/f" + Integer.toString(id); this.instanceMap = new HashMap<>(); String replicaHost = replicaAddr.split(":")[0]; int replicaPort = Integer.parseInt(replicaAddr.split(":")[1]); TTransport transport = new TSocket(replicaHost, replicaPort); try { transport.open(); } catch (TTransportException e) { throw new RuntimeException(e); } TProtocol protocol = new TBinaryProtocol(transport); c = new FuseOps.Client(protocol); }
/** * 读取操作 */ @Override public void read(TProtocol iprot) throws TException { if (!"org.apache.thrift.scheme.StandardScheme".equals(iprot.getScheme().getName())) throw new TApplicationException("Service scheme must be 'org.apache.thrift.scheme.StandardScheme' !"); TField schemeField; iprot.readStructBegin(); while (Boolean.TRUE) { schemeField = iprot.readFieldBegin(); if (schemeField.type == TType.STOP) break; if (schemeField.type == TType.STRING) str = iprot.readString(); else throw new TApplicationException("field type must be 'String' !"); iprot.readFieldEnd(); } iprot.readStructEnd(); }
@Override public Client create() throws Exception { TSocket transport = new TSocket(host, port); try { transport.open(); } catch (TTransportException e) { throw new InterpreterException(e); } TProtocol protocol = new TBinaryProtocol(transport); Client client = new RemoteInterpreterService.Client(protocol); synchronized (clientSocketMap) { clientSocketMap.put(client, transport); } return client; }
protected TProtocol requestTransport(String url) throws TTransportException { // probably not thread safe, but we need it? Not atm. TTransport act; if (!activeTransports.containsKey(url)) { logger.log(Level.DEBUG ,"Creating new transport for: " + url); activeTransports.put(url, new THttpClient(url)); } act = activeTransports.get(url); if (!act.isOpen()) { act.open(); } // THINK: always create new protocol? return new TJSONProtocol(act); }