1package sudoku; 2 3import java.net.InetSocketAddress; 4import java.util.concurrent.CountDownLatch; 5import java.util.concurrent.TimeUnit; 6 7import muduo.rpc.NewChannelCallback; 8import muduo.rpc.RpcChannel; 9import muduo.rpc.RpcClient; 10import sudoku.Sudoku.SudokuRequest; 11import sudoku.Sudoku.SudokuResponse; 12import sudoku.Sudoku.SudokuService; 13import sudoku.Sudoku.SudokuService.BlockingInterface; 14 15import com.google.protobuf.RpcCallback; 16 17public class Client { 18 19 private static void blockingConnect(InetSocketAddress addr) throws Exception { 20 RpcClient client = new RpcClient(); 21 RpcChannel channel = client.blockingConnect(addr); 22 //sendRequest(channel, client); 23 BlockingInterface remoteService = Sudoku.SudokuService.newBlockingStub(channel); 24 SudokuRequest request = SudokuRequest.newBuilder().setCheckerboard("001010").build(); 25 SudokuResponse response = remoteService.solve(null, request); 26 System.out.println(response); 27 channel.disconnect(); 28 client.stop(); 29 } 30 31 @SuppressWarnings("unused") 32 private static void asyncConnect(InetSocketAddress addr) { 33 final RpcClient client = new RpcClient(); 34 client.registerService(Sudoku.SudokuService.newReflectiveService(new SudokuImpl())); 35 client.startConnect(addr, new NewChannelCallback() { 36 @Override 37 public void run(RpcChannel channel) { 38 sendAsyncRequest(channel, client); 39 } 40 }); 41 } 42 43 private static void sendAsyncRequest(final RpcChannel channel, RpcClient client) { 44 final CountDownLatch latch = new CountDownLatch(1); 45 System.err.println("sendRequest " + channel); 46 SudokuService remoteService = Sudoku.SudokuService.newStub(channel); 47 SudokuRequest request = SudokuRequest.newBuilder().setCheckerboard("001010").build(); 48 remoteService.solve(null, request, new RpcCallback<Sudoku.SudokuResponse>() { 49 @Override 50 public void run(SudokuResponse parameter) { 51 System.out.println(parameter); 52 channel.disconnect(); 53 latch.countDown(); 54 } 55 }); 56 try { 57 latch.await(5, TimeUnit.SECONDS); 58 } catch (InterruptedException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 } 62 client.stop(); 63 } 64 65 public static void main(String[] args) throws Exception { 66 InetSocketAddress addr = new InetSocketAddress("localhost", 9981); 67 // asyncConnect(addr); 68 blockingConnect(addr); 69 } 70} 71