@@ -18,18 +18,22 @@ public class HubConnection {
18
18
private Boolean handshakeReceived = false ;
19
19
private static final String RECORD_SEPARATOR = "\u001e " ;
20
20
private HubConnectionState connectionState = HubConnectionState .DISCONNECTED ;
21
+ private Logger logger ;
21
22
22
- public HubConnection (String url , Transport transport ) {
23
+ public HubConnection (String url , Transport transport , Logger logger ) {
23
24
this .url = url ;
24
25
this .protocol = new JsonHubProtocol ();
26
+ this .logger = logger ;
25
27
this .callback = (payload ) -> {
26
28
27
29
if (!handshakeReceived ) {
28
30
int handshakeLength = payload .indexOf (RECORD_SEPARATOR ) + 1 ;
29
31
String handshakeResponseString = payload .substring (0 , handshakeLength - 1 );
30
32
HandshakeResponseMessage handshakeResponse = HandshakeProtocol .parseHandshakeResponse (handshakeResponseString );
31
33
if (handshakeResponse .error != null ) {
32
- throw new Exception ("Error in handshake " + handshakeResponse .error );
34
+ String errorMessage = "Error in handshake " + handshakeResponse .error ;
35
+ logger .log (LogLevel .Error , errorMessage );
36
+ throw new Exception (errorMessage );
33
37
}
34
38
handshakeReceived = true ;
35
39
@@ -43,36 +47,41 @@ public HubConnection(String url, Transport transport) {
43
47
HubMessage [] messages = protocol .parseMessages (payload );
44
48
45
49
for (HubMessage message : messages ) {
50
+ logger .log (LogLevel .Debug ,"Received message of type %s" , message .getMessageType ());
46
51
switch (message .getMessageType ()) {
47
52
case INVOCATION :
48
53
InvocationMessage invocationMessage = (InvocationMessage )message ;
49
- if (message != null && handlers .containsKey (invocationMessage .target )) {
54
+ if (handlers .containsKey (invocationMessage .target )) {
50
55
ArrayList <Object > args = gson .fromJson ((JsonArray )invocationMessage .arguments [0 ], (new ArrayList <>()).getClass ());
51
56
List <ActionBase > actions = handlers .get (invocationMessage .target );
52
57
if (actions != null ) {
58
+ logger .log (LogLevel .Debug , "Invoking handlers for target %s" , invocationMessage .target );
53
59
for (ActionBase action : actions ) {
54
60
action .invoke (args .toArray ());
55
61
}
56
62
}
63
+ } else {
64
+ logger .log (LogLevel .Warning , "Failed to find handler for %s method" , invocationMessage .target );
57
65
}
58
66
break ;
59
67
case STREAM_INVOCATION :
60
68
case STREAM_ITEM :
61
69
case CLOSE :
62
70
case CANCEL_INVOCATION :
63
71
case COMPLETION :
64
- throw new UnsupportedOperationException ("The message type " + message .getMessageType () + " is not supported yet." );
72
+ logger .log (LogLevel .Error , "This client does not support %s messages" , message .getMessageType ());
73
+
74
+ throw new UnsupportedOperationException (String .format ("The message type %s is not supported yet." , message .getMessageType ()));
65
75
case PING :
66
76
// We don't need to do anything in the case of a ping message.
67
- // The other message types aren't supported
68
77
break ;
69
78
}
70
79
}
71
80
};
72
81
73
82
if (transport == null ){
74
83
try {
75
- this .transport = new WebSocketTransport (this .url );
84
+ this .transport = new WebSocketTransport (this .url , this . logger );
76
85
} catch (URISyntaxException e ) {
77
86
e .printStackTrace ();
78
87
}
@@ -81,42 +90,57 @@ public HubConnection(String url, Transport transport) {
81
90
}
82
91
}
83
92
93
+ public HubConnection (String url , Transport transport ) {
94
+ this (url , transport , new NullLogger ());
95
+ }
96
+
84
97
public HubConnection (String url ) {
85
- this (url , null );
98
+ this (url , null , new NullLogger ());
99
+ }
100
+
101
+ public HubConnection (String url , LogLevel logLevel ){
102
+ this (url , null , new ConsoleLogger (logLevel ));
86
103
}
87
104
88
105
public HubConnectionState getConnectionState () {
89
106
return connectionState ;
90
107
}
91
108
92
109
public void start () throws Exception {
110
+ logger .log (LogLevel .Debug , "Starting HubConnection" );
93
111
transport .setOnReceive (this .callback );
94
112
transport .start ();
95
113
String handshake = HandshakeProtocol .createHandshakeRequestMessage (new HandshakeRequestMessage (protocol .getName (), protocol .getVersion ()));
96
114
transport .send (handshake );
97
115
connectionState = HubConnectionState .CONNECTED ;
116
+ logger .log (LogLevel .Information , "HubConnected started" );
98
117
}
99
118
100
119
public void stop (){
120
+ logger .log (LogLevel .Debug , "Stopping HubConnection" );
101
121
transport .stop ();
102
122
connectionState = HubConnectionState .DISCONNECTED ;
123
+ logger .log (LogLevel .Information , "HubConnection stopped" );
103
124
}
104
125
105
126
public void send (String method , Object ... args ) throws Exception {
106
127
InvocationMessage invocationMessage = new InvocationMessage (method , args );
107
128
String message = protocol .writeMessage (invocationMessage );
129
+ logger .log (LogLevel .Debug , "Sending message" );
108
130
transport .send (message );
109
131
}
110
132
111
133
public Subscription on (String target , Action callback ) {
112
134
ActionBase action = args -> callback .invoke ();
113
135
handlers .put (target , action );
136
+ logger .log (LogLevel .Trace , "Registering handler for client method: %s" , target );
114
137
return new Subscription (handlers , action , target );
115
138
}
116
139
117
140
public <T1 > Subscription on (String target , Action1 <T1 > callback , Class <T1 > param1 ) {
118
141
ActionBase action = params -> callback .invoke (param1 .cast (params [0 ]));
119
142
handlers .put (target , action );
143
+ logger .log (LogLevel .Trace , "Registering handler for client method: %s" , target );
120
144
return new Subscription (handlers , action , target );
121
145
}
122
146
@@ -125,6 +149,7 @@ public <T1, T2> Subscription on(String target, Action2<T1, T2> callback, Class<T
125
149
callback .invoke (param1 .cast (params [0 ]), param2 .cast (params [1 ]));
126
150
};
127
151
handlers .put (target , action );
152
+ logger .log (LogLevel .Trace , "Registering handler for client method: %s" , target );
128
153
return new Subscription (handlers , action , target );
129
154
}
130
155
@@ -134,6 +159,7 @@ public <T1, T2, T3> Subscription on(String target, Action3<T1, T2, T3> callback,
134
159
callback .invoke (param1 .cast (params [0 ]), param2 .cast (params [1 ]), param3 .cast (params [2 ]));
135
160
};
136
161
handlers .put (target , action );
162
+ logger .log (LogLevel .Trace , "Registering handler for client method: %s" , target );
137
163
return new Subscription (handlers , action , target );
138
164
}
139
165
@@ -143,6 +169,7 @@ public <T1, T2, T3, T4> Subscription on(String target, Action4<T1, T2, T3, T4> c
143
169
callback .invoke (param1 .cast (params [0 ]), param2 .cast (params [1 ]), param3 .cast (params [2 ]), param4 .cast (params [3 ]));
144
170
};
145
171
handlers .put (target , action );
172
+ logger .log (LogLevel .Trace , "Registering handler for client method: %s" , target );
146
173
return new Subscription (handlers , action , target );
147
174
}
148
175
@@ -153,6 +180,7 @@ public <T1, T2, T3, T4, T5> Subscription on(String target, Action5<T1, T2, T3, T
153
180
param5 .cast (params [4 ]));
154
181
};
155
182
handlers .put (target , action );
183
+ logger .log (LogLevel .Trace , "Registering handler for client method: %s" , target );
156
184
return new Subscription (handlers , action , target );
157
185
}
158
186
@@ -163,6 +191,7 @@ public <T1, T2, T3, T4, T5, T6> Subscription on(String target, Action6<T1, T2, T
163
191
param5 .cast (params [4 ]) ,param6 .cast (params [5 ]));
164
192
};
165
193
handlers .put (target , action );
194
+ logger .log (LogLevel .Trace , "Registering handler for client method: %s" , target );
166
195
return new Subscription (handlers , action , target );
167
196
}
168
197
@@ -173,6 +202,7 @@ public <T1, T2, T3, T4, T5, T6, T7> Subscription on(String target, Action7<T1, T
173
202
param5 .cast (params [4 ]) ,param6 .cast (params [5 ]), param7 .cast (params [6 ]));
174
203
};
175
204
handlers .put (target , action );
205
+ logger .log (LogLevel .Trace , "Registering handler for client method: %s" , target );
176
206
return new Subscription (handlers , action , target );
177
207
}
178
208
@@ -183,10 +213,12 @@ public <T1, T2, T3, T4, T5, T6, T7, T8> Subscription on(String target, Action8<T
183
213
param5 .cast (params [4 ]) ,param6 .cast (params [5 ]), param7 .cast (params [6 ]), param8 .cast (params [7 ]));
184
214
};
185
215
handlers .put (target , action );
216
+ logger .log (LogLevel .Trace , "Registering handler for client method: %s" , target );
186
217
return new Subscription (handlers , action , target );
187
218
}
188
219
189
220
public void remove (String name ) {
190
221
handlers .remove (name );
222
+ logger .log (LogLevel .Trace , "Removing handlers for client method %s" , name );
191
223
}
192
224
}
0 commit comments