|
16 | 16 |
|
17 | 17 | import java.io.IOException; |
18 | 18 |
|
19 | | -import codeu.chat.common.Hub; |
20 | 19 | import codeu.chat.relay.Server; |
21 | 20 | import codeu.chat.relay.ServerFrontEnd; |
22 | 21 | import codeu.chat.util.Logger; |
| 22 | +import codeu.chat.util.Timeline; |
23 | 23 | import codeu.chat.util.connections.Connection; |
24 | 24 | import codeu.chat.util.connections.ConnectionSource; |
25 | 25 | import codeu.chat.util.connections.ServerConnectionSource; |
@@ -56,39 +56,39 @@ public static void main(String[] args) { |
56 | 56 | private static void startRelay(ConnectionSource source) { |
57 | 57 |
|
58 | 58 | final Server relay = new Server(1024, 16); |
59 | | - |
60 | 59 | LOG.info("Relay object created."); |
61 | 60 |
|
62 | | - // TODO: Load team information |
63 | | - |
64 | 61 | final ServerFrontEnd frontEnd = new ServerFrontEnd(relay); |
65 | | - |
66 | 62 | LOG.info("Relay front end object created."); |
67 | 63 |
|
68 | | - LOG.info("Starting relay main loop..."); |
69 | | - |
70 | | - final Runnable hub = new Hub(source, new Hub.Handler() { |
71 | | - |
72 | | - @Override |
73 | | - public void handle(Connection connection) throws Exception { |
74 | | - |
75 | | - frontEnd.handleConnection(connection); |
| 64 | + final Timeline timeline = new Timeline(); |
| 65 | + LOG.info("Relay timeline created."); |
76 | 66 |
|
77 | | - } |
78 | | - |
79 | | - @Override |
80 | | - public void onException(Exception ex) { |
| 67 | + // TODO: Load team information |
81 | 68 |
|
82 | | - System.out.println("ERROR: front end failed to handle connection. Check log for details."); |
83 | | - LOG.error(ex, "Exception handling connection."); |
| 69 | + LOG.info("Starting relay main loop..."); |
84 | 70 |
|
| 71 | + while (true) { |
| 72 | + try { |
| 73 | + |
| 74 | + LOG.info("Established connection..."); |
| 75 | + final Connection connection = source.connect(); |
| 76 | + LOG.info("Connection established."); |
| 77 | + |
| 78 | + timeline.scheduleNow(new Runnable() { |
| 79 | + @Override |
| 80 | + public void run() { |
| 81 | + try { |
| 82 | + frontEnd.handleConnection(connection); |
| 83 | + } catch (Exception ex) { |
| 84 | + LOG.error(ex, "Exception handling connection."); |
| 85 | + } |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + } catch (IOException ex) { |
| 90 | + LOG.error(ex, "Failed to establish connection."); |
85 | 91 | } |
86 | | - }); |
87 | | - |
88 | | - LOG.info("Starting hub..."); |
89 | | - |
90 | | - hub.run(); |
91 | | - |
92 | | - LOG.info("Hub exited."); |
| 92 | + } |
93 | 93 | } |
94 | 94 | } |
0 commit comments