4
4
import com .acuity .iot .dsa .dslink .protocol .responder .DSResponder ;
5
5
import com .acuity .iot .dsa .dslink .transport .DSTransport ;
6
6
import java .io .IOException ;
7
- import java .util .LinkedList ;
8
- import java .util .List ;
7
+ import java .util .concurrent .ConcurrentLinkedQueue ;
9
8
import org .iot .dsa .conn .DSConnection ;
10
9
import org .iot .dsa .conn .DSIConnected ;
11
10
import org .iot .dsa .dslink .DSIRequester ;
@@ -49,8 +48,8 @@ public abstract class DSSession extends DSNode implements DSIConnected {
49
48
private int messageId = 0 ;
50
49
private int nextMessage = 1 ;
51
50
private final Object outgoingMutex = new Object ();
52
- private List <OutboundMessage > outgoingRequests = new LinkedList <OutboundMessage >();
53
- private List <OutboundMessage > outgoingResponses = new LinkedList <OutboundMessage >();
51
+ private ConcurrentLinkedQueue <OutboundMessage > outgoingRequests = new ConcurrentLinkedQueue <OutboundMessage >();
52
+ private ConcurrentLinkedQueue <OutboundMessage > outgoingResponses = new ConcurrentLinkedQueue <OutboundMessage >();
54
53
private DSInfo requesterAllowed = getInfo (REQUESTER_ALLOWED );
55
54
private ReadThread readThread ;
56
55
private WriteThread writeThread ;
@@ -78,10 +77,8 @@ public void enqueueOutgoingRequest(OutboundMessage arg) {
78
77
if (!isRequesterAllowed ()) {
79
78
throw new IllegalStateException ("Requester not allowed" );
80
79
}
81
- synchronized (outgoingMutex ) {
82
- outgoingRequests .add (arg );
83
- outgoingMutex .notify ();
84
- }
80
+ outgoingRequests .add (arg );
81
+ notifyOutgoing ();
85
82
}
86
83
}
87
84
@@ -90,10 +87,8 @@ public void enqueueOutgoingRequest(OutboundMessage arg) {
90
87
*/
91
88
public void enqueueOutgoingResponse (OutboundMessage arg ) {
92
89
if (connected ) {
93
- synchronized (outgoingMutex ) {
94
- outgoingResponses .add (arg );
95
- outgoingMutex .notify ();
96
- }
90
+ outgoingResponses .add (arg );
91
+ notifyOutgoing ();
97
92
}
98
93
}
99
94
@@ -175,24 +170,14 @@ protected void declareDefaults() {
175
170
* Can return null.
176
171
*/
177
172
protected OutboundMessage dequeueOutgoingRequest () {
178
- synchronized (outgoingMutex ) {
179
- if (!outgoingRequests .isEmpty ()) {
180
- return outgoingRequests .remove (0 );
181
- }
182
- }
183
- return null ;
173
+ return outgoingRequests .poll ();
184
174
}
185
175
186
176
/**
187
177
* Can return null.
188
178
*/
189
179
protected OutboundMessage dequeueOutgoingResponse () {
190
- synchronized (outgoingMutex ) {
191
- if (!outgoingResponses .isEmpty ()) {
192
- return outgoingResponses .remove (0 );
193
- }
194
- }
195
- return null ;
180
+ return outgoingResponses .poll ();
196
181
}
197
182
198
183
/**
@@ -247,10 +232,18 @@ protected boolean hasSomethingToSend() {
247
232
return false ;
248
233
}
249
234
if (!outgoingResponses .isEmpty ()) {
250
- return true ;
235
+ for (OutboundMessage msg : outgoingResponses ) {
236
+ if (msg .canWrite (this )) {
237
+ return true ;
238
+ }
239
+ }
251
240
}
252
241
if (!outgoingRequests .isEmpty ()) {
253
- return true ;
242
+ for (OutboundMessage msg : outgoingRequests ) {
243
+ if (msg .canWrite (this )) {
244
+ return true ;
245
+ }
246
+ }
254
247
}
255
248
return false ;
256
249
}
@@ -289,11 +282,9 @@ protected void onConnected() {
289
282
* Clear the outgoing queues and waits for the the read and write threads to exit.
290
283
*/
291
284
protected void onDisconnected () {
292
- synchronized (outgoingMutex ) {
293
- outgoingRequests .clear ();
294
- outgoingResponses .clear ();
295
- outgoingMutex .notifyAll ();
296
- }
285
+ outgoingRequests .clear ();
286
+ outgoingResponses .clear ();
287
+ notifyOutgoing ();
297
288
try {
298
289
readThread .join ();
299
290
} catch (Exception x ) {
@@ -322,15 +313,11 @@ protected void onDisconnecting() {
322
313
}
323
314
324
315
protected void requeueOutgoingRequest (OutboundMessage arg ) {
325
- synchronized (outgoingMutex ) {
326
- outgoingRequests .add (arg );
327
- }
316
+ outgoingRequests .add (arg );
328
317
}
329
318
330
319
protected void requeueOutgoingResponse (OutboundMessage arg ) {
331
- synchronized (outgoingMutex ) {
332
- outgoingResponses .add (arg );
333
- }
320
+ outgoingResponses .add (arg );
334
321
}
335
322
336
323
/**
@@ -396,7 +383,8 @@ private void waitForAcks(long timeout) {
396
383
warn (getPath (), x );
397
384
}
398
385
if ((System .currentTimeMillis () - start ) > timeout ) {
399
- debug (debug () ? String .format ("witForAcks timeout (%s / %s)" ,ackRcvd ,messageId )
386
+ debug (debug () ? String
387
+ .format ("waitForAcks timeout (%s / %s)" , ackRcvd , messageId )
400
388
: null );
401
389
break ;
402
390
}
0 commit comments