List of usage examples for org.apache.thrift.protocol TMultiplexedProtocol TMultiplexedProtocol
public TMultiplexedProtocol(TProtocol protocol, String serviceName)
From source file:alluxio.AbstractClient.java
License:Apache License
/** * Connects with the remote./*from ww w . j a v a2 s .c o m*/ */ public synchronized void connect() throws IOException { if (mConnected) { return; } disconnect(); Preconditions.checkState(!mClosed, "Client is closed, will not try to connect."); RetryPolicy retryPolicy = new ExponentialBackoffRetry(BASE_SLEEP_MS, MAX_SLEEP_MS, RPC_MAX_NUM_RETRY); while (!mClosed) { mAddress = getAddress(); LOG.info("Alluxio client (version {}) is trying to connect with {} @ {}", RuntimeConstants.VERSION, getServiceName(), mAddress); TProtocol binaryProtocol = new TBinaryProtocol( mTransportProvider.getClientTransport(mParentSubject, mAddress)); mProtocol = new TMultiplexedProtocol(binaryProtocol, getServiceName()); try { mProtocol.getTransport().open(); LOG.info("Client registered with {} @ {}", getServiceName(), mAddress); mConnected = true; afterConnect(); checkVersion(getClient(), getServiceVersion()); return; } catch (IOException e) { if (e.getMessage() != null && FRAME_SIZE_EXCEPTION_PATTERN.matcher(e.getMessage()).find()) { // See an error like "Frame size (67108864) larger than max length (16777216)!", // pointing to the helper page. String message = String.format( "Failed to connect with %s @ %s: %s. " + "This exception may be caused by incorrect network configuration. " + "Please consult %s for common solutions to address this problem.", getServiceName(), mAddress, e.getMessage(), RuntimeConstants.ALLUXIO_DEBUG_DOCS_URL); throw new UnimplementedException(message, e); } throw e; } catch (TTransportException e) { LOG.warn("Failed to connect ({}) with {} @ {}: {}", retryPolicy.getRetryCount(), getServiceName(), mAddress, e.getMessage()); if (e.getCause() instanceof java.net.SocketTimeoutException) { // Do not retry if socket timeout. String message = "Thrift transport open times out. Please check whether the " + "authentication types match between client and server. Note that NOSASL client " + "is not able to connect to servers with SIMPLE security mode."; throw new UnavailableException(message, e); } // TODO(peis): Consider closing the connection here as well. if (!retryPolicy.attemptRetry()) { break; } } } // Reaching here indicates that we did not successfully connect. throw new UnavailableException(String.format("Failed to connect to %s @ %s after %s attempts", getServiceName(), mAddress, retryPolicy.getRetryCount())); }
From source file:alluxio.client.block.BlockWorkerClient.java
License:Apache License
/** * Opens the connection to the worker. And start the heartbeat thread. * * @throws IOException if a non-Alluxio exception occurs *//*from ww w .j a v a 2s .com*/ private synchronized void connectOperation() throws IOException { if (!mConnected) { LOG.info("Connecting to {} worker @ {}", (mIsLocal ? "local" : "remote"), mAddress); TProtocol binaryProtocol = new TBinaryProtocol(mTransportProvider.getClientTransport(mAddress)); mProtocol = new TMultiplexedProtocol(binaryProtocol, getServiceName()); mClient = new BlockWorkerClientService.Client(mProtocol); try { mProtocol.getTransport().open(); } catch (TTransportException e) { LOG.error(e.getMessage(), e); return; } mConnected = true; // only start the heartbeat thread if the connection is successful and if there is not // another heartbeat thread running if (mHeartbeat == null || mHeartbeat.isCancelled() || mHeartbeat.isDone()) { final int interval = Configuration.getInt(Constants.USER_HEARTBEAT_INTERVAL_MS); mHeartbeat = mExecutorService .submit(new HeartbeatThread(HeartbeatContext.WORKER_CLIENT, mHeartbeatExecutor, interval)); } } }
From source file:alluxio.network.connection.ThriftClientPool.java
License:Apache License
/** * Creates a thrift client instance.// ww w . j a v a 2 s.c o m * * @return the thrift client created * @throws IOException if it fails to create a thrift client */ @Override protected T createNewResource() throws IOException { TTransport transport = mTransportProvider.getClientTransport(mAddress); TProtocol binaryProtocol = new TBinaryProtocol(transport); T client = createThriftClient(new TMultiplexedProtocol(binaryProtocol, mServiceName)); RetryPolicy retry = new ExponentialBackoffRetry(CONNECTION_OPEN_RETRY_BASE_SLEEP_MS, Constants.SECOND_MS, CONNECTION_OPEN_RETRY_MAX); while (true) { try { if (!transport.isOpen()) { transport.open(); } if (transport.isOpen()) { checkVersion(client); } } catch (TTransportException e) { LOG.error("Failed to connect (" + retry.getRetryCount() + ") to " + getServiceNameForLogging() + " @ " + mAddress, e); if (e.getCause() instanceof java.net.SocketTimeoutException) { // Do not retry if socket timeout. String message = "Thrift transport open times out. Please check whether the " + "authentication types match between client and server. Note that NOSASL client " + "is not able to connect to servers with SIMPLE security mode."; throw new IOException(message, e); } if (!retry.attemptRetry()) { throw new IOException(e); } } break; } LOG.info("Created a new thrift client {}", client.toString()); return client; }
From source file:alluxio.network.thrift.ThriftUtils.java
License:Apache License
/** * @param transport a transport for communicating with an Alluxio Thrift server * @param serviceName the service to communicate with * @return a Thrift protocol for communicating with the given service through the transport *//* w ww . ja va 2 s . co m*/ public static TProtocol createThriftProtocol(TTransport transport, String serviceName) throws UnauthenticatedException { TProtocol binaryProtocol = new TBinaryProtocol(transport); TProtocol multiplexedProtocol = new TMultiplexedProtocol(binaryProtocol, serviceName); return multiplexedProtocol; }
From source file:alluxio.security.authentication.TProtocols.java
License:Apache License
/** * @param transport a transport for communicating with an Alluxio Thrift server * @param serviceName the service to communicate with * @return a Thrift protocol for communicating with the given service through the transport *//*from www . j a v a2 s.c o m*/ public static TProtocol createProtocol(TTransport transport, String serviceName) throws UnauthenticatedException { TProtocol binaryProtocol = new TBinaryProtocol(transport); TProtocol multiplexedProtocol = new TMultiplexedProtocol(binaryProtocol, serviceName); return multiplexedProtocol; }
From source file:api.CGame.java
public static IGame connectToServer(String addr, int port) { transport = new TSocket(addr, port); try {//from w w w.j a v a2 s . c om transport.open(); } catch (TTransportException ex) { Logger.getLogger(CGame.class.getName()).log(Level.SEVERE, null, ex); } protocol = new TBinaryProtocol(transport); System.out.println("connected to server: " + addr + ":" + port); CGame instance = new CGame(); instance.client = new Game.Client(new TMultiplexedProtocol(protocol, "Game")); return instance; }
From source file:api.CPlayer.java
CPlayer(int clientid) { System.out.println("CPlayer id: " + clientid); TMultiplexedProtocol protocol = new TMultiplexedProtocol(CGame.protocol, "Player"); client = new Player.Client(protocol); }
From source file:club.jmint.crossing.bservice.provider.ThriftRpcProvider.java
License:Apache License
@Override public String execute(String server, String clazz, String inf, String jsonParams, boolean encrypt) throws CrossException { Object jsonResult = null;//from w ww . j a va 2s . com Object execObj = null; Method[] am; Method mm; Class<?> cl = null; TMultiplexedProtocol mp = null; String proxyClassName = clazz + "$Client"; try { cl = Class.forName(proxyClassName); } catch (ClassNotFoundException e) { throw new CrossException(ErrorCode.CROSSING_ERR_CLASS_NOT_FOUND.getCode(), ErrorCode.CROSSING_ERR_CLASS_NOT_FOUND.getInfo()); } try { synchronized (proxyObjMap) { execObj = proxyObjMap.get(clazz); if (execObj == null) { mp = new TMultiplexedProtocol(tpMap.get(server).protocol, clazz); execObj = cl.getDeclaredConstructor(TProtocol.class).newInstance(mp); proxyObjMap.put(clazz, execObj); } } am = cl.getDeclaredMethods(); int si = 0; for (int i = 0; i < am.length; i++) { //should be optimized later. //System.out.println(am[i]); if (am[i].getName().equals(callProxyMap.get(server))) { si = i; break; } } mm = cl.getDeclaredMethod(am[si].getName(), am[si].getParameterTypes()); jsonResult = mm.invoke(execObj, inf, jsonParams, encrypt); CrossLog.logger.info("Invoked ThriftRpc call: " + server + " ==> " + clazz + " ==> " + inf); } catch (Exception e) { CrossLog.printStackTrace(e); throw new CrossException(ErrorCode.CROSSING_ERR_INTERNAL.getCode(), ErrorCode.CROSSING_ERR_INTERNAL.getInfo()); } return jsonResult.toString(); }
From source file:club.jmint.demo.mifty.client.TestClient.java
License:Apache License
public static void main(String[] args) { try {/*from w w w .j av a 2s . c o m*/ TTransport transport; // if (true) { transport = new TSocket("localhost", 9001); transport.open(); // } // else { // /* // * Similar to the server, you can use the parameters to setup client parameters or // * use the default settings. On the client side, you will need a TrustStore which // * contains the trusted certificate along with the public key. // * For this example it's a self-signed cert. // */ // TSSLTransportParameters params = new TSSLTransportParameters(); // params.setTrustStore("../../lib/java/test/.truststore", "thrift", "SunX509", "JKS"); // /* // * Get a client transport instead of a server transport. The connection is opened on // * invocation of the factory method, no need to specifically call open() // */ // transport = TSSLTransportFactory.getClientSocket("localhost", 9091, 0, params); // } TProtocol protocol = new TBinaryProtocol(transport); TMultiplexedProtocol mp1 = new TMultiplexedProtocol(protocol, "com.twohalf.mifty.service.gen.UserService"); UserService.Client client = new UserService.Client(mp1); // TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol,"DemoService"); // DemoService.Client client2 = new DemoService.Client(mp2); // transport.open(); //first call JsonObject pp1 = new JsonObject(); pp1.addProperty("userId", "13530081915"); pp1.addProperty("userPwd", "Darren"); String signP1 = null; try { signP1 = ParamBuilder.buildSignedParams(pp1.toString(), "miftyExampleKey"); signP1 = ParamBuilder.buildEncryptedParams(signP1, "miftyExampleKey"); } catch (CrossException e) { e.printStackTrace(); } String ret = client.callProxy("passwordVerify", signP1, true); String retp = ParamBuilder.buildDecryptedParams(ret, "miftyExampleKey"); System.out.println(retp); System.out.println(ParamBuilder.checkSignAndRemove(retp, "miftyExampleKey")); //second call // JsonObject pp2 = new JsonObject(); // pp2.addProperty("hello", "Good morning!"); // pp2.addProperty("name", "Darren"); // String signP2 =null; // try { // signP2 = ParamBuilder.buildEncryptedParams( // ParamBuilder.buildSignedParams(pp2.toString(), "miftyExampleKey"),"miftyExampleKey"); // } catch (CrossException e) { // e.printStackTrace(); // } // ret = client.sayHello(signP2, true); // System.out.println(ParamBuilder.checkSignAndRemove( // ParamBuilder.buildDecryptedParams(ret, "miftyExampleKey"),"miftyExampleKey")); //third call {"echo":"I am a DEMO","name":"DemoService"} // JsonObject pp3 = new JsonObject(); // pp3.addProperty("echo", "I am a DEMO"); // pp3.addProperty("name", "DemoService"); // String signP3 =null; // try { // signP3 = ParamBuilder.buildEncryptedParams( // ParamBuilder.buildSignedParams(pp3.toString(), "miftyExampleKey"),"miftyExampleKey"); // } catch (CrossException e) { // e.printStackTrace(); // } // ret = client2.demoSay(signP3, true); // System.out.println(ParamBuilder.checkSignAndRemove( // ParamBuilder.buildDecryptedParams(ret, "miftyExampleKey"),"miftyExampleKey")); JsonObject jo = new JsonObject(); JsonArray ja = new JsonArray(); JsonNull jn = new JsonNull(); JsonPrimitive jp_str = new JsonPrimitive("kkkkklllllmmmm"); JsonPrimitive jp_int = new JsonPrimitive(113); JsonPrimitive jp_bool = new JsonPrimitive(false); System.out.println(jp_str.toString()); System.out.println(jp_int.toString()); System.out.println(jp_bool.toString()); transport.close(); } catch (TException x) { x.printStackTrace(); } catch (CrossException e1) { e1.printStackTrace(); } }
From source file:club.jmint.mifty.example.ExaClient.java
License:Apache License
public static void main(String[] args) { try {/* w ww. ja v a2 s. c om*/ TTransport transport; // if (true) { transport = new TSocket("localhost", 9090); //transport.open(); // } // else { // /* // * Similar to the server, you can use the parameters to setup client parameters or // * use the default settings. On the client side, you will need a TrustStore which // * contains the trusted certificate along with the public key. // * For this example it's a self-signed cert. // */ // TSSLTransportParameters params = new TSSLTransportParameters(); // params.setTrustStore("../../lib/java/test/.truststore", "thrift", "SunX509", "JKS"); // /* // * Get a client transport instead of a server transport. The connection is opened on // * invocation of the factory method, no need to specifically call open() // */ // transport = TSSLTransportFactory.getClientSocket("localhost", 9091, 0, params); // } TProtocol protocol = new TBinaryProtocol(transport); TMultiplexedProtocol mp1 = new TMultiplexedProtocol(protocol, "ExaService"); ExaService.Client client = new ExaService.Client(mp1); TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "DemoService"); DemoService.Client client2 = new DemoService.Client(mp2); transport.open(); //first call JsonObject pp1 = new JsonObject(); pp1.addProperty("echo", "How are you!"); pp1.addProperty("name", "Darren"); String signP1 = null; try { signP1 = ParamBuilder.buildSignedParams(pp1.toString(), "miftyExampleKey"); } catch (CrossException e) { e.printStackTrace(); } String ret = client.echo(signP1, false); System.out.println(ParamBuilder.checkSignAndRemove(ret, "miftyExampleKey")); //second call JsonObject pp2 = new JsonObject(); pp2.addProperty("hello", "Good morning!"); pp2.addProperty("name", "Darren"); String signP2 = null; try { signP2 = ParamBuilder.buildEncryptedParams( ParamBuilder.buildSignedParams(pp2.toString(), "miftyExampleKey"), "miftyExampleKey"); } catch (CrossException e) { e.printStackTrace(); } ret = client.sayHello(signP2, true); System.out.println(ParamBuilder.checkSignAndRemove( ParamBuilder.buildDecryptedParams(ret, "miftyExampleKey"), "miftyExampleKey")); //third call {"echo":"I am a DEMO","name":"DemoService"} JsonObject pp3 = new JsonObject(); pp3.addProperty("echo", "I am a DEMO"); pp3.addProperty("name", "DemoService"); String signP3 = null; try { signP3 = ParamBuilder.buildEncryptedParams( ParamBuilder.buildSignedParams(pp3.toString(), "miftyExampleKey"), "miftyExampleKey"); } catch (CrossException e) { e.printStackTrace(); } ret = client2.demoSay(signP3, true); System.out.println(ParamBuilder.checkSignAndRemove( ParamBuilder.buildDecryptedParams(ret, "miftyExampleKey"), "miftyExampleKey")); transport.close(); } catch (TException x) { x.printStackTrace(); } catch (CrossException e1) { e1.printStackTrace(); } }