Skip to content
This repository was archived by the owner on Sep 19, 2021. It is now read-only.

Commit 9613ca8

Browse files
authored
Merge pull request #5 from google/relay-integration
Finish Integration With Relay
2 parents 851e5cd + 05b2008 commit 9613ca8

5 files changed

Lines changed: 302 additions & 221 deletions

File tree

src/codeu/chat/RelayMain.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
import java.io.IOException;
1818

19-
import codeu.chat.common.Hub;
2019
import codeu.chat.relay.Server;
2120
import codeu.chat.relay.ServerFrontEnd;
2221
import codeu.chat.util.Logger;
22+
import codeu.chat.util.Timeline;
2323
import codeu.chat.util.connections.Connection;
2424
import codeu.chat.util.connections.ConnectionSource;
2525
import codeu.chat.util.connections.ServerConnectionSource;
@@ -56,39 +56,39 @@ public static void main(String[] args) {
5656
private static void startRelay(ConnectionSource source) {
5757

5858
final Server relay = new Server(1024, 16);
59-
6059
LOG.info("Relay object created.");
6160

62-
// TODO: Load team information
63-
6461
final ServerFrontEnd frontEnd = new ServerFrontEnd(relay);
65-
6662
LOG.info("Relay front end object created.");
6763

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.");
7666

77-
}
78-
79-
@Override
80-
public void onException(Exception ex) {
67+
// TODO: Load team information
8168

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...");
8470

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.");
8591
}
86-
});
87-
88-
LOG.info("Starting hub...");
89-
90-
hub.run();
91-
92-
LOG.info("Hub exited.");
92+
}
9393
}
9494
}

src/codeu/chat/ServerMain.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.io.IOException;
1919

20-
import codeu.chat.common.Hub;
2120
import codeu.chat.common.Relay;
2221
import codeu.chat.common.Secret;
2322
import codeu.chat.common.Uuid;
@@ -83,30 +82,21 @@ private static void runServer(Uuid id,
8382

8483
final Server server = new Server(id, secret, relay);
8584

86-
LOG.info("Server object created.");
85+
LOG.info("Created server.");
8786

88-
final Runnable hub = new Hub(serverSource, new Hub.Handler() {
87+
while (true) {
8988

90-
@Override
91-
public void handle(Connection connection) throws Exception {
89+
try {
9290

93-
server.handleConnection(connection);
94-
95-
}
91+
LOG.info("Established connection...");
92+
final Connection connection = serverSource.connect();
93+
LOG.info("Connection established.");
9694

97-
@Override
98-
public void onException(Exception ex) {
99-
100-
System.out.println("ERROR: Exception during server tick. Check log for details.");
101-
LOG.error(ex, "Exception during server tick.");
95+
server.handleConnection(connection);
10296

97+
} catch (IOException ex) {
98+
LOG.error(ex, "Failed to establish connection.");
10399
}
104-
});
105-
106-
LOG.info("Starting hub...");
107-
108-
hub.run();
109-
110-
LOG.info("Hub exited.");
100+
}
111101
}
112102
}

src/codeu/chat/common/Hub.java

Lines changed: 0 additions & 147 deletions
This file was deleted.

0 commit comments

Comments
 (0)