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

Commit df0f038

Browse files
committed
Load Team Info into Relay
There was a TODO in the relay server to load team information so that the relay would be able to accept read and write requests from servers. This change adds the ability to read a file that can contain the team id and team secrets that it will accept. Closes: #9
1 parent 851e5cd commit df0f038

3 files changed

Lines changed: 60 additions & 9 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: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414

1515
package codeu.chat;
1616

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

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

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

52+
final Server relay = new Server(1024, 16);
53+
54+
LOG.info("Relay object created.");
55+
56+
LOG.info("Loading team data...");
57+
58+
loadTeamInfo(relay, args[1]);
59+
60+
LOG.info("Done loading team data.");
61+
4762
LOG.info("Starting relay...");
4863

49-
startRelay(source);
64+
startRelay(relay, source);
5065

5166
} catch (IOException ex) {
5267
LOG.error(ex, "Failed to establish server accept port");
5368
}
5469
}
5570

56-
private static void startRelay(ConnectionSource source) {
57-
58-
final Server relay = new Server(1024, 16);
59-
60-
LOG.info("Relay object created.");
61-
62-
// TODO: Load team information
71+
private static void startRelay(Server relay, ConnectionSource source) {
6372

6473
final ServerFrontEnd frontEnd = new ServerFrontEnd(relay);
6574

@@ -91,4 +100,41 @@ public void onException(Exception ex) {
91100

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

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)