| [4940] | 1 | package com.joelpm.bidiMessages.server;
|
|---|
| 2 |
|
|---|
| 3 | import org.apache.log4j.Logger;
|
|---|
| 4 |
|
|---|
| 5 | import org.apache.thrift.TProcessor;
|
|---|
| 6 | import org.apache.thrift.TProcessorFactory;
|
|---|
| 7 | import org.apache.thrift.server.TServer;
|
|---|
| 8 | import org.apache.thrift.server.TThreadPoolServer;
|
|---|
| 9 | import org.apache.thrift.transport.TServerSocket;
|
|---|
| 10 | import org.apache.thrift.transport.TServerTransport;
|
|---|
| 11 | import org.apache.thrift.transport.TTransport;
|
|---|
| 12 | import com.joelpm.bidiMessages.generated.MessageService;
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * A simple server that accepts messages from clients and broadcasts
|
|---|
| 16 | * them out to all connected clients.
|
|---|
| 17 | *
|
|---|
| 18 | * @author Joel Meyer
|
|---|
| 19 | */
|
|---|
| 20 | public class Server {
|
|---|
| 21 | private static final Logger LOGGER = Logger.getLogger(Server.class);
|
|---|
| 22 |
|
|---|
| 23 | public static void main(String[] args) throws Exception {
|
|---|
| 24 | final int port = Integer.parseInt(args[0]);
|
|---|
| 25 |
|
|---|
| 26 | final MessageDistributor messageDistributor = new MessageDistributor();
|
|---|
| 27 |
|
|---|
| 28 | new Thread(messageDistributor).start();
|
|---|
| 29 |
|
|---|
| 30 | // Using our own TProcessorFactory gives us an opportunity to get
|
|---|
| 31 | // access to the transport right after the client connection is
|
|---|
| 32 | // accepted.
|
|---|
| 33 | TProcessorFactory processorFactory = new TProcessorFactory(null) {
|
|---|
| 34 | @Override
|
|---|
| 35 | public TProcessor getProcessor(TTransport trans) {
|
|---|
| 36 | messageDistributor.addClient(new MessageServiceClient(trans));
|
|---|
| 37 | return new MessageService.Processor(messageDistributor);
|
|---|
| 38 | }
|
|---|
| 39 | };
|
|---|
| 40 |
|
|---|
| 41 | TServerTransport serverTransport = new TServerSocket(port);
|
|---|
| 42 | TServer server = new TThreadPoolServer(processorFactory, serverTransport);
|
|---|
| 43 | LOGGER.info("Server started");
|
|---|
| 44 | server.serve();
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|