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

Commit 1eb0540

Browse files
authored
Merge branch 'master' into relay-load-team-data
2 parents df0f038 + 9613ca8 commit 1eb0540

5 files changed

Lines changed: 303 additions & 219 deletions

File tree

src/codeu/chat/RelayMain.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
import java.io.FileReader;
1919
import java.io.IOException;
2020

21-
import codeu.chat.common.Hub;
2221
import codeu.chat.common.Secret;
2322
import codeu.chat.common.Uuid;
2423
import codeu.chat.common.Uuids;
2524
import codeu.chat.relay.Server;
2625
import codeu.chat.relay.ServerFrontEnd;
2726
import codeu.chat.util.Logger;
27+
import codeu.chat.util.Timeline;
2828
import codeu.chat.util.connections.Connection;
2929
import codeu.chat.util.connections.ConnectionSource;
3030
import codeu.chat.util.connections.ServerConnectionSource;
@@ -49,6 +49,8 @@ public static void main(String[] args) {
4949

5050
try (final ConnectionSource source = ServerConnectionSource.forPort(myPort)) {
5151

52+
// Limit the number of messages that the server tracks to be 1024 and limit the
53+
// max number of messages that the relay will send out to be 16.
5254
final Server relay = new Server(1024, 16);
5355

5456
LOG.info("Relay object created.");
@@ -69,36 +71,37 @@ public static void main(String[] args) {
6971
}
7072

7173
private static void startRelay(Server relay, ConnectionSource source) {
72-
74+
7375
final ServerFrontEnd frontEnd = new ServerFrontEnd(relay);
74-
7576
LOG.info("Relay front end object created.");
7677

77-
LOG.info("Starting relay main loop...");
78-
79-
final Runnable hub = new Hub(source, new Hub.Handler() {
80-
81-
@Override
82-
public void handle(Connection connection) throws Exception {
83-
84-
frontEnd.handleConnection(connection);
85-
86-
}
78+
final Timeline timeline = new Timeline();
79+
LOG.info("Relay timeline created.");
8780

88-
@Override
89-
public void onException(Exception ex) {
81+
LOG.info("Starting relay main loop...");
9082

91-
System.out.println("ERROR: front end failed to handle connection. Check log for details.");
92-
LOG.error(ex, "Exception handling connection.");
83+
while (true) {
84+
try {
85+
86+
LOG.info("Establishing connection...");
87+
final Connection connection = source.connect();
88+
LOG.info("Connection established.");
89+
90+
timeline.scheduleNow(new Runnable() {
91+
@Override
92+
public void run() {
93+
try {
94+
frontEnd.handleConnection(connection);
95+
} catch (Exception ex) {
96+
LOG.error(ex, "Exception handling connection.");
97+
}
98+
}
99+
});
93100

101+
} catch (IOException ex) {
102+
LOG.error(ex, "Failed to establish connection.");
94103
}
95-
});
96-
97-
LOG.info("Starting hub...");
98-
99-
hub.run();
100-
101-
LOG.info("Hub exited.");
104+
}
102105
}
103106

104107
private static void loadTeamInfo(Server relay, String file) {

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)