ChatClient.java 2.88 KB
Newer Older
Gabriele Civitarese's avatar
Gabriele Civitarese committed
1 2
package chat;

Gabriele Civitarese's avatar
Gabriele Civitarese committed
3
import com.example.chat.ChatServiceGrpc.*;
Gabriele Civitarese's avatar
Gabriele Civitarese committed
4
import com.example.chat.ChatServiceGrpc;
Gabriele Civitarese's avatar
Gabriele Civitarese committed
5
import com.example.chat.ChatServiceOuterClass.*;
Gabriele Civitarese's avatar
Gabriele Civitarese committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ChatClient {

    public final static String IP = "localhost";
    public final static int PORT = 1337;

    public static void main(String[] args) throws IOException {

        //buffered reader to read from standard input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        //simply asking the nickname
        String nickname;

        System.out.println("What's your nickname?");
        nickname = br.readLine();


        System.out.println("---------");
        System.out.println("[CHAT CLIENT] Connecting to chat server @ "+IP+":"+PORT);

        //opening a connection with chat server

        final ManagedChannel channel = ManagedChannelBuilder.forTarget(IP+":"+PORT).usePlaintext(true).build();

        System.out.println("[CHAT CLIENT] Connected!");

        System.out.println("[CHAT CLIENT] Establishing stream from server...");

        //creating the asynchronous stub

Gabriele Civitarese's avatar
Gabriele Civitarese committed
44
        ChatServiceStub stub = ChatServiceGrpc.newStub(channel);
Gabriele Civitarese's avatar
Gabriele Civitarese committed
45 46 47 48

        //the stub returns a stream (to communicate with the server, and thus with all the other clients).
        //the argument is the stream of messages which are transmitted by the server.

Gabriele Civitarese's avatar
Gabriele Civitarese committed
49
        StreamObserver<ChatMessage> serverStream = stub.chat(new StreamObserver<ChatMessage>() {
Gabriele Civitarese's avatar
Gabriele Civitarese committed
50 51 52 53

            //remember: all the methods here are CALLBACKS which are handled in an asynchronous manner.

            //we define what to do when a message from the server arrives (just print the message)
Gabriele Civitarese's avatar
Gabriele Civitarese committed
54
            public void onNext(ChatMessage chatMessage) {
Gabriele Civitarese's avatar
Gabriele Civitarese committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

                String from = chatMessage.getFrom();
                String message = chatMessage.getMessage();

                System.out.println("\n"+from+" -> "+message);

            }

            public void onError(Throwable throwable) {

            }

            public void onCompleted() {

            }
        });


        System.out.println("[CHAT CLIENT] Now you can chat :)");


        //dumb code to handle the chat
        //it is a while loop which reads the message
        while(true){

            String message = br.readLine();

            //if the message is quit
            if(message.equals("quit")){
                //we communicate to the server that we are done and we exit the loop
                serverStream.onCompleted();
                break;
            }

            //we use the stream to communicate to the server our message.
Gabriele Civitarese's avatar
Gabriele Civitarese committed
90
            serverStream.onNext(ChatMessage.newBuilder().setFrom(nickname).setMessage(message).build());
Gabriele Civitarese's avatar
Gabriele Civitarese committed
91 92 93 94 95 96 97 98

        }


        System.out.println("Goodbye!");
    }

}