1420c9859SShuo Chenpackage sudoku;
2420c9859SShuo Chen
3420c9859SShuo Chenimport java.net.InetSocketAddress;
4b5a588dfSShuo Chenimport java.util.concurrent.CountDownLatch;
5b5a588dfSShuo Chenimport java.util.concurrent.TimeUnit;
6420c9859SShuo Chen
7420c9859SShuo Chenimport muduo.rpc.NewChannelCallback;
8420c9859SShuo Chenimport muduo.rpc.RpcChannel;
9420c9859SShuo Chenimport muduo.rpc.RpcClient;
10420c9859SShuo Chenimport sudoku.Sudoku.SudokuRequest;
11420c9859SShuo Chenimport sudoku.Sudoku.SudokuResponse;
12420c9859SShuo Chenimport sudoku.Sudoku.SudokuService;
13b5a588dfSShuo Chenimport sudoku.Sudoku.SudokuService.BlockingInterface;
14420c9859SShuo Chen
15420c9859SShuo Chenimport com.google.protobuf.RpcCallback;
16420c9859SShuo Chen
17420c9859SShuo Chenpublic class Client {
18420c9859SShuo Chen
19b5a588dfSShuo Chen    private static void blockingConnect(InetSocketAddress addr) throws Exception {
20420c9859SShuo Chen        RpcClient client = new RpcClient();
21420c9859SShuo Chen        RpcChannel channel = client.blockingConnect(addr);
22b5a588dfSShuo Chen        //sendRequest(channel, client);
23b5a588dfSShuo Chen        BlockingInterface remoteService = Sudoku.SudokuService.newBlockingStub(channel);
24b5a588dfSShuo Chen        SudokuRequest request = SudokuRequest.newBuilder().setCheckerboard("001010").build();
25b5a588dfSShuo Chen        SudokuResponse response = remoteService.solve(null, request);
26b5a588dfSShuo Chen        System.out.println(response);
27b5a588dfSShuo Chen        channel.disconnect();
28b5a588dfSShuo Chen        client.stop();
29420c9859SShuo Chen    }
30420c9859SShuo Chen
31b5a588dfSShuo Chen    @SuppressWarnings("unused")
32420c9859SShuo Chen    private static void asyncConnect(InetSocketAddress addr) {
33b5a588dfSShuo Chen        final RpcClient client = new RpcClient();
34420c9859SShuo Chen        client.registerService(Sudoku.SudokuService.newReflectiveService(new SudokuImpl()));
35420c9859SShuo Chen        client.startConnect(addr, new NewChannelCallback() {
36420c9859SShuo Chen            @Override
37420c9859SShuo Chen            public void run(RpcChannel channel) {
38b5a588dfSShuo Chen                sendAsyncRequest(channel, client);
39420c9859SShuo Chen            }
40420c9859SShuo Chen        });
41420c9859SShuo Chen    }
42420c9859SShuo Chen
43b5a588dfSShuo Chen    private static void sendAsyncRequest(final RpcChannel channel, RpcClient client) {
44b5a588dfSShuo Chen        final CountDownLatch latch = new CountDownLatch(1);
45b5a588dfSShuo Chen        System.err.println("sendRequest " + channel);
46420c9859SShuo Chen        SudokuService remoteService = Sudoku.SudokuService.newStub(channel);
47420c9859SShuo Chen        SudokuRequest request = SudokuRequest.newBuilder().setCheckerboard("001010").build();
48420c9859SShuo Chen        remoteService.solve(null, request, new RpcCallback<Sudoku.SudokuResponse>() {
49420c9859SShuo Chen            @Override
50420c9859SShuo Chen            public void run(SudokuResponse parameter) {
51420c9859SShuo Chen                System.out.println(parameter);
52b5a588dfSShuo Chen                channel.disconnect();
53b5a588dfSShuo Chen                latch.countDown();
54420c9859SShuo Chen            }
55420c9859SShuo Chen        });
56b5a588dfSShuo Chen        try {
57b5a588dfSShuo Chen            latch.await(5, TimeUnit.SECONDS);
58b5a588dfSShuo Chen        } catch (InterruptedException e) {
59b5a588dfSShuo Chen            // TODO Auto-generated catch block
60b5a588dfSShuo Chen            e.printStackTrace();
61b5a588dfSShuo Chen        }
62b5a588dfSShuo Chen        client.stop();
63420c9859SShuo Chen    }
64420c9859SShuo Chen
65420c9859SShuo Chen    public static void main(String[] args) throws Exception {
66b5a588dfSShuo Chen        InetSocketAddress addr = new InetSocketAddress("localhost", 9981);
67420c9859SShuo Chen        // asyncConnect(addr);
68420c9859SShuo Chen        blockingConnect(addr);
69420c9859SShuo Chen    }
70420c9859SShuo Chen}
71