PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/jain-slee/resources/smpp5/ra/src/main/java/org/mobicents/slee/resources/smpp/SmppResourceAdaptor.java

http://mobicents.googlecode.com/
Java | 1285 lines | 984 code | 186 blank | 115 comment | 97 complexity | 4629b7b2c492581984b99da376fb3a9d MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.slee.resources.smpp;
  23. import java.io.IOException;
  24. import java.io.Serializable;
  25. import java.net.UnknownHostException;
  26. import java.util.concurrent.ConcurrentHashMap;
  27. import java.util.concurrent.Semaphore;
  28. import java.util.concurrent.TimeUnit;
  29. import javax.slee.Address;
  30. import javax.slee.AddressPlan;
  31. import javax.slee.SLEEException;
  32. import javax.slee.facilities.AlarmFacility;
  33. import javax.slee.facilities.AlarmLevel;
  34. import javax.slee.facilities.EventLookupFacility;
  35. import javax.slee.facilities.Tracer;
  36. import javax.slee.resource.ActivityAlreadyExistsException;
  37. import javax.slee.resource.ActivityFlags;
  38. import javax.slee.resource.ActivityHandle;
  39. import javax.slee.resource.ConfigProperties;
  40. import javax.slee.resource.FailureReason;
  41. import javax.slee.resource.FireableEventType;
  42. import javax.slee.resource.InvalidConfigurationException;
  43. import javax.slee.resource.Marshaler;
  44. import javax.slee.resource.ReceivableService;
  45. import javax.slee.resource.ResourceAdaptorContext;
  46. import javax.slee.resource.SleeEndpoint;
  47. import javax.slee.resource.StartActivityException;
  48. import net.java.slee.resources.smpp.SmppTransaction;
  49. import org.mobicents.protocols.smpp.BadCommandIDException;
  50. import org.mobicents.protocols.smpp.NotBoundException;
  51. import org.mobicents.protocols.smpp.Session;
  52. import org.mobicents.protocols.smpp.message.Bind;
  53. import org.mobicents.protocols.smpp.message.BindReceiver;
  54. import org.mobicents.protocols.smpp.message.BindTransceiver;
  55. import org.mobicents.protocols.smpp.message.BindTransmitter;
  56. import org.mobicents.protocols.smpp.message.CommandId;
  57. import org.mobicents.protocols.smpp.message.EnquireLink;
  58. import org.mobicents.protocols.smpp.version.SMPPVersion;
  59. import org.mobicents.protocols.smpp.version.VersionException;
  60. import org.mobicents.slee.resource.cluster.FaultTolerantResourceAdaptor;
  61. import org.mobicents.slee.resource.cluster.FaultTolerantResourceAdaptorContext;
  62. import org.mobicents.slee.resource.cluster.MemberAddress;
  63. import org.mobicents.slee.resources.smpp.pdu.AlertNotificationImpl;
  64. import org.mobicents.slee.resources.smpp.pdu.BroadcastSMImpl;
  65. import org.mobicents.slee.resources.smpp.pdu.BroadcastSMRespImpl;
  66. import org.mobicents.slee.resources.smpp.pdu.CancelBroadcastSMImpl;
  67. import org.mobicents.slee.resources.smpp.pdu.CancelBroadcastSMRespImpl;
  68. import org.mobicents.slee.resources.smpp.pdu.CancelSMImpl;
  69. import org.mobicents.slee.resources.smpp.pdu.CancelSMRespImpl;
  70. import org.mobicents.slee.resources.smpp.pdu.DataSMImpl;
  71. import org.mobicents.slee.resources.smpp.pdu.DataSMRespImpl;
  72. import org.mobicents.slee.resources.smpp.pdu.DeliverSMImpl;
  73. import org.mobicents.slee.resources.smpp.pdu.DeliverSMRespImpl;
  74. import org.mobicents.slee.resources.smpp.pdu.GenericNackImpl;
  75. import org.mobicents.slee.resources.smpp.pdu.QueryBroadcastSMImpl;
  76. import org.mobicents.slee.resources.smpp.pdu.QueryBroadcastSMRespImpl;
  77. import org.mobicents.slee.resources.smpp.pdu.QuerySMImpl;
  78. import org.mobicents.slee.resources.smpp.pdu.QuerySMRespImpl;
  79. import org.mobicents.slee.resources.smpp.pdu.ReplaceSMImpl;
  80. import org.mobicents.slee.resources.smpp.pdu.ReplaceSMRespImpl;
  81. import org.mobicents.slee.resources.smpp.pdu.SubmitMultiImpl;
  82. import org.mobicents.slee.resources.smpp.pdu.SubmitMultiRespImpl;
  83. import org.mobicents.slee.resources.smpp.pdu.SubmitSMImpl;
  84. import org.mobicents.slee.resources.smpp.pdu.SubmitSMRespImpl;
  85. /**
  86. *
  87. * @author amit bhayani
  88. *
  89. */
  90. @SuppressWarnings("rawtypes")
  91. public class SmppResourceAdaptor implements FaultTolerantResourceAdaptor, org.mobicents.protocols.smpp.event.SessionObserver {
  92. private transient Tracer tracer;
  93. private transient ResourceAdaptorContext raContext;
  94. private transient SleeEndpoint sleeEndpoint = null;
  95. private transient EventLookupFacility eventLookup = null;
  96. private transient SmppSessionImpl smppSession = null;
  97. private transient FireableEventTypeCache eventTypeCache;
  98. private transient FireableEventTypeFilter eventTypeFilter;
  99. private transient FaultTolerantResourceAdaptorContext ftRAContext;
  100. private SmppResourceAdaptorUsageParameters usageParams;
  101. private Utils utils;
  102. private Thread linkMonitorThread;
  103. public String smscAlarm;
  104. /**
  105. * Configuration Properties
  106. */
  107. private String host = "localhost";
  108. private int port = 2727;
  109. private String systemID = "1";
  110. private String systemType = "ESME";
  111. private String password = "1";
  112. private int addressTON = 0;
  113. private int addressNPI = 1;
  114. private String addressRange = "50";
  115. private int enquireLinkTimeout = 1000 * 30;
  116. private String bindType = BindType.TRANSMITTER.toString();
  117. private boolean statsOn;
  118. private int smppResponseReceivedTimeout = 5000;
  119. private int smppResponseSentTimeout = 5000;
  120. /**
  121. * SMPP API related variables
  122. */
  123. protected org.mobicents.protocols.smpp.Session protoSmppSession = null;
  124. protected org.mobicents.protocols.smpp.util.SequenceNumberScheme seq = null;;
  125. private Semaphore semaphore = new Semaphore(0);
  126. private int bindStatus;
  127. private volatile boolean isRAStoped = false;
  128. private Bind bind = null;
  129. private long lastMessagesExchanged = 0l;
  130. private long lastStatsUpdatedTime = 0l;
  131. /**
  132. * ActivityHandle holder
  133. */
  134. private ConcurrentHashMap<SmppTransactionHandle, SmppTransactionImpl> handleVsActivityMap = new ConcurrentHashMap<SmppTransactionHandle, SmppTransactionImpl>();
  135. public SmppResourceAdaptor() {
  136. // TODO Auto-generated constructor stub
  137. }
  138. public void setHost(String host) {
  139. this.host = host;
  140. }
  141. public String getHost() {
  142. return host;
  143. }
  144. public void setPort(Integer port) {
  145. this.port = port.intValue();
  146. }
  147. public Integer getPort() {
  148. return new Integer(port);
  149. }
  150. public String getSystemId() {
  151. return systemID;
  152. }
  153. public void setSystemId(String systemID) {
  154. this.systemID = systemID;
  155. }
  156. public String getSystemType() {
  157. return systemType;
  158. }
  159. public void setSystemType(String systemType) {
  160. this.systemType = systemType;
  161. }
  162. public String getPassword() {
  163. return password;
  164. }
  165. public void setPassword(String password) {
  166. this.password = password;
  167. }
  168. public int getAddressTon() {
  169. return addressTON;
  170. }
  171. public void setAddressTon(Integer addressTON) {
  172. this.addressTON = addressTON.intValue();
  173. }
  174. public Integer getAddressNpi() {
  175. return new Integer(addressNPI);
  176. }
  177. public void setAddressNpi(Integer addressNPI) {
  178. this.addressNPI = addressNPI.intValue();
  179. }
  180. public String getAddressRange() {
  181. return addressRange;
  182. }
  183. public void setAddressRange(String addressRange) {
  184. this.addressRange = addressRange;
  185. }
  186. public Integer getEnquireLinkTimeout() {
  187. return this.enquireLinkTimeout;
  188. }
  189. public void setEnquireLinkTimeout(Integer enquireLinkTimeout) {
  190. this.enquireLinkTimeout = enquireLinkTimeout.intValue() * 1000;
  191. }
  192. public String getBindType() {
  193. return bindType;
  194. }
  195. public void setBindType(String bindTypeArg) {
  196. bindType = bindTypeArg;
  197. }
  198. public int getSmppResponseReceivedTimeout() {
  199. return smppResponseReceivedTimeout;
  200. }
  201. public void setSmppResponseReceivedTimeout(int smppResponseReceivedTimeout) {
  202. if (smppResponseReceivedTimeout < 100) {
  203. smppResponseReceivedTimeout = 5000;
  204. }
  205. this.smppResponseReceivedTimeout = smppResponseReceivedTimeout;
  206. }
  207. public int getSmppResponseSentTimeout() {
  208. return smppResponseSentTimeout;
  209. }
  210. public void setSmppResponseSentTimeout(int smppResponseSentTimeout) {
  211. if (smppResponseSentTimeout < 100) {
  212. smppResponseSentTimeout = 5000;
  213. }
  214. this.smppResponseSentTimeout = smppResponseSentTimeout;
  215. }
  216. public void dataRemoved(Serializable arg0) {
  217. // TODO Auto-generated method stub
  218. }
  219. public void failOver(Serializable arg0) {
  220. if (this.tracer.isInfoEnabled()) {
  221. this.tracer.info("Failed over the SMPP. Available memebers now ");
  222. for (MemberAddress memAdd : this.ftRAContext.getMembers()) {
  223. this.tracer.info(memAdd.toString());
  224. }
  225. }
  226. }
  227. public void setFaultTolerantResourceAdaptorContext(FaultTolerantResourceAdaptorContext arg0) {
  228. tracer.info("setFaultTolerantResourceAdaptorContext(FaultTolerantResourceAdaptorContext arg0)");
  229. this.ftRAContext = arg0;
  230. }
  231. public void unsetFaultTolerantResourceAdaptorContext() {
  232. tracer.info("unsetFaultTolerantResourceAdaptorContext()");
  233. this.ftRAContext = null;
  234. }
  235. public void activityEnded(ActivityHandle activityHandle) {
  236. this.handleVsActivityMap.remove(activityHandle);
  237. }
  238. public void activityUnreferenced(ActivityHandle arg0) {
  239. // TODO Auto-generated method stub
  240. }
  241. public void administrativeRemove(ActivityHandle arg0) {
  242. // TODO Auto-generated method stub
  243. }
  244. public void eventProcessingFailed(ActivityHandle arg0, FireableEventType arg1, Object arg2, Address arg3, ReceivableService arg4, int arg5,
  245. FailureReason arg6) {
  246. // TODO Auto-generated method stub
  247. }
  248. public void eventProcessingSuccessful(ActivityHandle arg0, FireableEventType arg1, Object arg2, Address arg3, ReceivableService arg4, int arg5) {
  249. // TODO Auto-generated method stub
  250. }
  251. public void eventUnreferenced(ActivityHandle arg0, FireableEventType arg1, Object arg2, Address arg3, ReceivableService arg4, int arg5) {
  252. // TODO Auto-generated method stub
  253. }
  254. public Object getActivity(ActivityHandle activityHandle) {
  255. return this.handleVsActivityMap.get(activityHandle);
  256. }
  257. public ActivityHandle getActivityHandle(Object obj) {
  258. return ((SmppTransactionImpl) obj).getHandle();
  259. }
  260. public Marshaler getMarshaler() {
  261. // TODO Auto-generated method stub
  262. return null;
  263. }
  264. public Object getResourceAdaptorInterface(String arg0) {
  265. return this.smppSession;
  266. }
  267. public void queryLiveness(ActivityHandle activityHandle) {
  268. if (getActivity(activityHandle) == null) {
  269. this.tracer.warning("The queryLiveness failed for activity " + activityHandle + " Ending this activity");
  270. this.sleeEndpoint.endActivity(activityHandle);
  271. }
  272. }
  273. public void raActive() {
  274. if (this.tracer.isInfoEnabled()) {
  275. tracer.info("Activation RA " + this.raContext.getEntityName());
  276. }
  277. this.lastMessagesExchanged = 0;
  278. this.lastStatsUpdatedTime = System.currentTimeMillis();
  279. try {
  280. bindSMSC();
  281. } catch (IOException e) {
  282. this.tracer.severe("Binding to SMSC Failed ", e);
  283. }
  284. this.isRAStoped = false;
  285. // Start the ENQUIRE Link Thread in any case
  286. linkMonitorThread = new Thread(new LinkMonitor());
  287. linkMonitorThread.start();
  288. }
  289. public void raConfigurationUpdate(ConfigProperties properties) {
  290. raConfigure(properties);
  291. }
  292. public void raConfigure(ConfigProperties properties) {
  293. if (tracer.isFineEnabled()) {
  294. tracer.fine("Configuring RA Entity " + this.raContext.getEntityName());
  295. }
  296. setAddressNpi((Integer) properties.getProperty("addressNpi").getValue());
  297. setAddressRange((String) properties.getProperty("addressRange").getValue());
  298. setAddressTon((Integer) properties.getProperty("addressTon").getValue());
  299. setEnquireLinkTimeout((Integer) properties.getProperty("enquireLinkTimeout").getValue());
  300. setHost((String) properties.getProperty("host").getValue());
  301. setPassword((String) properties.getProperty("password").getValue());
  302. setPort((Integer) properties.getProperty("port").getValue());
  303. setSystemId((String) properties.getProperty("systemId").getValue());
  304. setSystemType((String) properties.getProperty("systemType").getValue());
  305. setBindType((String) properties.getProperty("bindType").getValue());
  306. setSmppResponseReceivedTimeout((Integer) properties.getProperty("smppResponseReceivedTimeout").getValue());
  307. setSmppResponseSentTimeout((Integer) properties.getProperty("smppResponseSentTimeout").getValue());
  308. statsOn = (Boolean) properties.getProperty("statsOn").getValue();
  309. if (tracer.isFineEnabled()) {
  310. StringBuffer sb = new StringBuffer();
  311. sb.append("addressNpi = ").append(getAddressNpi()).append("\n").append("addressTon = ").append(getAddressTon()).append("\n")
  312. .append("addressRange = ").append(getAddressRange()).append("\n").append("host = ").append(getHost()).append("\n").append("port = ")
  313. .append(getPort()).append("\n").append("systemId = ").append(getSystemId()).append("\n").append("systemType = ").append(getSystemType())
  314. .append("\n").append("password = ").append(getPassword()).append("\n").append("bindType = ").append(getBindType()).append("\n");
  315. tracer.fine(sb.toString());
  316. }
  317. }
  318. public void raInactive() {
  319. this.unbindSMSC();
  320. }
  321. public void raStopping() {
  322. isRAStoped = true;
  323. this.smppSession.setIsAlive(false);
  324. if (linkMonitorThread != null) {
  325. linkMonitorThread.interrupt();
  326. }
  327. }
  328. public void raUnconfigure() {
  329. setAddressNpi(-1);
  330. setAddressRange(null);
  331. setAddressTon(-1);
  332. setEnquireLinkTimeout(-1);
  333. setHost(null);
  334. setPassword(null);
  335. setPort(-1);
  336. setSystemId(null);
  337. setSystemType(null);
  338. setBindType(null);
  339. }
  340. public void raVerifyConfiguration(ConfigProperties properties) throws InvalidConfigurationException {
  341. String addressRange = null;
  342. String host = null;
  343. String password = null;
  344. String systemId = null;
  345. String systemType = null;
  346. BindType binidType = null;
  347. try {
  348. addressRange = (String) properties.getProperty("addressRange").getValue();
  349. host = (String) properties.getProperty("host").getValue();
  350. password = (String) properties.getProperty("password").getValue();
  351. systemId = (String) properties.getProperty("systemId").getValue();
  352. systemType = (String) properties.getProperty("systemType").getValue();
  353. binidType = BindType.getBindType((String) properties.getProperty("bindType").getValue());
  354. } catch (Throwable e) {
  355. throw new InvalidConfigurationException(e.getMessage(), e);
  356. }
  357. if (addressRange == null) {
  358. throw new InvalidConfigurationException("Address Range cannot be null");
  359. } else if (host == null) {
  360. throw new InvalidConfigurationException("Host (of SMSC) cannot be null");
  361. } else if (password == null) {
  362. throw new InvalidConfigurationException("Password cannot be null");
  363. } else if (systemId == null) {
  364. throw new InvalidConfigurationException("System ID cannot be null");
  365. } else if (systemType == null) {
  366. throw new InvalidConfigurationException("System Type cannot be null");
  367. } else if (binidType == null) {
  368. throw new InvalidConfigurationException("Bind Type cannot be null");
  369. }
  370. }
  371. public void serviceActive(ReceivableService receivableService) {
  372. this.eventTypeFilter.serviceActive(receivableService);
  373. }
  374. public void serviceInactive(ReceivableService receivableService) {
  375. this.eventTypeFilter.serviceInactive(receivableService);
  376. }
  377. public void serviceStopping(ReceivableService receivableService) {
  378. this.eventTypeFilter.serviceStopping(receivableService);
  379. }
  380. public void setResourceAdaptorContext(ResourceAdaptorContext raContext) {
  381. this.tracer = raContext.getTracer(getClass().getSimpleName());
  382. this.raContext = raContext;
  383. this.sleeEndpoint = raContext.getSleeEndpoint();
  384. this.eventLookup = raContext.getEventLookupFacility();
  385. this.smppSession = new SmppSessionImpl(this);
  386. this.eventTypeCache = new FireableEventTypeCache(tracer);
  387. this.eventTypeFilter = new FireableEventTypeFilter();
  388. this.usageParams = (SmppResourceAdaptorUsageParameters) raContext.getDefaultUsageParameterSet();
  389. this.utils = new Utils(this);
  390. }
  391. public void unsetResourceAdaptorContext() {
  392. this.raContext = null;
  393. this.sleeEndpoint = null;
  394. this.eventTypeCache = null;
  395. this.eventTypeFilter = null;
  396. this.usageParams = null;
  397. }
  398. /**
  399. * ConnectionObserver methods
  400. */
  401. public void packetReceived(Session source, org.mobicents.protocols.smpp.message.SMPPPacket packet) {
  402. String entityName = raContext.getEntityName();
  403. //Update usage params
  404. incrementUsageParams(packet.getCommandId());
  405. switch (packet.getCommandId()) {
  406. // A connected ESME has requested to bind as an ESME Transceiver
  407. // (by issuing a bind_transceiver PDU) and has received a response from
  408. // the SMSC authorising its Bind request. An ESME bound as a Transceiver
  409. // supports the complete set of operations supported by a Transmitter
  410. // ESME and a Receiver ESME. Thus an ESME bound as a transceiver may
  411. // send short messages to an SMSC for onward delivery to a Mobile
  412. // Station
  413. // or to another ESME. The ESME may also receive short messages from an
  414. // SMSC which may be originated by a mobile station, by another ESME or
  415. // by the SMSC itself (for example an SMSC delivery receipt).
  416. case CommandId.BIND_TRANSCEIVER_RESP:
  417. bindStatus = packet.getCommandStatus();
  418. if (tracer.isFineEnabled()) {
  419. tracer.fine(entityName + " receive bind_transaceiver_resp. Statu = " + bindStatus + " Message = " + this.utils.statusMessage(bindStatus));
  420. }
  421. semaphore.release();
  422. break;
  423. case CommandId.BIND_RECEIVER_RESP:
  424. bindStatus = packet.getCommandStatus();
  425. if (tracer.isFineEnabled()) {
  426. tracer.fine(entityName + " receive bind_receiver_resp. Statu = " + bindStatus + " Message = " + this.utils.statusMessage(bindStatus));
  427. }
  428. semaphore.release();
  429. break;
  430. case CommandId.BIND_TRANSMITTER_RESP:
  431. bindStatus = packet.getCommandStatus();
  432. if (tracer.isFineEnabled()) {
  433. tracer.fine(entityName + " receive bind_transmitter_resp. Statu = " + bindStatus + " Message = " + this.utils.statusMessage(bindStatus));
  434. }
  435. semaphore.release();
  436. break;
  437. // An ESME has unbound from the SMSC and has closed the network
  438. // connection. The SMSC may also unbind from the ESME.
  439. case CommandId.UNBIND_RESP: {
  440. if (tracer.isFineEnabled()) {
  441. tracer.fine(raContext.getEntityName() + " unbound successfuly");
  442. }
  443. semaphore.release();
  444. break;
  445. }
  446. // This message can be sent by either the ESME or SMSC and is used
  447. // to provide a confidence-check of the communication path between
  448. // an ESME and an SMSC. On receipt of this request the receiving
  449. // party should respond with an enquire_link_resp, thus verifying
  450. // that the application level connection between the SMSC and the
  451. // ESME is functioning. The ESME may also respond by sending any
  452. // valid SMPP primitive.
  453. case CommandId.ENQUIRE_LINK:
  454. // TODO reply with enquire_link_resp
  455. if (tracer.isFineEnabled()) {
  456. tracer.fine("Enquire link packet received");
  457. }
  458. break;
  459. case CommandId.ENQUIRE_LINK_RESP:
  460. if (tracer.isFineEnabled()) {
  461. tracer.fine("Enquire link response packet received");
  462. }
  463. break;
  464. case CommandId.DELIVER_SM: {
  465. DeliverSMImpl deliverSMImpl = new DeliverSMImpl((org.mobicents.protocols.smpp.message.DeliverSM) packet);
  466. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(deliverSMImpl, true, SmppTransactionType.INCOMING);
  467. if (txImpl != null) {
  468. if (isEsmClassUserMessage(deliverSMImpl.getEsmClass())) {
  469. fireEvent(Utils.DELIVER_SM, txImpl, deliverSMImpl);
  470. } else {
  471. fireEvent(Utils.DELIVERY_REPORT, txImpl, deliverSMImpl);
  472. }
  473. }
  474. break;
  475. }
  476. // The command acknowledges deliver_sm message.
  477. case CommandId.DELIVER_SM_RESP: {
  478. // TODO : Take care of Delivery Acknowledgement
  479. DeliverSMRespImpl deliverSMRespImpl = new DeliverSMRespImpl((org.mobicents.protocols.smpp.message.DeliverSMResp) packet);
  480. try {
  481. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(deliverSMRespImpl, false, SmppTransactionType.OUTGOING);
  482. txImpl.cancelResponseNotReceivedTimeout();
  483. fireEvent(Utils.DELIVER_SM_RESP, txImpl, deliverSMRespImpl);
  484. this.endActivity(txImpl);
  485. } catch (Exception e) {
  486. this.tracer.warning("Activity not found for received SMPP Response " + packet, e);
  487. }
  488. break;
  489. }
  490. case CommandId.DATA_SM: {
  491. DataSMImpl dataSMImpl = new DataSMImpl((org.mobicents.protocols.smpp.message.DataSM) packet);
  492. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(dataSMImpl, true, SmppTransactionType.INCOMING);
  493. if (txImpl != null) {
  494. fireEvent(Utils.DATA_SM, txImpl, dataSMImpl);
  495. }
  496. break;
  497. }
  498. case CommandId.DATA_SM_RESP: {
  499. DataSMRespImpl dataSMRespImpl = new DataSMRespImpl((org.mobicents.protocols.smpp.message.DataSMResp) packet);
  500. try {
  501. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(dataSMRespImpl, false, SmppTransactionType.OUTGOING);
  502. txImpl.cancelResponseNotReceivedTimeout();
  503. fireEvent(Utils.DATA_SM_RESP, txImpl, dataSMRespImpl);
  504. this.endActivity(txImpl);
  505. } catch (Exception e) {
  506. this.tracer.warning("Activity not found for received SMPP Response " + packet, e);
  507. }
  508. break;
  509. }
  510. case CommandId.SUBMIT_SM: {
  511. SubmitSMImpl submitSMImpl = new SubmitSMImpl((org.mobicents.protocols.smpp.message.SubmitSM) packet);
  512. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(submitSMImpl, true, SmppTransactionType.INCOMING);
  513. if (txImpl != null) {
  514. fireEvent(Utils.SUBMIT_SM, txImpl, submitSMImpl);
  515. }
  516. break;
  517. }
  518. case CommandId.SUBMIT_SM_RESP: {
  519. SubmitSMRespImpl submitSMRespImpl = new SubmitSMRespImpl((org.mobicents.protocols.smpp.message.SubmitSMResp) packet);
  520. try {
  521. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(submitSMRespImpl, false, SmppTransactionType.OUTGOING);
  522. txImpl.cancelResponseNotReceivedTimeout();
  523. fireEvent(Utils.SUBMIT_SM_RESP, txImpl, submitSMRespImpl);
  524. this.endActivity(txImpl);
  525. } catch (Exception e) {
  526. this.tracer.warning("Activity not found for received SMPP Response " + packet, e);
  527. }
  528. break;
  529. }
  530. case CommandId.SUBMIT_MULTI: {
  531. SubmitMultiImpl submitMultiImpl = new SubmitMultiImpl((org.mobicents.protocols.smpp.message.SubmitMulti) packet);
  532. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(submitMultiImpl, true, SmppTransactionType.INCOMING);
  533. if (txImpl != null) {
  534. fireEvent(Utils.SUBMIT_MULTI, txImpl, submitMultiImpl);
  535. }
  536. break;
  537. }
  538. case CommandId.SUBMIT_MULTI_RESP: {
  539. SubmitMultiRespImpl submitMultiRespImpl = new SubmitMultiRespImpl((org.mobicents.protocols.smpp.message.SubmitMultiResp) packet);
  540. try {
  541. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(submitMultiRespImpl, false, SmppTransactionType.OUTGOING);
  542. txImpl.cancelResponseNotReceivedTimeout();
  543. fireEvent(Utils.SUBMIT_MULTI_RESP, txImpl, submitMultiRespImpl);
  544. this.endActivity(txImpl);
  545. } catch (Exception e) {
  546. this.tracer.warning("Activity not found for received SMPP Response " + packet, e);
  547. }
  548. break;
  549. }
  550. case CommandId.QUERY_SM: {
  551. QuerySMImpl querySMImpl = new QuerySMImpl((org.mobicents.protocols.smpp.message.QuerySM) packet);
  552. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(querySMImpl, true, SmppTransactionType.INCOMING);
  553. if (txImpl != null) {
  554. fireEvent(Utils.QUERY_SM, txImpl, querySMImpl);
  555. }
  556. break;
  557. }
  558. case CommandId.QUERY_SM_RESP: {
  559. QuerySMRespImpl querySMRespImpl = new QuerySMRespImpl((org.mobicents.protocols.smpp.message.QuerySMResp) packet);
  560. try {
  561. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(querySMRespImpl, false, SmppTransactionType.OUTGOING);
  562. txImpl.cancelResponseNotReceivedTimeout();
  563. fireEvent(Utils.QUERY_SM_RESP, txImpl, querySMRespImpl);
  564. this.endActivity(txImpl);
  565. } catch (Exception e) {
  566. this.tracer.warning("Activity not found for received SMPP Response " + packet, e);
  567. }
  568. break;
  569. }
  570. case CommandId.REPLACE_SM: {
  571. ReplaceSMImpl replaceSMImpl = new ReplaceSMImpl((org.mobicents.protocols.smpp.message.ReplaceSM) packet);
  572. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(replaceSMImpl, true, SmppTransactionType.INCOMING);
  573. if (txImpl != null) {
  574. fireEvent(Utils.REPLACE_SM, txImpl, replaceSMImpl);
  575. }
  576. break;
  577. }
  578. case CommandId.REPLACE_SM_RESP: {
  579. ReplaceSMRespImpl replaceSMRespImpl = new ReplaceSMRespImpl((org.mobicents.protocols.smpp.message.ReplaceSMResp) packet);
  580. try {
  581. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(replaceSMRespImpl, false, SmppTransactionType.OUTGOING);
  582. txImpl.cancelResponseNotReceivedTimeout();
  583. fireEvent(Utils.REPLACE_SM_RESP, txImpl, replaceSMRespImpl);
  584. this.endActivity(txImpl);
  585. } catch (Exception e) {
  586. this.tracer.warning("Activity not found for received SMPP Response " + packet, e);
  587. }
  588. break;
  589. }
  590. case CommandId.CANCEL_SM: {
  591. CancelSMImpl cancelSMImpl = new CancelSMImpl((org.mobicents.protocols.smpp.message.CancelSM) packet);
  592. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(cancelSMImpl, true, SmppTransactionType.INCOMING);
  593. if (txImpl != null) {
  594. fireEvent(Utils.CANCEL_SM, txImpl, cancelSMImpl);
  595. }
  596. break;
  597. }
  598. case CommandId.CANCEL_SM_RESP: {
  599. CancelSMRespImpl cancelSMRespImpl = new CancelSMRespImpl((org.mobicents.protocols.smpp.message.CancelSMResp) packet);
  600. try {
  601. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(cancelSMRespImpl, false, SmppTransactionType.OUTGOING);
  602. txImpl.cancelResponseNotReceivedTimeout();
  603. fireEvent(Utils.CANCEL_SM_RESP, txImpl, cancelSMRespImpl);
  604. this.endActivity(txImpl);
  605. } catch (Exception e) {
  606. this.tracer.warning("Activity not found for received SMPP Response " + packet, e);
  607. }
  608. break;
  609. }
  610. case CommandId.BROADCAST_SM: {
  611. BroadcastSMImpl broadcastSMImpl = new BroadcastSMImpl((org.mobicents.protocols.smpp.message.BroadcastSM) packet);
  612. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(broadcastSMImpl, true, SmppTransactionType.INCOMING);
  613. if (txImpl != null) {
  614. fireEvent(Utils.BROADCAST_SM, txImpl, broadcastSMImpl);
  615. }
  616. break;
  617. }
  618. case CommandId.BROADCAST_SM_RESP: {
  619. BroadcastSMRespImpl broadcastSMRespImpl = new BroadcastSMRespImpl((org.mobicents.protocols.smpp.message.BroadcastSMResp) packet);
  620. try {
  621. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(broadcastSMRespImpl, false, SmppTransactionType.OUTGOING);
  622. txImpl.cancelResponseNotReceivedTimeout();
  623. fireEvent(Utils.BROADCAST_SM_RESP, txImpl, broadcastSMRespImpl);
  624. this.endActivity(txImpl);
  625. } catch (Exception e) {
  626. this.tracer.warning("Activity not found for received SMPP Response " + packet, e);
  627. }
  628. break;
  629. }
  630. case CommandId.CANCEL_BROADCAST_SM: {
  631. CancelBroadcastSMImpl cancelBroadcastSMImpl = new CancelBroadcastSMImpl((org.mobicents.protocols.smpp.message.CancelBroadcastSM) packet);
  632. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(cancelBroadcastSMImpl, true, SmppTransactionType.INCOMING);
  633. if (txImpl != null) {
  634. fireEvent(Utils.CANCEL_BROADCAST_SM, txImpl, cancelBroadcastSMImpl);
  635. }
  636. break;
  637. }
  638. case CommandId.CANCEL_BROADCAST_SM_RESP: {
  639. CancelBroadcastSMRespImpl cancelBroadcastSMRespImpl = new CancelBroadcastSMRespImpl(
  640. (org.mobicents.protocols.smpp.message.CancelBroadcastSMResp) packet);
  641. try {
  642. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(cancelBroadcastSMRespImpl, false, SmppTransactionType.OUTGOING);
  643. txImpl.cancelResponseNotReceivedTimeout();
  644. fireEvent(Utils.CANCEL_BROADCAST_SM_RESP, txImpl, cancelBroadcastSMRespImpl);
  645. this.endActivity(txImpl);
  646. } catch (Exception e) {
  647. this.tracer.warning("Activity not found for received SMPP Response " + packet, e);
  648. }
  649. break;
  650. }
  651. case CommandId.QUERY_BROADCAST_SM: {
  652. QueryBroadcastSMImpl queryBroadcastSMImpl = new QueryBroadcastSMImpl((org.mobicents.protocols.smpp.message.QueryBroadcastSM) packet);
  653. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(queryBroadcastSMImpl, true, SmppTransactionType.INCOMING);
  654. if (txImpl != null) {
  655. fireEvent(Utils.QUERY_BROADCAST_SM, txImpl, queryBroadcastSMImpl);
  656. }
  657. break;
  658. }
  659. case CommandId.QUERY_BROADCAST_SM_RESP: {
  660. QueryBroadcastSMRespImpl queryBroadcastSMRespImpl = new QueryBroadcastSMRespImpl((org.mobicents.protocols.smpp.message.QueryBroadcastSMResp) packet);
  661. try {
  662. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(queryBroadcastSMRespImpl, false, SmppTransactionType.OUTGOING);
  663. txImpl.cancelResponseNotReceivedTimeout();
  664. fireEvent(Utils.QUERY_BROADCAST_SM_RESP, txImpl, queryBroadcastSMRespImpl);
  665. this.endActivity(txImpl);
  666. } catch (Exception e) {
  667. this.tracer.warning("Activity not found for received SMPP Response " + packet, e);
  668. }
  669. break;
  670. }
  671. case CommandId.GENERIC_NACK: {
  672. GenericNackImpl genericNackImpl = new GenericNackImpl((org.mobicents.protocols.smpp.message.GenericNack) packet);
  673. if (genericNackImpl.getCommandStatus() == SmppTransaction.ESME_ROK) {
  674. if (this.tracer.isFineEnabled()) {
  675. this.tracer.fine("Receievd GENERIC_NACK acknowledgemet " + genericNackImpl);
  676. }
  677. } else {
  678. // This is Error Response to one of the Request sent
  679. try {
  680. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(genericNackImpl, false, SmppTransactionType.OUTGOING);
  681. txImpl.cancelResponseNotReceivedTimeout();
  682. fireEvent(Utils.GENERIC_NACK, txImpl, genericNackImpl);
  683. this.endActivity(txImpl);
  684. } catch (Exception e) {
  685. this.tracer.warning("Activity not found for received SMPP Response " + packet, e);
  686. }
  687. }
  688. break;
  689. }
  690. case CommandId.ALERT_NOTIFICATION: {
  691. AlertNotificationImpl alertNotiImpl = new AlertNotificationImpl((org.mobicents.protocols.smpp.message.AlertNotification) packet);
  692. SmppTransactionImpl txImpl = this.smppSession.getSmppTransactionImpl(alertNotiImpl, true, SmppTransactionType.INCOMING);
  693. if (txImpl != null) {
  694. fireEvent(Utils.ALERT_NOTIFICATION, txImpl, alertNotiImpl);
  695. }
  696. this.endActivity(txImpl);
  697. break;
  698. }
  699. default:
  700. tracer.warning("Unexpected packet received! Id = 0x" + Integer.toHexString(packet.getCommandId()));
  701. }
  702. }
  703. public void update(Session source, org.mobicents.protocols.smpp.event.SMPPEvent smppEvent) {
  704. switch (smppEvent.getType()) {
  705. case org.mobicents.protocols.smpp.event.SMPPEvent.RECEIVER_START:
  706. if (this.tracer.isFineEnabled()) {
  707. this.tracer.fine("Receiver Thread started for SMPP RA " + this.raContext.getEntityName());
  708. }
  709. break;
  710. case org.mobicents.protocols.smpp.event.SMPPEvent.RECEIVER_EXCEPTION:
  711. if (this.tracer.isWarningEnabled()) {
  712. org.mobicents.protocols.smpp.event.ReceiverExceptionEvent recExcepEvent = (org.mobicents.protocols.smpp.event.ReceiverExceptionEvent) smppEvent;
  713. this.tracer.warning("The Receiver Thread for SMPP RA " + this.raContext.getEntityName() + " throws recovrable Exception ",
  714. recExcepEvent.getException());
  715. }
  716. //This is recovrable error. Don't reconnect!
  717. //this.reconnect();
  718. break;
  719. case org.mobicents.protocols.smpp.event.SMPPEvent.RECEIVER_EXIT:
  720. org.mobicents.protocols.smpp.event.ReceiverExitEvent recExitEvent = (org.mobicents.protocols.smpp.event.ReceiverExitEvent) smppEvent;
  721. switch (recExitEvent.getReason()) {
  722. case org.mobicents.protocols.smpp.event.ReceiverExitEvent.EXCEPTION:
  723. this.tracer.severe("The Receiver Thread for SMPP RA " + this.raContext.getEntityName() + " exited with error ", recExitEvent.getException());
  724. this.reconnect();
  725. break;
  726. case org.mobicents.protocols.smpp.event.ReceiverExitEvent.BIND_TIMEOUT:
  727. if (this.tracer.isFineEnabled()) {
  728. this.tracer.fine("The Receiver Thread for SMPP RA " + this.raContext.getEntityName() + " exited with BIND_TIMEOUT");
  729. }
  730. break;
  731. case org.mobicents.protocols.smpp.event.ReceiverExitEvent.UNKNOWN:
  732. //This is graceful shutdown. No need to reconnect here
  733. if (this.tracer.isFineEnabled()) {
  734. this.tracer.fine("The Receiver Thread for SMPP RA " + this.raContext.getEntityName() + " exited normally");
  735. }
  736. break;
  737. }
  738. break;
  739. }
  740. }
  741. /**
  742. * Private Methods
  743. */
  744. private void bindSMSC() throws UnknownHostException, IOException {
  745. this.bindStatus = -1;
  746. protoSmppSession = new org.mobicents.protocols.smpp.Session(host, port);
  747. seq = protoSmppSession.getSequenceNumberScheme();
  748. protoSmppSession.addObserver(this);
  749. protoSmppSession.setVersion(SMPPVersion.VERSION_5_0);
  750. // smscConnection.autoAckLink(true);
  751. // smscConnection.autoAckMessages(true);
  752. IOException ioException = null;
  753. // Bind
  754. try {
  755. this.createBind();
  756. protoSmppSession.bind(bind);
  757. //Update usage params
  758. incrementUsageParams(bind.getCommandId());
  759. semaphore.drainPermits();
  760. semaphore.tryAcquire(5, TimeUnit.SECONDS);
  761. } catch (InterruptedException e) {
  762. this.tracer.severe("Binding to SMS failed", e);
  763. this.bindStatus = -1;
  764. } catch (IOException e) {
  765. this.bindStatus = -1;
  766. ioException = e;
  767. }
  768. if (this.bindStatus != SmppTransaction.ESME_ROK) {
  769. AlarmFacility alarmFacility = raContext.getAlarmFacility();
  770. smscAlarm = alarmFacility.raiseAlarm(raContext.getEntityName(), "SMSCALARM", AlarmLevel.CRITICAL,
  771. "SMSCALARM: bind status " + this.utils.statusMessage(bindStatus));
  772. if (ioException != null) {
  773. throw ioException;
  774. } else {
  775. throw new IOException("Could not bind to SMSC. The reason is " + this.utils.statusMessage(bindStatus));
  776. }
  777. }
  778. tracer.info("Successfully bound to SMSC. ");
  779. this.smppSession.setIsAlive(true);
  780. if (smscAlarm != null) {
  781. AlarmFacility alarmFacility = raContext.getAlarmFacility();
  782. alarmFacility.clearAlarm(smscAlarm);
  783. }
  784. }
  785. private Bind createBind() {
  786. BindType bindType = BindType.getBindType(this.getBindType());
  787. switch (bindType) {
  788. case TRANSMITTER:
  789. bind = new BindTransmitter();
  790. break;
  791. case RECEIVER:
  792. bind = new BindReceiver();
  793. break;
  794. case TRANSCEIVER:
  795. bind = new BindTransceiver();
  796. break;
  797. }
  798. bind.setAddressTon(this.getAddressTon());
  799. bind.setAddressNpi(this.getAddressNpi());
  800. bind.setAddressRange(this.getAddressRange());
  801. bind.setSystemType(this.getSystemType());
  802. bind.setSystemId(this.getSystemId());
  803. bind.setPassword(this.getPassword());
  804. return bind;
  805. }
  806. /**
  807. * reconnect() is called from LinkMonitor when connectivity between RA and
  808. * SMSC fails. lets set isBound to true so while loop of LinkMonitor keeps
  809. * trying to establish link again
  810. */
  811. private void reconnect() {
  812. try {
  813. unbindSMSC();
  814. this.protoSmppSession.closeLink();
  815. } catch (Exception e) {
  816. // ignore all errors during unbinding
  817. }
  818. try {
  819. bindSMSC();
  820. } catch (Exception e) {
  821. if (tracer.isSevereEnabled())
  822. tracer.severe("Reconnect error: " + e.getMessage());
  823. }
  824. }
  825. private void unbindSMSC() {
  826. this.smppSession.setIsAlive(false);
  827. try {
  828. protoSmppSession.unbind();
  829. semaphore.drainPermits();
  830. semaphore.tryAcquire(5, TimeUnit.SECONDS);
  831. if (tracer.isInfoEnabled())
  832. tracer.info(raContext.getEntityName() + ": unbinding from SMSC");
  833. } catch (Exception e) {
  834. if (tracer.isSevereEnabled())
  835. tracer.severe("There was an error unbinding. ", e);
  836. }
  837. }
  838. /**
  839. * Checking if the EsmClass is user message or is at a notification.
  840. *
  841. * @param esmClassArg
  842. * - esm_class value
  843. * @return true if it is a user message, otherwise false
  844. */
  845. private static boolean isEsmClassUserMessage(int esmClassArg) {
  846. return (esmClassArg & 0X3C) == 0;
  847. }
  848. private void updateStats(){
  849. if(statsOn) {
  850. return;
  851. }
  852. long now = System.currentTimeMillis();
  853. long elpasedNumberOfMessagesCounter = usageParams.getMessagesExchanged() - this.lastMessagesExchanged;
  854. if(elpasedNumberOfMessagesCounter > 0){
  855. double elpasedTimeInSec = (now - this.lastStatsUpdatedTime) / 1000;
  856. double avgMessagesExchangedPerSec = elpasedNumberOfMessagesCounter / elpasedTimeInSec;
  857. usageParams.sampleMessagesExchangedPerSec((long)avgMessagesExchangedPerSec);
  858. }
  859. this.lastMessagesExchanged = usageParams.getMessagesExchanged();
  860. this.lastStatsUpdatedTime = now;
  861. }
  862. private void incrementUsageParams(int commandId) {
  863. if(!statsOn) {
  864. return;
  865. }
  866. usageParams.incrementMessagesExchanged(1);
  867. switch (commandId) {
  868. case CommandId.SUBMIT_SM:
  869. usageParams.incrementReqSubmitSm(1);
  870. break;
  871. case CommandId.SUBMIT_SM_RESP:
  872. usageParams.incrementResSubmitSm(1);
  873. break;
  874. case CommandId.DELIVER_SM:
  875. usageParams.incrementReqDeliverSm(1);
  876. break;
  877. case CommandId.DELIVER_SM_RESP:
  878. usageParams.incrementResDeliverSm(1);
  879. break;
  880. case CommandId.GENERIC_NACK:
  881. usageParams.incrementGenericNack(1);
  882. break;
  883. case CommandId.BIND_RECEIVER:
  884. usageParams.incrementReqBindReceiver(1);
  885. break;
  886. case CommandId.BIND_RECEIVER_RESP:
  887. usageParams.incrementResBindReceiver(1);
  888. break;
  889. case CommandId.BIND_TRANSMITTER:
  890. usageParams.incrementReqBindTransmitter(1);
  891. break;
  892. case CommandId.BIND_TRANSMITTER_RESP:
  893. usageParams.incrementResBindTransmitter(1);
  894. break;
  895. case CommandId.QUERY_SM:
  896. usageParams.incrementReqQuerySm(1);
  897. break;
  898. case CommandId.QUERY_SM_RESP:
  899. usageParams.incrementResQuerySm(1);
  900. break;
  901. case CommandId.UNBIND:
  902. usageParams.incrementReqUnbind(1);
  903. break;
  904. case CommandId.UNBIND_RESP:
  905. usageParams.incrementResUnbind(1);
  906. break;
  907. case CommandId.REPLACE_SM:
  908. usageParams.incrementReqReplaceSm(1);
  909. break;
  910. case CommandId.REPLACE_SM_RESP:
  911. usageParams.incrementResReplaceSm(1);
  912. break;
  913. case CommandId.CANCEL_SM:
  914. usageParams.incrementReqCancelSm(1);
  915. break;
  916. case CommandId.CANCEL_SM_RESP:
  917. usageParams.incrementResCancelSm(1);
  918. break;
  919. case CommandId.BIND_TRANSCEIVER:
  920. usageParams.incrementReqBindTransceiver(1);
  921. break;
  922. case CommandId.BIND_TRANSCEIVER_RESP:
  923. usageParams.incrementResBindTransceiver(1);
  924. break;
  925. case CommandId.OUTBIND:
  926. //TODO Supported?
  927. break;
  928. case CommandId.ENQUIRE_LINK:
  929. usageParams.incrementReqEnquireLink(1);
  930. break;
  931. case CommandId.ENQUIRE_LINK_RESP:
  932. usageParams.incrementResEnquireLink(1);
  933. break;
  934. case CommandId.SUBMIT_MULTI:
  935. usageParams.incrementReqSubmitMulti(1);
  936. break;
  937. case CommandId.SUBMIT_MULTI_RESP:
  938. usageParams.incrementResSubmitMulti(1);
  939. break;
  940. case CommandId.PARAM_RETRIEVE:
  941. usageParams.incrementReqParamRetrieve(1);
  942. break;
  943. case CommandId.PARAM_RETRIEVE_RESP:
  944. usageParams.incrementResParamRetrieve(1);
  945. break;
  946. case CommandId.QUERY_LAST_MSGS:
  947. usageParams.incrementReqQueryLastMessages(1);
  948. break;
  949. case CommandId.QUERY_LAST_MSGS_RESP:
  950. usageParams.incrementResQueryLastMessages(1);
  951. break;
  952. case CommandId.QUERY_MSG_DETAILS:
  953. usageParams.incrementReqQueryMessageDetails(1);
  954. break;
  955. case CommandId.QUERY_MSG_DETAILS_RESP:
  956. usageParams.incrementResQueryMessageDetails(1);
  957. break;
  958. case CommandId.ALERT_NOTIFICATION:
  959. usageParams.incrementReqAlertNotification(1);
  960. break;
  961. case CommandId.DATA_SM:
  962. usageParams.incrementReqDataSm(1);
  963. break;
  964. case CommandId.DATA_SM_RESP:
  965. usageParams.incrementResDataSm(1);
  966. break;
  967. case CommandId.BROADCAST_SM:
  968. usageParams.incrementReqBroadcastSm(1);
  969. break;
  970. case CommandId.BROADCAST_SM_RESP:
  971. usageParams.incrementResBroadcastSm(1);
  972. break;
  973. case CommandId.QUERY_BROADCAST_SM:
  974. usageParams.incrementReqQuerySm(1);
  975. break;
  976. case CommandId.QUERY_BROADCAST_SM_RESP:
  977. usageParams.incrementResQuerySm(1);
  978. break;
  979. case CommandId.CANCEL_BROADCAST_SM:
  980. usageParams.incrementReqBroadcastSm(1);
  981. break;
  982. case CommandId.CANCEL_BROADCAST_SM_RESP:
  983. usageParams.incrementResBroadcastSm(1);
  984. break;
  985. default:
  986. //Do nothing?
  987. //tracer.warning("Unexpected packet received! Id = 0x" + Integer.toHexString(commandId));
  988. }
  989. }
  990. /**
  991. * Protected Methods
  992. *
  993. * @throws StartActivityException
  994. * @throws SLEEException
  995. * @throws IllegalStateException
  996. * @throws NullPointerException
  997. * @throws ActivityAlreadyExistsException
  998. */
  999. protected void startNewSmppTransactionActivity(SmppTransactionImpl txImpl) throws ActivityAlreadyExistsException, NullPointerException,
  1000. IllegalStateException, SLEEException, StartActivityException {
  1001. sleeEndpoint.startActivity(txImpl.getHandle(), txImpl, ActivityFlags.REQUEST_ENDED_CALLBACK);
  1002. this.handleVsActivityMap.put(txImpl.getHandle(), txImpl);
  1003. }
  1004. protected void startNewSmppTransactionSuspendedActivity(SmppTransactionImpl txImpl) throws ActivityAlreadyExistsException, NullPointerException,
  1005. IllegalStateException, SLEEException, StartActivityException {
  1006. sleeEndpoint.startActivitySuspended(txImpl.getHandle(), txImpl, ActivityFlags.REQUEST_ENDED_CALLBACK);
  1007. this.handleVsActivityMap.put(txImpl.getHandle(), txImpl);
  1008. }
  1009. protected void sendResponse(ExtSmppResponse response) throws IOException {
  1010. this.protoSmppSession.sendPacket(response.getSMPPPacket());
  1011. //Update usage params
  1012. incrementUsageParams(response.getCommandId());
  1013. }
  1014. protected void sendRequest(ExtSmppRequest request) throws IOException {
  1015. this.protoSmppSession.sendPacket(request.getSMPPPacket());
  1016. //Update usage params
  1017. incrementUsageParams(request.getCommandId());
  1018. }
  1019. protected void fireEvent(String eventName, SmppTransactionImpl activity, Object event) {
  1020. final ActivityHandle handle = this.getActivityHandle(activity);
  1021. if (handle == null) {
  1022. this.tracer.warning("Firing event " + eventName + " for null ActivityHandle Activity=" + activity);
  1023. return;
  1024. }
  1025. final FireableEventType eventType = eventTypeCache.getEventType(eventLookup, eventName);
  1026. if (eventTypeFilter.filterEvent(eventType)) {
  1027. if (tracer.isFineEnabled()) {
  1028. tracer.fine("event " + eventName + " filtered");
  1029. }
  1030. return;
  1031. }
  1032. final String addressStr = this.utils.getAddressForEventFire(activity.getSmppRequest());
  1033. final Address address = new Address(AddressPlan.E164_MOBILE, addressStr);
  1034. try {
  1035. sleeEndpoint.fireEvent(handle, eventType, event, address, null);
  1036. if (tracer.isInfoEnabled()) {
  1037. tracer.info("Fired event: " + eventName);
  1038. }
  1039. } catch (Throwable e) {
  1040. tracer.severe("Failed to fire event", e);
  1041. }
  1042. }
  1043. protected void endActivity(SmppTransactionImpl activity) {
  1044. try {
  1045. this.sleeEndpoint.endActivity(activity.getHandle());
  1046. } catch (Exception e) {
  1047. this.tracer.severe("Error while Ending Activity " + activity, e);
  1048. }
  1049. }
  1050. /**
  1051. * Thread for sending ENQUIRE_LINK
  1052. */
  1053. private class LinkMonitor implements Runnable {
  1054. public void run() {
  1055. if (tracer.isFineEnabled())
  1056. tracer.fine("In LinkMonitor, isRAStoped = " + isRAStoped);
  1057. // We keep trying only of we are bound atleast once and we are Head
  1058. // Member
  1059. while (!isRAStoped) {
  1060. // long currentTime = System.currentTimeMillis();
  1061. try {
  1062. Thread.sleep(enquireLinkTimeout);
  1063. updateStats();
  1064. try {
  1065. EnquireLink sm = new EnquireLink();
  1066. protoSmppSession.sendPacket(sm);
  1067. //Update usage params
  1068. incrementUsageParams(sm.getCommandId());
  1069. if (tracer.isFineEnabled()) {
  1070. tracer.fine("Sent enquire link for " + raContext.getEntityName());
  1071. }
  1072. } catch (NotBoundException nbe) {
  1073. if (tracer.isWarningEnabled())
  1074. tracer.warning("Connection lost! for RA " + raContext.getEntityName() + "Reconnecting...", nbe);
  1075. reconnect();
  1076. } catch (IOException ie) {
  1077. if (tracer.isSevereEnabled())
  1078. tracer.severe("Connection lost! for RA " + raContext.getEntityName() + " Communication failed", ie);
  1079. reconnect();
  1080. } catch (BadCommandIDException ex) {
  1081. tracer.severe("BadCommandIDException. Failed to enquire link ", ex);
  1082. } catch (VersionException ex) {
  1083. if (tracer.isSevereEnabled())
  1084. tracer.severe("Failed to enquire link due to wrong Version ", ex);
  1085. }
  1086. } catch (InterruptedException e) {
  1087. if (tracer.isInfoEnabled())
  1088. tracer.info("Terminate link monitor: " + raContext.getEntityName());
  1089. }
  1090. }// while loop
  1091. }
  1092. }
  1093. protected ResourceAdaptorContext getRAContext() {
  1094. return this.raContext;
  1095. }
  1096. public ConcurrentHashMap<SmppTransactionHandle, SmppTransactionImpl> getHandleVsActivityMap() {
  1097. return handleVsActivityMap;
  1098. }
  1099. }