Skip to content

Commit

Permalink
Load Team Info into Relay
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vaage committed Mar 28, 2017
1 parent 851e5cd commit df0f038
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
4 changes: 3 additions & 1 deletion run_relay.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

TEAMS_FILE="$(pwd)/teams"

cd './bin'
java codeu.chat.RelayMain "2008"
java codeu.chat.RelayMain '2008' "$TEAMS_FILE"
62 changes: 54 additions & 8 deletions src/codeu/chat/RelayMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@

package codeu.chat;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import codeu.chat.common.Hub;
import codeu.chat.common.Secret;
import codeu.chat.common.Uuid;
import codeu.chat.common.Uuids;
import codeu.chat.relay.Server;
import codeu.chat.relay.ServerFrontEnd;
import codeu.chat.util.Logger;
Expand Down Expand Up @@ -44,22 +49,26 @@ public static void main(String[] args) {

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

final Server relay = new Server(1024, 16);

LOG.info("Relay object created.");

LOG.info("Loading team data...");

loadTeamInfo(relay, args[1]);

LOG.info("Done loading team data.");

LOG.info("Starting relay...");

startRelay(source);
startRelay(relay, source);

} catch (IOException ex) {
LOG.error(ex, "Failed to establish server accept port");
}
}

private static void startRelay(ConnectionSource source) {

final Server relay = new Server(1024, 16);

LOG.info("Relay object created.");

// TODO: Load team information
private static void startRelay(Server relay, ConnectionSource source) {

final ServerFrontEnd frontEnd = new ServerFrontEnd(relay);

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

LOG.info("Hub exited.");
}

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

try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {

String line;
for (line = reader.readLine();
line != null;
line = reader.readLine()) {

line = line.trim();

if (line.startsWith("#")) {
// this is a comment, skip it
} else {

try {

final String[] tokens = line.split(":");

// There are just so many things that could go wrong when parsing
// this line that it is not worth trying to handle ahead of time.
// So instead just try to parse it and catch any exception.

final Uuid id = Uuids.fromString(tokens[0].trim());
final byte[] secret = Secret.parse(tokens[1].trim());

relay.addTeam(id, secret);
} catch (Exception ex) {
LOG.error(ex, "Skipping line \"%s\". Could not parse", line);
}
}
}
} catch (IOException ex) {
LOG.error(ex, "Failed to load team data");
}
}
}
3 changes: 3 additions & 0 deletions teams
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# sample team
# this will match the values in the run_server.sh
100.101:ABABAB

0 comments on commit df0f038

Please sign in to comment.