1818import java .io .FileReader ;
1919import java .io .IOException ;
2020
21- import codeu .chat .common .Hub ;
2221import codeu .chat .common .Secret ;
2322import codeu .chat .common .Uuid ;
2423import codeu .chat .common .Uuids ;
2524import codeu .chat .relay .Server ;
2625import codeu .chat .relay .ServerFrontEnd ;
2726import codeu .chat .util .Logger ;
27+ import codeu .chat .util .Timeline ;
2828import codeu .chat .util .connections .Connection ;
2929import codeu .chat .util .connections .ConnectionSource ;
3030import 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 ) {
0 commit comments