Client.java revision 420c9859
1420c9859SShuo Chenpackage sudoku; 2420c9859SShuo Chen 3420c9859SShuo Chenimport java.net.InetSocketAddress; 4420c9859SShuo Chen 5420c9859SShuo Chenimport muduo.rpc.NewChannelCallback; 6420c9859SShuo Chenimport muduo.rpc.RpcChannel; 7420c9859SShuo Chenimport muduo.rpc.RpcClient; 8420c9859SShuo Chenimport sudoku.Sudoku.SudokuRequest; 9420c9859SShuo Chenimport sudoku.Sudoku.SudokuResponse; 10420c9859SShuo Chenimport sudoku.Sudoku.SudokuService; 11420c9859SShuo Chen 12420c9859SShuo Chenimport com.google.protobuf.RpcCallback; 13420c9859SShuo Chen 14420c9859SShuo Chenpublic class Client { 15420c9859SShuo Chen 16420c9859SShuo Chen private static void blockingConnect(InetSocketAddress addr) { 17420c9859SShuo Chen RpcClient client = new RpcClient(); 18420c9859SShuo Chen RpcChannel channel = client.blockingConnect(addr); 19420c9859SShuo Chen sendRequest(channel); 20420c9859SShuo Chen } 21420c9859SShuo Chen 22420c9859SShuo Chen private static void asyncConnect(InetSocketAddress addr) { 23420c9859SShuo Chen RpcClient client = new RpcClient(); 24420c9859SShuo Chen client.registerService(Sudoku.SudokuService.newReflectiveService(new SudokuImpl())); 25420c9859SShuo Chen client.startConnect(addr, new NewChannelCallback() { 26420c9859SShuo Chen @Override 27420c9859SShuo Chen public void run(RpcChannel channel) { 28420c9859SShuo Chen sendRequest(channel); 29420c9859SShuo Chen } 30420c9859SShuo Chen }); 31420c9859SShuo Chen } 32420c9859SShuo Chen 33420c9859SShuo Chen private static void sendRequest(RpcChannel channel) { 34420c9859SShuo Chen SudokuService remoteService = Sudoku.SudokuService.newStub(channel); 35420c9859SShuo Chen SudokuRequest request = SudokuRequest.newBuilder().setCheckerboard("001010").build(); 36420c9859SShuo Chen remoteService.solve(null, request, new RpcCallback<Sudoku.SudokuResponse>() { 37420c9859SShuo Chen @Override 38420c9859SShuo Chen public void run(SudokuResponse parameter) { 39420c9859SShuo Chen System.out.println(parameter); 40420c9859SShuo Chen } 41420c9859SShuo Chen }); 42420c9859SShuo Chen } 43420c9859SShuo Chen 44420c9859SShuo Chen public static void main(String[] args) throws Exception { 45420c9859SShuo Chen InetSocketAddress addr = new InetSocketAddress("localhost", 8888); 46420c9859SShuo Chen // asyncConnect(addr); 47420c9859SShuo Chen blockingConnect(addr); 48420c9859SShuo Chen } 49420c9859SShuo Chen} 50