package vs;

import java.io.IOException;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;


import vs.SyslogGrpc;
import vs.SyslogOuterClass.Message;
import vs.SyslogOuterClass.Result;
import vs.SyslogOuterClass.Severity;

public class SyslogServer extends SyslogGrpc.SyslogImplBase {

    @Override
    public void checkUrgency(Message message,
            StreamObserver<Result> resultObserver) {
        boolean urgency = false;
        if (message.getPriority() == Severity.Alert
                || message.getPriority() == Severity.Critical) {
            urgency = true;
        }

        Result result = Result //
                .newBuilder() //
                .setVal(urgency) //
                .build();
        resultObserver.onNext(result);
        resultObserver.onCompleted();
    }

    @Override
    public void log(Message message, StreamObserver<Result> resultObserver) {
        System.out.println(message);
        Result result = Result //
                .newBuilder() //
                .setVal(true) //
                .build();
        resultObserver.onNext(result);
        resultObserver.onCompleted();
    }

    public static void main(String[] args)
            throws IOException, InterruptedException {
        Server server = ServerBuilder //
                .forPort(9000) //
                .addService(new SyslogServer()) //
                .build();
        server.start();
        server.awaitTermination();
    }
}
