-
Notifications
You must be signed in to change notification settings - Fork 17
/
SmppClientOpsThread.java
456 lines (376 loc) · 15.8 KB
/
SmppClientOpsThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* TeleStax, Open Source Cloud Communications
* Copyright 2012, Telestax Inc and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.restcomm.smpp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import javolution.util.FastList;
import javolution.util.FastMap;
import org.apache.log4j.Logger;
import com.cloudhopper.smpp.PduAsyncResponse;
import com.cloudhopper.smpp.SmppSession;
import com.cloudhopper.smpp.SmppSession.Type;
import com.cloudhopper.smpp.SmppSessionConfiguration;
import com.cloudhopper.smpp.SmppSessionHandler;
import com.cloudhopper.smpp.impl.DefaultSmppClient;
import com.cloudhopper.smpp.impl.DefaultSmppSession;
import com.cloudhopper.smpp.pdu.EnquireLink;
import com.cloudhopper.smpp.pdu.PduRequest;
import com.cloudhopper.smpp.pdu.PduResponse;
import com.cloudhopper.smpp.ssl.SslConfiguration;
import com.cloudhopper.smpp.type.Address;
import com.cloudhopper.smpp.type.RecoverablePduException;
import com.cloudhopper.smpp.type.UnrecoverablePduException;
/**
* @author Amit Bhayani
*
*/
public class SmppClientOpsThread implements Runnable {
private static final Logger logger = Logger.getLogger(SmppClientOpsThread.class);
private static final long SCHEDULE_CONNECT_DELAY = 1000 * 30; // 30 sec
private final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
protected volatile boolean started = true;
ConcurrentLinkedQueue<ChangeRequest> workingSet = new ConcurrentLinkedQueue<ChangeRequest>();
ConcurrentLinkedQueue<ChangeRequest> futureSet = new ConcurrentLinkedQueue<ChangeRequest>();
private Object waitObject = new Object();
private final DefaultSmppClient clientBootstrap;
private final SmppSessionHandlerInterface smppSessionHandlerInterface;
private final EsmeManagement esmeManagement;
/**
*
*/
public SmppClientOpsThread(DefaultSmppClient clientBootstrap,
SmppSessionHandlerInterface smppSessionHandlerInterface, EsmeManagement esmeManagement) {
this.clientBootstrap = clientBootstrap;
this.smppSessionHandlerInterface = smppSessionHandlerInterface;
this.esmeManagement = esmeManagement;
}
/**
* @param started
* the started to set
*/
protected void setStarted(boolean started) {
this.started = started;
synchronized (this.waitObject) {
this.waitObject.notify();
}
}
protected void scheduleConnect(Esme esme) {
logger.debug("Scheduling a Client SMPP connection for ESME: " + esme.getName()
+ " systemId=" + esme.getSystemId());
long executionTime = System.currentTimeMillis() + SCHEDULE_CONNECT_DELAY;
if (esme.getInConnectingQueue().compareAndSet(false, true)) {
this.futureSet.offer(new ChangeRequest(esme, ChangeRequest.CONNECT, executionTime));
logger.debug("Pending change request CONNECT has been added for esme: " + esme.getName()
+ " with scheduled execution time on " + DATE_FORMAT.format(new Date(executionTime)));
} else {
logger.debug("Pending change request CONNECT has NOT been added for esme: " + esme.getName()
+ " because it is already in queue");
}
synchronized (this.waitObject) {
this.waitObject.notify();
}
}
protected void scheduleEnquireLink(Esme esme) {
this.futureSet.offer(new ChangeRequest(esme, ChangeRequest.ENQUIRE_LINK, System.currentTimeMillis()
+ esme.getEnquireLinkDelay()));
synchronized (this.waitObject) {
this.waitObject.notify();
}
}
@Override
public void run() {
FastMap<String, Long> startedClosedTime = new FastMap<String, Long>();
if (logger.isInfoEnabled()) {
logger.info("SmppClientOpsThread started.");
}
while (this.started) {
workingSet = futureSet;
futureSet = new ConcurrentLinkedQueue<ChangeRequest>();
FastList<Esme> pendingList = new FastList<Esme>();
ChangeRequest change = workingSet.poll();
while (change != null) {
switch (change.getType()) {
case ChangeRequest.CONNECT:
if (!change.getEsme().isStarted()) {
change.getEsme().getInConnectingQueue().set(false);
logger.warn("ESME " + change.getEsme().getName() + " is stopped. Removing change request.");
} else {
if (change.getExecutionTime() <= System.currentTimeMillis()) {
change.getEsme().getInConnectingQueue().set(false);
initiateConnection(change.getEsme());
} else {
futureSet.offer(change);
logger.debug("Change request for ESME " + change.getEsme().getName() + " is scheduled for later: "
+ DATE_FORMAT.format(new Date(change.getExecutionTime())));
}
}
break;
case ChangeRequest.ENQUIRE_LINK:
if (change.getEsme().isStarted()) {
if (change.getEsme().getEnquireClientEnabled()) {
if (change.getExecutionTime() <= System.currentTimeMillis()) {
pendingList.add(change.getEsme());
} else {
futureSet.offer(change);
}
}
}
break;
}
change = workingSet.poll();
}
// Sending Enquire messages
Iterator<Esme> pendingchanges = pendingList.iterator();
while (pendingchanges.hasNext()) {
Esme esme = pendingchanges.next();
this.enquireLink(esme);
}
try {
synchronized (this.waitObject) {
this.waitObject.wait(5000);
}
// checking of ESME CLOSED state - TODO: we need to refactor it after finding a reason of ESME not connecting
try {
long curTimeStamp = System.currentTimeMillis();
for (FastList.Node<Esme> n = this.esmeManagement.esmes.head(), end = this.esmeManagement.esmes.tail(); (n = n
.getNext()) != end;) {
Esme esme = n.getValue();
if (esme.getSmppSessionType() == Type.CLIENT) {
if (esme.isStarted() && esme.isClosed()) {
Long stTime = startedClosedTime.get(esme.getName());
if (stTime == null) {
startedClosedTime.put(esme.getName(), curTimeStamp);
} else {
long stTimeV = stTime;
// checking if a disconnection time > 5 min == 300 sec
if (curTimeStamp - stTimeV > 300000) {
startedClosedTime.remove(esme.getName());
logger.warn("Client ESME is not connected for 5 minutes :" + esme.getName());
}
}
} else {
startedClosedTime.remove(esme.getName());
}
}
}
} catch (Throwable e) {
}
} catch (InterruptedException e) {
logger.error("Error while looping SmppClientOpsThread thread", e);
}
}// while
if (logger.isInfoEnabled()) {
logger.info("SmppClientOpsThread for stopped.");
}
}
private void enquireLink(Esme esme) {
SmppSession smppSession = esme.getSmppSession();
if (!esme.isStarted()) {
return;
}
if (smppSession != null && smppSession.isBound()) {
try {
smppSession.enquireLink(new EnquireLink(), 10000);
// all ok lets schedule another ENQUIRE_LINK
this.scheduleEnquireLink(esme);
return;
} catch (RecoverablePduException e) {
logger.warn(String.format("RecoverablePduException while sending the ENQURE_LINK for ESME SystemId=%s",
esme.getSystemId()), e);
// Recoverabel exception is ok
// all ok lets schedule another ENQUIRE_LINK
this.scheduleEnquireLink(esme);
return;
} catch (Exception e) {
logger.error(
String.format("Exception while trying to send ENQUIRE_LINK for ESME SystemId=%s",
esme.getSystemId()), e);
// For all other exceptions lets close session and re-try
// connect
try {
smppSession.close();
} catch (Exception ex) {
logger.error(String.format("Failed to close smpp client session for %s.",
smppSession.getConfiguration().getName()));
}
this.scheduleConnect(esme);
}
} else {
// This should never happen
logger.warn(String.format("Sending ENQURE_LINK failed for ESME SystemId=%s as SmppSession is =%s !",
esme.getSystemId(), (smppSession == null ? null : smppSession.getStateName())));
if (smppSession != null) {
try {
smppSession.close();
} catch (Exception e) {
logger.error(String.format("Failed to close smpp client session for %s.",
smppSession.getConfiguration().getName()));
}
}
this.scheduleConnect(esme);
}
}
private void initiateConnection(Esme esme) {
logger.debug("Initiating connection for esme " + esme.getName() + " is started.");
// If Esme is stopped, don't try to initiate connect
if (!esme.isStarted()) {
logger.warn("ESME: " + esme.getName() + " is stopped. Will not try to initiate connection.");
return;
}
SmppSession smppSession = esme.getSmppSession();
if ((smppSession != null && smppSession.isBound()) || (smppSession != null && smppSession.isBinding())) {
// If process has already begun lets not do it again
logger.debug("SMPP session is already bound or binding for ESME: " + esme.getName() + ". Will not try to initiate connection");
return;
}
SmppSession session0 = null;
try {
logger.debug("Creating SMPP Session with ESME " + esme.getName() + " started.");
SmppSessionConfiguration config0 = new SmppSessionConfiguration();
config0.setWindowSize(esme.getWindowSize());
config0.setName(esme.getName());
config0.setType(esme.getSmppBindType());
config0.setBindTimeout(esme.getClientBindTimeout());
config0.setHost(esme.getHost());
config0.setPort(esme.getPort());
config0.setConnectTimeout(esme.getConnectTimeout());
config0.setSystemId(esme.getSystemId());
config0.setPassword(esme.getPassword());
config0.setSystemType(esme.getSystemType());
config0.getLoggingOptions().setLogBytes(true);
// to enable monitoring (request expiration)
config0.setRequestExpiryTimeout(esme.getRequestExpiryTimeout());
config0.setWindowMonitorInterval(esme.getWindowMonitorInterval());
config0.setCountersEnabled(esme.isCountersEnabled());
config0.setWriteTimeout(SmppManagement.getInstance().getSmppServerManagement().getWriteTimeout());
int addressTon = esme.getEsmeTon();
int addressNpi = esme.getEsmeNpi();
String addressRange = esme.getEsmeAddressRange();
Address addressRangeObj = new Address();
if(addressTon!=-1){
addressRangeObj.setTon((byte)addressTon);
}
if(addressNpi != -1){
addressRangeObj.setNpi((byte)addressNpi);
}
if(addressRange != null){
addressRangeObj.setAddress(addressRange);
}
config0.setAddressRange(addressRangeObj);
SmppSessionHandler sessionHandler = new ClientSmppSessionHandler(esme,
this.smppSessionHandlerInterface.createNewSmppSessionHandler(esme));
// SSL settings
if (esme.isUseSsl()) {
logger.info(String.format("%s ESME will use SSL Configuration", esme.getName()));
SslConfiguration sslConfiguration = esme.getWrappedSslConfig();
config0.setUseSsl(true);
config0.setSslConfiguration(sslConfiguration);
}
logger.debug("Binding with ESME " + esme.getName() + " systemId=" + esme.getSystemId());
session0 = clientBootstrap.bind(config0, sessionHandler);
// Set in ESME
logger.debug("SMPP session has been created for ESME: " + esme.getName());
esme.setSmppSession((DefaultSmppSession) session0);
// Finally set Enquire Link schedule
this.scheduleEnquireLink(esme);
} catch (Throwable e) {
logger.error(
String.format("Exception when trying to bind client SMPP connection for ESME systemId=%s",
esme.getSystemId()) + " name = " + esme.getName(), e);
if (session0 != null) {
session0.close();
}
this.scheduleConnect(esme);
}
}
protected class ClientSmppSessionHandler implements SmppSessionHandler {
private final Esme esme;
private final SmppSessionHandler wrappedSmppSessionHandler;
/**
* @param esme
*/
public ClientSmppSessionHandler(Esme esme, SmppSessionHandler wrappedSmppSessionHandler) {
super();
this.esme = esme;
this.wrappedSmppSessionHandler = wrappedSmppSessionHandler;
}
@Override
public String lookupResultMessage(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public String lookupTlvTagName(short arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void fireChannelUnexpectedlyClosed() {
this.wrappedSmppSessionHandler.fireChannelUnexpectedlyClosed();
if (this.esme.getSmppSession() != null) {
this.esme.getSmppSession().close();
}
// Schedule the connection again
scheduleConnect(this.esme);
}
@Override
public void fireExpectedPduResponseReceived(PduAsyncResponse pduAsyncResponse) {
this.wrappedSmppSessionHandler.fireExpectedPduResponseReceived(pduAsyncResponse);
}
@Override
public void firePduRequestExpired(PduRequest pduRequest) {
this.wrappedSmppSessionHandler.firePduRequestExpired(pduRequest);
}
@Override
public PduResponse firePduRequestReceived(PduRequest pduRequest) {
return this.wrappedSmppSessionHandler.firePduRequestReceived(pduRequest);
}
@Override
public void fireRecoverablePduException(RecoverablePduException e) {
this.wrappedSmppSessionHandler.fireRecoverablePduException(e);
}
@Override
public void fireUnexpectedPduResponseReceived(PduResponse pduResponse) {
this.wrappedSmppSessionHandler.fireUnexpectedPduResponseReceived(pduResponse);
}
@Override
public void fireUnknownThrowable(Throwable e) {
this.wrappedSmppSessionHandler.fireUnknownThrowable(e);
// TODO is this ok?
if (this.esme.getSmppSession() != null) {
this.esme.getSmppSession().close();
}
// Schedule the connection again
scheduleConnect(this.esme);
}
@Override
public void fireUnrecoverablePduException(UnrecoverablePduException e) {
// TODO shall we call wrapped?
this.wrappedSmppSessionHandler.fireUnrecoverablePduException(e);
this.esme.getSmppSession().close();
// Schedule the connection again
scheduleConnect(this.esme);
}
}
}