[4940] | 1 | package com.joelpm.bidiMessages.client;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.List;
|
---|
| 5 |
|
---|
| 6 | import org.apache.thrift.TException;
|
---|
| 7 | import org.apache.thrift.protocol.TBinaryProtocol;
|
---|
| 8 | import org.apache.thrift.protocol.TProtocol;
|
---|
| 9 | import org.apache.thrift.transport.TSocket;
|
---|
| 10 | import org.apache.thrift.transport.TTransport;
|
---|
| 11 | import com.joelpm.bidiMessages.generated.Message;
|
---|
| 12 | import com.joelpm.bidiMessages.generated.MessageService;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * Client that connects to the server and handles the sending and receiving
|
---|
| 16 | * of message objects. Will also attempt to reconnect if the server disappears.
|
---|
| 17 | *
|
---|
| 18 | * @author Joel Meyer
|
---|
| 19 | */
|
---|
| 20 | public class Client implements MessageService.Iface {
|
---|
| 21 | private final ConnectionStatusMonitor connectionMonitor;
|
---|
| 22 | private final MessageSender sender;
|
---|
| 23 | private final MessageReceiver receiver;
|
---|
| 24 |
|
---|
| 25 | private final String name;
|
---|
| 26 |
|
---|
| 27 | private final TTransport transport;
|
---|
| 28 | private final TProtocol protocol;
|
---|
| 29 |
|
---|
| 30 | private final List<MessageListener> listeners;
|
---|
| 31 |
|
---|
| 32 | public Client(String name, String server, int port, MessageService.Iface messageHandler) {
|
---|
| 33 | this.name = name;
|
---|
| 34 | this.transport = new TSocket(server, port);
|
---|
| 35 | this.protocol = new TBinaryProtocol(transport);
|
---|
| 36 |
|
---|
| 37 | this.connectionMonitor = new ConnectionStatusMonitor(transport);
|
---|
| 38 |
|
---|
| 39 | this.sender = new MessageSender(protocol, connectionMonitor);
|
---|
| 40 | this.receiver = new MessageReceiver(protocol, messageHandler, connectionMonitor);
|
---|
| 41 |
|
---|
| 42 | new Thread(sender).start();
|
---|
| 43 | new Thread(receiver).start();
|
---|
| 44 |
|
---|
| 45 | this.connectionMonitor.tryOpen();
|
---|
| 46 |
|
---|
| 47 | this.listeners = new ArrayList<MessageListener>();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public void addListener(MessageListener listener) {
|
---|
| 51 | listeners.add(listener);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public void sendMessageToServer(String msg) {
|
---|
| 55 | sender.send(new Message(name, msg));
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @Override
|
---|
| 59 | public void sendMessage(Message msg) throws TException {
|
---|
| 60 | for (MessageListener listener : listeners) {
|
---|
| 61 | listener.messageReceived(msg);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
| 66 | * @param args
|
---|
| 67 | */
|
---|
| 68 | public static void main(String[] args) throws Exception {
|
---|
| 69 | MessageService.Iface handler = new MessageService.Iface() {
|
---|
| 70 | @Override
|
---|
| 71 | public void sendMessage(Message msg) throws TException {
|
---|
| 72 | System.out.println("Got msg: " + msg);
|
---|
| 73 | }
|
---|
| 74 | };
|
---|
| 75 |
|
---|
| 76 | Client client = new Client(args[0], args[1], Integer.parseInt(args[2]), handler);
|
---|
| 77 |
|
---|
| 78 | client.sendMessageToServer("Hello there!");
|
---|
| 79 |
|
---|
| 80 | for (int i = 0; i < 100; i++) {
|
---|
| 81 | client.sendMessageToServer(String.format("Message %s", i));
|
---|
| 82 | Thread.sleep(1000);
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|