@Test public void testCall6() throws Exception { class TestBizSocket extends AbstractBizSocket { public TestBizSocket(Configuration configuration) { super(configuration); } @Override protected PacketFactory createPacketFactory() { return new SamplePacketFactory(); } @Override public Object request(bizsocket.tcp.Request request, ResponseHandler responseHandler) { JSONObject obj = new JSONObject(); try { obj.put("code",200); obj.put("msg","ok"); } catch (JSONException e) { e.printStackTrace(); } ByteString responseStr = ByteString.encodeUtf8(obj.toString()); responseHandler.sendSuccessMessage(22, ByteString.encodeUtf8("{}"), getPacketFactory().getRequestPacket(new bizsocket.tcp.Request.Builder().command(22).body(responseStr).build())); return new Object(); } } TestBizSocket bizSocket = new TestBizSocket(new Configuration.Builder() .host("127.0.0.1") .port(8080).build()); BizSocketRxSupport bizSocketRxSupport = new BizSocketRxSupport.Builder() .requestConverter(new JSONRequestConverter()) .responseConverter(new JSONResponseConverter()) .bizSocket(bizSocket).build(); Method method = serviceClazz.getMethod("testCall6",Object.class,String.class,String.class); BizSocketCall call = new BizSocketCall(); Observable<JSONObject> observable = (Observable<JSONObject>) call.call(bizSocketRxSupport, method, new Object(), "myusername", "mypassword"); observable.subscribe(new Subscriber<JSONObject>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { fail(e.getMessage()); } @Override public void onNext(JSONObject s) { System.out.println(s); } }); }
public Observable<?> call(final BizSocketRxSupport bizSocketRxSupport, final Method method, Object... args) throws Exception { Request request = null; if ((request = method.getAnnotation(Request.class)) == null) { throw new IllegalArgumentException("Can not found annotation(" + Request.class.getPackage() + "." + Request.class.getSimpleName() + ")"); } final int command = getCommand(request,method,args); System.out.print("cmd: " + command); final Object tag = getTag(method,args); final ByteString requestBody = getRequestBody(bizSocketRxSupport,method,args); final String desc = request.desc(); return Observable.create(new Observable.OnSubscribe<Object>() { @Override public void call(final Subscriber<? super Object> subscriber) { bizsocket.tcp.Request req = new bizsocket.tcp.Request.Builder().tag(tag).command(command).body(requestBody).description(desc).build(); bizSocketRxSupport.getBizSocket().request(req, new ResponseHandler() { @Override public void sendSuccessMessage(int command, ByteString requestBody, Packet responsePacket) { Object response = bizSocketRxSupport.getResponseConverter().convert(command,requestBody,getResponseType(method),responsePacket); subscriber.onNext(response); } @Override public void sendFailureMessage(int command, Throwable error) { subscriber.onError(error); } }); } }); }
BizSocketCall { public Observable<?> call(final BizSocketRxSupport bizSocketRxSupport, final Method method, Object... args) throws Exception { Request request = null; if ((request = method.getAnnotation(Request.class)) == null) { throw new IllegalArgumentException("Can not found annotation(" + Request.class.getPackage() + "." + Request.class.getSimpleName() + ")"); } final int command = getCommand(request,method,args); System.out.print("cmd: " + command); final Object tag = getTag(method,args); final ByteString requestBody = getRequestBody(bizSocketRxSupport,method,args); final String desc = request.desc(); return Observable.create(new Observable.OnSubscribe<Object>() { @Override public void call(final Subscriber<? super Object> subscriber) { bizsocket.tcp.Request req = new bizsocket.tcp.Request.Builder().tag(tag).command(command).body(requestBody).description(desc).build(); bizSocketRxSupport.getBizSocket().request(req, new ResponseHandler() { @Override public void sendSuccessMessage(int command, ByteString requestBody, Packet responsePacket) { Object response = bizSocketRxSupport.getResponseConverter().convert(command,requestBody,getResponseType(method),responsePacket); subscriber.onNext(response); } @Override public void sendFailureMessage(int command, Throwable error) { subscriber.onError(error); } }); } }); } }
BizSocketCall { public Observable<?> call(final BizSocketRxSupport bizSocketRxSupport, final Method method, Object... args) throws Exception { Request request = null; if ((request = method.getAnnotation(Request.class)) == null) { throw new IllegalArgumentException("Can not found annotation(" + Request.class.getPackage() + "." + Request.class.getSimpleName() + ")"); } final int command = getCommand(request,method,args); System.out.print("cmd: " + command); final Object tag = getTag(method,args); final ByteString requestBody = getRequestBody(bizSocketRxSupport,method,args); final String desc = request.desc(); return Observable.create(new Observable.OnSubscribe<Object>() { @Override public void call(final Subscriber<? super Object> subscriber) { bizsocket.tcp.Request req = new bizsocket.tcp.Request.Builder().tag(tag).command(command).body(requestBody).description(desc).build(); bizSocketRxSupport.getBizSocket().request(req, new ResponseHandler() { @Override public void sendSuccessMessage(int command, ByteString requestBody, Packet responsePacket) { Object response = bizSocketRxSupport.getResponseConverter().convert(command,requestBody,getResponseType(method),responsePacket); subscriber.onNext(response); } @Override public void sendFailureMessage(int command, Throwable error) { subscriber.onError(error); } }); } }); }  }
BizSocketCall { public Observable<?> call(final BizSocketRxSupport bizSocketRxSupport, final Method method, Object... args) throws Exception { Request request = null; if ((request = method.getAnnotation(Request.class)) == null) { throw new IllegalArgumentException("Can not found annotation(" + Request.class.getPackage() + "." + Request.class.getSimpleName() + ")"); } final int command = getCommand(request,method,args); System.out.print("cmd: " + command); final Object tag = getTag(method,args); final ByteString requestBody = getRequestBody(bizSocketRxSupport,method,args); final String desc = request.desc(); return Observable.create(new Observable.OnSubscribe<Object>() { @Override public void call(final Subscriber<? super Object> subscriber) { bizsocket.tcp.Request req = new bizsocket.tcp.Request.Builder().tag(tag).command(command).body(requestBody).description(desc).build(); bizSocketRxSupport.getBizSocket().request(req, new ResponseHandler() { @Override public void sendSuccessMessage(int command, ByteString requestBody, Packet responsePacket) { Object response = bizSocketRxSupport.getResponseConverter().convert(command,requestBody,getResponseType(method),responsePacket); subscriber.onNext(response); } @Override public void sendFailureMessage(int command, Throwable error) { subscriber.onError(error); } }); } }); }  Observable<?> call(final BizSocketRxSupport bizSocketRxSupport, final Method method, Object... args); int getCommand(Request request,Method method, Object[] args); Object getDefaultTag(); Type getResponseType(Method method); Object getTag(Method method,Object... args); ByteString getRequestBody(BizSocketRxSupport bizSocketRxSupport, Method method, Object[] args); }
BizSocketCall { public Observable<?> call(final BizSocketRxSupport bizSocketRxSupport, final Method method, Object... args) throws Exception { Request request = null; if ((request = method.getAnnotation(Request.class)) == null) { throw new IllegalArgumentException("Can not found annotation(" + Request.class.getPackage() + "." + Request.class.getSimpleName() + ")"); } final int command = getCommand(request,method,args); System.out.print("cmd: " + command); final Object tag = getTag(method,args); final ByteString requestBody = getRequestBody(bizSocketRxSupport,method,args); final String desc = request.desc(); return Observable.create(new Observable.OnSubscribe<Object>() { @Override public void call(final Subscriber<? super Object> subscriber) { bizsocket.tcp.Request req = new bizsocket.tcp.Request.Builder().tag(tag).command(command).body(requestBody).description(desc).build(); bizSocketRxSupport.getBizSocket().request(req, new ResponseHandler() { @Override public void sendSuccessMessage(int command, ByteString requestBody, Packet responsePacket) { Object response = bizSocketRxSupport.getResponseConverter().convert(command,requestBody,getResponseType(method),responsePacket); subscriber.onNext(response); } @Override public void sendFailureMessage(int command, Throwable error) { subscriber.onError(error); } }); } }); }  Observable<?> call(final BizSocketRxSupport bizSocketRxSupport, final Method method, Object... args); int getCommand(Request request,Method method, Object[] args); Object getDefaultTag(); Type getResponseType(Method method); Object getTag(Method method,Object... args); ByteString getRequestBody(BizSocketRxSupport bizSocketRxSupport, Method method, Object[] args);  }