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

Commit f3b618b

Browse files
authored
Merge pull request #10 from google/relay-load-team-data
Load Team Info into Relay
2 parents 9613ca8 + 1eb0540 commit f3b618b

3 files changed

Lines changed: 64 additions & 10 deletions

File tree

run_relay.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
TEAMS_FILE="$(pwd)/teams"
18+
1719
cd './bin'
18-
java codeu.chat.RelayMain "2008"
20+
java codeu.chat.RelayMain '2008' "$TEAMS_FILE"

src/codeu/chat/RelayMain.java

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@
1414

1515
package codeu.chat;
1616

17+
import java.io.BufferedReader;
18+
import java.io.FileReader;
1719
import java.io.IOException;
1820

21+
import codeu.chat.common.Secret;
22+
import codeu.chat.common.Uuid;
23+
import codeu.chat.common.Uuids;
1924
import codeu.chat.relay.Server;
2025
import codeu.chat.relay.ServerFrontEnd;
2126
import codeu.chat.util.Logger;
@@ -44,34 +49,41 @@ public static void main(String[] args) {
4449

4550
try (final ConnectionSource source = ServerConnectionSource.forPort(myPort)) {
4651

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.
54+
final Server relay = new Server(1024, 16);
55+
56+
LOG.info("Relay object created.");
57+
58+
LOG.info("Loading team data...");
59+
60+
loadTeamInfo(relay, args[1]);
61+
62+
LOG.info("Done loading team data.");
63+
4764
LOG.info("Starting relay...");
4865

49-
startRelay(source);
66+
startRelay(relay, source);
5067

5168
} catch (IOException ex) {
5269
LOG.error(ex, "Failed to establish server accept port");
5370
}
5471
}
5572

56-
private static void startRelay(ConnectionSource source) {
57-
58-
final Server relay = new Server(1024, 16);
59-
LOG.info("Relay object created.");
60-
73+
private static void startRelay(Server relay, ConnectionSource source) {
74+
6175
final ServerFrontEnd frontEnd = new ServerFrontEnd(relay);
6276
LOG.info("Relay front end object created.");
6377

6478
final Timeline timeline = new Timeline();
6579
LOG.info("Relay timeline created.");
6680

67-
// TODO: Load team information
68-
6981
LOG.info("Starting relay main loop...");
7082

7183
while (true) {
7284
try {
7385

74-
LOG.info("Established connection...");
86+
LOG.info("Establishing connection...");
7587
final Connection connection = source.connect();
7688
LOG.info("Connection established.");
7789

@@ -91,4 +103,41 @@ public void run() {
91103
}
92104
}
93105
}
106+
107+
private static void loadTeamInfo(Server relay, String file) {
108+
109+
try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {
110+
111+
String line;
112+
for (line = reader.readLine();
113+
line != null;
114+
line = reader.readLine()) {
115+
116+
line = line.trim();
117+
118+
if (line.startsWith("#")) {
119+
// this is a comment, skip it
120+
} else {
121+
122+
try {
123+
124+
final String[] tokens = line.split(":");
125+
126+
// There are just so many things that could go wrong when parsing
127+
// this line that it is not worth trying to handle ahead of time.
128+
// So instead just try to parse it and catch any exception.
129+
130+
final Uuid id = Uuids.fromString(tokens[0].trim());
131+
final byte[] secret = Secret.parse(tokens[1].trim());
132+
133+
relay.addTeam(id, secret);
134+
} catch (Exception ex) {
135+
LOG.error(ex, "Skipping line \"%s\". Could not parse", line);
136+
}
137+
}
138+
}
139+
} catch (IOException ex) {
140+
LOG.error(ex, "Failed to load team data");
141+
}
142+
}
94143
}

teams

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# sample team
2+
# this will match the values in the run_server.sh
3+
100.101:ABABAB

0 commit comments

Comments
 (0)