summaryrefslogtreecommitdiff
path: root/src/main/java/com/orbekk/protobuf/SimpleProtobufServer.java
blob: a6dd7c4fe8e3285dfa6abc9dc37d11c8a330925a (plain)
1
2
3
4
5
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
44
45
46
47
48
49
50
51
52
53
54
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.orbekk.protobuf;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.OutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import com.google.protobuf.Message;
import com.google.protobuf.Service;
import com.google.protobuf.RpcController;
import com.google.protobuf.RpcCallback;
import com.google.protobuf.Descriptors;
import java.util.Map;
import java.util.HashMap;

public class SimpleProtobufServer extends Thread {
    private static Logger logger = Logger.getLogger(
            SimpleProtobufServer.class.getName());
    private ServerSocket serverSocket;
    private Map<String, Service> registeredServices =
            new HashMap<String, Service>();

    public static SimpleProtobufServer create(int port) {
        try {
            InetSocketAddress address = new InetSocketAddress(port);
            ServerSocket serverSocket = new ServerSocket();
            serverSocket.setReuseAddress(true);
            serverSocket.bind(address);
            return new SimpleProtobufServer(serverSocket);
        } catch (IOException e) {
            logger.log(Level.WARNING, "Could not create server. ", e);
            return null;
        }
    }

    public SimpleProtobufServer(ServerSocket serverSocket) {
        this.serverSocket = serverSocket;
    }

    public synchronized void registerService(Service service) {
        String serviceName = service.getDescriptorForType().getFullName();
        if (registeredServices.containsKey(serviceName)) {
            logger.warning("Already registered service with this name.");
        }
        logger.info("Registering service: " + serviceName);
        registeredServices.put(serviceName, service);
    }

    public void handleRequest(Rpc.Request request, OutputStream out)
            throws IOException {
        Service service = registeredServices.get(request.getFullServiceName());
        final Rpc.Response.Builder response = Rpc.Response.newBuilder();
        response.setRequestId(request.getRequestId());
        if (service == null) {
            response.setError(Rpc.Response.Error.UNKNOWN_SERVICE);
            response.build().writeDelimitedTo(out);
            return;
        }
        Descriptors.MethodDescriptor method = service.getDescriptorForType()
                .findMethodByName(request.getMethodName());
        if (method == null) {
            response.setError(Rpc.Response.Error.UNKNOWN_METHOD);
            response.build().writeDelimitedTo(out);
            return;
        }
        RpcCallback<Message> doneCallback = new RpcCallback<Message>() {
            @Override public void run(Message responseMessage) {
                response.setResponseProto(responseMessage.toByteString());
            }
        };
        Message requestMessage = service.getRequestPrototype(method)
                .toBuilder()
                .mergeFrom(request.getRequestProto())
                .build();
        service.callMethod(method, null,  requestMessage, doneCallback);
        response.build().writeDelimitedTo(out);
    }

    private void handleConnection(final Socket connection) {
        new Thread(new Runnable() {
            @Override public void run() {
                try {
                    while (true) {
                        Rpc.Request r1 = Rpc.Request.parseDelimitedFrom(
                            connection.getInputStream());
                        if (r1 == null) {
                            try {
                                connection.close();
                            } catch (IOException e) {
                                // Connection is closed.
                            }
                        }
                        handleRequest(r1, connection.getOutputStream());
                    }
                } catch (IOException e) {
                    logger.info("Closed connection: " + connection);
                } finally {
                    try {
                        connection.close();
                    } catch (IOException e) {
                    }
                }
            }
        }).start();
    }

    public void run() {
        logger.info("Running server on port " + serverSocket.getLocalPort());
        while (!serverSocket.isClosed()) {
            try {
                Socket connection = serverSocket.accept();
                handleConnection(connection);
            } catch (IOException e) {
                logger.log(Level.WARNING, "Could not establish connection. ",
                        e);
            }
        }
    }

    public static void main(String[] args) {
        SimpleProtobufServer server = SimpleProtobufServer.create(10000);
        Test.TestService testService = new Test.TestService() {
            @Override public void run(RpcController controller,
                    Test.TestRequest request,
                    RpcCallback<Test.TestResponse> done) {
                System.out.println("Hello from TestService!");
                done.run(Test.TestResponse.newBuilder()
                        .setId("Hello from server.")
                        .build());
            }
        };
        server.registerService(testService);
        server.start();
        try {
            server.join();
        } catch (InterruptedException e) {
            System.out.println("Stopped.");
        }
    }
}