3535import codeu .chat .common .Uuids ;
3636import codeu .chat .util .Logger ;
3737import codeu .chat .util .Serializers ;
38+ import codeu .chat .util .Timeline ;
3839import codeu .chat .util .connections .Connection ;
3940
4041public final class Server {
4142
4243 private final static Logger .Log LOG = Logger .newLog (Server .class );
4344
45+ private final Timeline timeline = new Timeline ();
46+
4447 private final Uuid id ;
4548 private final byte [] secret ;
4649
@@ -58,20 +61,57 @@ public Server(Uuid id, byte[] secret, Relay relay) {
5861
5962 this .controller = new Controller (id , model );
6063 this .relay = relay ;
61- }
6264
63- public void syncWithRelay (int maxReadSize ) throws Exception {
64- for (final Relay .Bundle bundle : relay .read (id , secret , lastSeen , maxReadSize )) {
65- onBundle (bundle );
66- lastSeen = bundle .id ();
67- }
65+ timeline .scheduleNow (new Runnable () {
66+ @ Override
67+ public void run () {
68+ try {
69+
70+ LOG .info ("Reading update from relay..." );
71+
72+ for (final Relay .Bundle bundle : relay .read (id , secret , lastSeen , 32 )) {
73+ onBundle (bundle );
74+ lastSeen = bundle .id ();
75+ }
76+
77+ } catch (Exception ex ) {
78+
79+ LOG .error (ex , "Failed to read update from relay." );
80+
81+ }
82+
83+ // Do this all again in 60 seconds
84+ timeline .scheduleIn (60000 , this );
85+ }
86+ });
6887 }
6988
70- public boolean handleConnection (Connection connection ) throws Exception {
89+ public void handleConnection (final Connection connection ) {
90+ timeline .scheduleNow (new Runnable () {
91+ @ Override
92+ public void run () {
93+ try {
94+
95+ LOG .info ("Handling connection..." );
96+
97+ final boolean success = onMessage (
98+ connection .in (),
99+ connection .out ());
100+
101+ LOG .info ("Connection handled: %s" , success ? "ACCEPTED" : "REJECTED" );
102+ } catch (Exception ex ) {
71103
72- LOG .info ( "Handling new connection.. ." );
104+ LOG .error ( ex , "Exception while handling connection." );
73105
74- return onMessage (connection .in (), connection .out ());
106+ }
107+
108+ try {
109+ connection .close ();
110+ } catch (Exception ex ) {
111+ LOG .error (ex , "Exception while closing connection." );
112+ }
113+ }
114+ });
75115 }
76116
77117 private boolean onMessage (InputStream in , OutputStream out ) throws IOException {
@@ -89,13 +129,10 @@ private boolean onMessage(InputStream in, OutputStream out) throws IOException {
89129 Serializers .INTEGER .write (out , NetworkCode .NEW_MESSAGE_RESPONSE );
90130 Serializers .nullable (Message .SERIALIZER ).write (out , message );
91131
92- // Unlike the other calls - we need to send something the result of this
93- // call to the relay. Waiting until after the server has written back to
94- // the client allows the client to get the response, but the network
95- // connection has not been closed. However to wait after until the client-server
96- // connection was closed before sending would be very complicated.
97-
98- sendToRelay (author , conversation , message .id );
132+ timeline .scheduleNow (createSendToRelayEvent (
133+ author ,
134+ conversation ,
135+ message .id ));
99136
100137 } else if (type == NetworkCode .NEW_USER_REQUEST ) {
101138
@@ -252,16 +289,21 @@ private void onBundle(Relay.Bundle bundle) {
252289 }
253290 }
254291
255- private void sendToRelay (Uuid userId , Uuid conversationId , Uuid messageId ) {
256-
257- final User user = view .findUser (userId );
258- final Conversation conversation = view .findConversation (conversationId );
259- final Message message = view .findMessage (messageId );
260-
261- relay .write (id ,
262- secret ,
263- relay .pack (user .id , user .name , user .creation ),
264- relay .pack (conversation .id , conversation .title , conversation .creation ),
265- relay .pack (message .id , message .content , message .creation ));
292+ private Runnable createSendToRelayEvent (final Uuid userId ,
293+ final Uuid conversationId ,
294+ final Uuid messageId ) {
295+ return new Runnable () {
296+ @ Override
297+ public void run () {
298+ final User user = view .findUser (userId );
299+ final Conversation conversation = view .findConversation (conversationId );
300+ final Message message = view .findMessage (messageId );
301+ relay .write (id ,
302+ secret ,
303+ relay .pack (user .id , user .name , user .creation ),
304+ relay .pack (conversation .id , conversation .title , conversation .creation ),
305+ relay .pack (message .id , message .content , message .creation ));
306+ }
307+ };
266308 }
267309}
0 commit comments