1414
1515package codeu .chat ;
1616
17+ import java .io .BufferedReader ;
18+ import java .io .FileReader ;
1719import java .io .IOException ;
1820
21+ import codeu .chat .common .Secret ;
22+ import codeu .chat .common .Uuid ;
23+ import codeu .chat .common .Uuids ;
1924import codeu .chat .relay .Server ;
2025import codeu .chat .relay .ServerFrontEnd ;
2126import 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}
0 commit comments