PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/jain-slee/resources/jcc/jcc-camel-provider/java/src/main/java/org/mobicents/jcc/inap/JccInapProviderImpl.java

http://mobicents.googlecode.com/
Java | 534 lines | 291 code | 76 blank | 167 comment | 5 complexity | aedd54deec37b2023fe183c9cb477206 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. /*
  23. * The Java Call Control API for CAMEL 2
  24. *
  25. * The source code contained in this file is in in the public domain.
  26. * It can be used in any project or product without prior permission,
  27. * license or royalty payments. There is NO WARRANTY OF ANY KIND,
  28. * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, WITHOUT LIMITATION,
  29. * THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  30. * AND DATA ACCURACY. We do not warrant or make any representations
  31. * regarding the use of the software or the results thereof, including
  32. * but not limited to the correctness, accuracy, reliability or
  33. * usefulness of the software.
  34. */
  35. package org.mobicents.jcc.inap;
  36. import java.util.ArrayList;
  37. import java.util.Properties;
  38. import javax.csapi.cc.jcc.CallLoadControlListener;
  39. import javax.csapi.cc.jcc.EventFilter;
  40. import javax.csapi.cc.jcc.InvalidArgumentException;
  41. import javax.csapi.cc.jcc.InvalidPartyException;
  42. import javax.csapi.cc.jcc.InvalidStateException;
  43. import javax.csapi.cc.jcc.JccAddress;
  44. import javax.csapi.cc.jcc.JccCall;
  45. import javax.csapi.cc.jcc.JccCallListener;
  46. import javax.csapi.cc.jcc.JccConnectionListener;
  47. import javax.csapi.cc.jcc.JccProvider;
  48. import javax.csapi.cc.jcc.JccProviderListener;
  49. import javax.csapi.cc.jcc.MethodNotSupportedException;
  50. import javax.csapi.cc.jcc.PrivilegeViolationException;
  51. import javax.csapi.cc.jcc.ProviderUnavailableException;
  52. import javax.csapi.cc.jcc.ResourceUnavailableException;
  53. import org.apache.log4j.Logger;
  54. import org.mobicents.jcc.inap.address.JccCalledPartyBCDNumber;
  55. import org.mobicents.jcc.inap.address.JccCalledPartyNumber;
  56. import org.mobicents.jcc.inap.address.JccCallingPartyNumber;
  57. import org.mobicents.jcc.inap.protocol.parms.CalledPartyBcdNumber;
  58. import org.mobicents.jcc.inap.protocol.parms.CalledPartyNumber;
  59. import org.mobicents.jcc.inap.protocol.parms.CallingPartyNumber;
  60. import org.mobicents.protocols.ss7.tcap.TCAPStackImpl;
  61. import org.mobicents.protocols.ss7.tcap.api.TCAPProvider;
  62. import org.mobicents.protocols.ss7.tcap.api.TCAPStack;
  63. import org.mobicents.protocols.ss7.tcap.api.TCListener;
  64. import org.mobicents.protocols.ss7.tcap.api.tc.dialog.Dialog;
  65. import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCBeginIndication;
  66. import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCContinueIndication;
  67. import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCEndIndication;
  68. import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCPAbortIndication;
  69. import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCUniIndication;
  70. import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCUserAbortIndication;
  71. import org.mobicents.protocols.ss7.tcap.asn.comp.Invoke;
  72. import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
  73. import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
  74. import javax.naming.InitialContext;
  75. import org.mobicents.protocols.ss7.sccp.SccpProvider;
  76. import org.mobicents.protocols.ss7.sccp.parameter.SccpAddress;
  77. /**
  78. *
  79. * @author Oleg Kulikov
  80. */
  81. public class JccInapProviderImpl implements JccProvider, TCListener {
  82. public final static int DEFAULT_POOL_SIZE = 10;
  83. protected final static String name = "Java call control provider for INAP 1.1";
  84. protected ArrayList callListeners = new ArrayList();
  85. protected ArrayList connectionListeners = new ArrayList();
  86. private int state = JccProvider.OUT_OF_SERVICE;
  87. private SccpProvider sccpProvider;
  88. protected TCAPStack tcapStack = null;
  89. protected TCAPProvider tcapProvider = null;
  90. protected ConcurrentReaderHashMap calls = new ConcurrentReaderHashMap();
  91. protected ConcurrentReaderHashMap connections = new ConcurrentReaderHashMap();
  92. private Properties properties;
  93. private PooledExecutor threadPool;
  94. private Logger logger = Logger.getLogger(JccInapProviderImpl.class);
  95. private Thread monitor;
  96. private boolean stopped = false;
  97. /** Creates a new instance of JccInapProviderImpl */
  98. public JccInapProviderImpl(Properties properties) {
  99. this.properties = properties;
  100. //check for SCCP jndi name
  101. if (properties.getProperty("sccp.service") == null) {
  102. throw new ProviderUnavailableException("SCCP service JNDI name not specified");
  103. }
  104. //obtain SCCP service JNDI name
  105. String jndiName = properties.getProperty("sccp.service");
  106. //obtain sccp service reference
  107. logger.info("Accesing SCCP service...");
  108. try {
  109. InitialContext ic = new InitialContext();
  110. sccpProvider = (SccpProvider) ic.lookup(jndiName);
  111. logger.info("Sucssefully connected to SCCP service[" + jndiName + "]");
  112. } catch (Exception e) {
  113. throw new ProviderUnavailableException(e.getMessage());
  114. }
  115. //construct SCCP address
  116. // SccpAddressDesc desc = new SccpAddressDesc();
  117. // SccpAddress address = desc.load(properties);
  118. int ssn = Integer.parseInt(properties.getProperty("sccp.ssn"));
  119. logger.info("Local SSN: " + ssn);
  120. try {
  121. this.tcapStack = new TCAPStackImpl(sccpProvider, ssn);
  122. this.tcapProvider = this.tcapStack.getProvider();
  123. this.tcapProvider.addTCListener(this);
  124. this.tcapStack.start();
  125. //sccpProvider.setSccpListener(this);
  126. logger.info("Initialized SCCP provider");
  127. int poolSize = DEFAULT_POOL_SIZE;
  128. if (properties.getProperty("thread.pool.size") != null) {
  129. try {
  130. poolSize = Integer.parseInt(properties.getProperty("thread.pool.size"));
  131. } catch (NumberFormatException e) {
  132. logger.warn("Use default pool size", e);
  133. }
  134. }
  135. threadPool = new PooledExecutor(poolSize);
  136. logger.info("Initialized thread pool");
  137. state = JccProvider.IN_SERVICE;
  138. monitor = new Thread(new Monitor());
  139. monitor.start();
  140. logger.info("Started monitor");
  141. } catch (Exception e) {
  142. logger.error("Could not load JccProvider", e);
  143. throw new ProviderUnavailableException(e.getMessage());
  144. }
  145. }
  146. /**
  147. * (Non java-doc)
  148. *
  149. * @see javax.csapi.cc.jcc.JccProvier#addCallListener.
  150. */
  151. public void addCallListener(JccCallListener listener)
  152. throws MethodNotSupportedException, ResourceUnavailableException {
  153. callListeners.add(listener);
  154. }
  155. /**
  156. * (Non java-doc)
  157. *
  158. * @see javax.csapi.cc.jcc.JccProvier#addCallLoadControlListener.
  159. */
  160. public void addCallLoadControlListener(CallLoadControlListener listener)
  161. throws MethodNotSupportedException, ResourceUnavailableException {
  162. throw new MethodNotSupportedException("Call load controll not supported");
  163. }
  164. /**
  165. * (Non java-doc)
  166. *
  167. * @see javax.csapi.cc.jcc.JccProvier#addConnectionListener.
  168. */
  169. public void addConnectionListener(JccConnectionListener listener, EventFilter filter)
  170. throws ResourceUnavailableException, MethodNotSupportedException {
  171. Object[] ls = new Object[2];
  172. ls[0] = listener;
  173. ls[1] = new DefaultFilter();
  174. connectionListeners.add(ls);
  175. }
  176. public void addProviderListener(JccProviderListener providerlistener)
  177. throws ResourceUnavailableException, MethodNotSupportedException {
  178. throw new MethodNotSupportedException();
  179. }
  180. /**
  181. * (Non Java-doc).
  182. * @see javax.csapi.cc.jcc.JccProvider#createCall().
  183. */
  184. public JccCall createCall()
  185. throws InvalidStateException, ResourceUnavailableException, PrivilegeViolationException, MethodNotSupportedException {
  186. return null;
  187. }
  188. public EventFilter createEventFilterAddressRange(String lowAddress, String highAddress, int matchDisposition, int nomatchDisposition)
  189. throws ResourceUnavailableException, InvalidArgumentException {
  190. return null;
  191. }
  192. public EventFilter createEventFilterAddressRegEx(String addressRegex, int matchDisposition, int nomatchDisposition)
  193. throws ResourceUnavailableException, InvalidArgumentException {
  194. return null;
  195. }
  196. public EventFilter createEventFilterAnd(EventFilter[] filters, int nomatchDisposition)
  197. throws ResourceUnavailableException, InvalidArgumentException {
  198. return null;
  199. }
  200. public EventFilter createEventFilterCauseCode(int causeCode, int matchDisposition, int nomatchDisposition)
  201. throws ResourceUnavailableException, InvalidArgumentException {
  202. return null;
  203. }
  204. public EventFilter createEventFilterDestAddressRange(
  205. String lowDestAddress, String highDestAddress, int matchDisposition, int nomatchDisposition) throws ResourceUnavailableException, InvalidArgumentException {
  206. return null;
  207. }
  208. public EventFilter createEventFilterDestAddressRegEx(
  209. String destAddressRegex, int matchDisposition, int nomatchDisposition) throws ResourceUnavailableException, InvalidArgumentException {
  210. return null;
  211. }
  212. public EventFilter createEventFilterEventSet(
  213. int[] blockEvents, int[] notifyEvents) throws ResourceUnavailableException, InvalidArgumentException {
  214. return null;
  215. }
  216. public EventFilter createEventFilterMidCallEvent(
  217. int midCallType, String midCallValue, int matchDisposition, int nomatchDisposition) throws ResourceUnavailableException, InvalidArgumentException {
  218. return null;
  219. }
  220. public EventFilter createEventFilterMinimunCollectedAddressLength(
  221. int minimumAddressLength, int matchDisposition, int nomatchDisposition) throws ResourceUnavailableException, InvalidArgumentException {
  222. return null;
  223. }
  224. public EventFilter createEventFilterOr(
  225. EventFilter[] filters, int nomatchDisposition) throws ResourceUnavailableException, InvalidArgumentException {
  226. return null;
  227. }
  228. public EventFilter createEventFilterOrigAddressRange(
  229. String lowOrigAddress, String highOrigAddress, int matchDisposition, int nomatchDisposition) throws ResourceUnavailableException, InvalidArgumentException {
  230. return null;
  231. }
  232. public EventFilter createEventFilterOrigAddressRegEx(
  233. String origAddressRegex, int matchDisposition, int nomatchDisposition) throws ResourceUnavailableException, InvalidArgumentException {
  234. return null;
  235. }
  236. public JccAddress getAddress(String address) throws InvalidPartyException {
  237. return null;
  238. }
  239. public String getName() {
  240. return name;
  241. }
  242. public int getState() {
  243. return state;
  244. }
  245. public void removeCallListener(JccCallListener calllistener) {
  246. }
  247. public void removeCallLoadControlListener(CallLoadControlListener loadcontrollistener) {
  248. }
  249. public void removeConnectionListener(JccConnectionListener connectionlistener) {
  250. }
  251. public void removeProviderListener(JccProviderListener providerlistener) {
  252. }
  253. public void setCallLoadControl(JccAddress[] address, double duration, double[] mechanism, int[] treatment) throws MethodNotSupportedException {
  254. }
  255. public void shutdown() {
  256. this.stopped = true;
  257. this.callListeners.clear();
  258. this.connectionListeners.clear();
  259. this.calls.clear();
  260. this.connections.clear();
  261. this.threadPool.shutdownNow();
  262. //sccpProvider.shutdown();
  263. this.tcapStack.stop();
  264. this.state = JccProvider.SHUTDOWN;
  265. this.stopped = true;
  266. }
  267. // public void onMessage(SccpAddress calledPartyAddress, SccpAddress callingPartyAddress, byte[] data) {
  268. // logger.info(" Received MSG: "+data.length+" --> "+calledPartyAddress+" --> "+callingPartyAddress);
  269. // ByteArrayInputStream in = new ByteArrayInputStream(data);
  270. //
  271. // int mt = in.read() & 0xff;
  272. // int len = 0;
  273. //
  274. // try {
  275. // len = Util.readLen(in);
  276. // } catch (IOException e) {
  277. // logger.error("Could not decode length of the TC_MESSAGE:", e);
  278. // return;
  279. // }
  280. //
  281. // byte[] buffer = new byte[len];
  282. // try {
  283. // in.read(buffer);
  284. // } catch (IOException e) {
  285. // }
  286. //
  287. // TCMessage tcMessage = null;
  288. // switch (mt) {
  289. // case TCMessage.BEGIN :
  290. // try {
  291. // tcMessage = new TCBegin(buffer);
  292. // } catch (IOException e) {
  293. // logger.error("Could not decode TC_BEGIN", e);
  294. // return;
  295. // }
  296. // break;
  297. // case TCMessage.CONTINUE :
  298. // try {
  299. // tcMessage = new TCContinue(buffer);
  300. // } catch (IOException e) {
  301. // logger.error("Could not decode TC_CONTINUE", e);
  302. // return;
  303. // }
  304. // break;
  305. // case TCMessage.ABORT :
  306. // try {
  307. // tcMessage = new TCAbort(buffer);
  308. // } catch (IOException e) {
  309. // logger.error("Could not decode TC_ABORT", e);
  310. // return;
  311. // }
  312. // break;
  313. // case TCMessage.END :
  314. // try {
  315. // tcMessage = new TCAbort(buffer);
  316. // } catch (IOException e) {
  317. // logger.error("Could not decode TC_END", e);
  318. // return;
  319. // }
  320. // break;
  321. // default :
  322. // logger.warn("Unexpected TCMessage: " + Integer.toHexString(mt) + "h");
  323. // return;
  324. // }
  325. //
  326. // if (logger.isDebugEnabled()) {
  327. // logger.debug("--> " + tcMessage);
  328. // }
  329. //
  330. // TCHandler handler = new TCHandler(this, calledPartyAddress, callingPartyAddress, tcMessage);
  331. // try {
  332. // threadPool.execute(handler);
  333. // } catch (InterruptedException e) {
  334. // }
  335. // }
  336. // protected synchronized void send(SccpAddress calledParty, SccpAddress callingParty, TCMessage msg) throws IOException {
  337. // sccpProvider.send(calledParty, callingParty, msg.toByteArray());
  338. // if (logger.isDebugEnabled()) {
  339. // logger.debug("<-- " + msg);
  340. // }
  341. // }
  342. protected JccCallImpl getCall(JccAddress callingNumber) {
  343. JccCallImpl call = (JccCallImpl) calls.get(callingNumber.getName());
  344. return call;
  345. }
  346. protected AbstractConnection getConnection(long connectionID) {
  347. return (AbstractConnection) connections.get(new Long(connectionID));
  348. }
  349. protected synchronized JccAddress createAddress(CalledPartyNumber cpn) {
  350. return new JccCalledPartyNumber(this, cpn);
  351. }
  352. protected synchronized JccAddress createAddress(CalledPartyBcdNumber cpn) {
  353. return new JccCalledPartyBCDNumber(this, cpn);
  354. }
  355. protected synchronized JccAddress createAddress(CallingPartyNumber cpn) {
  356. return new JccCallingPartyNumber(this, cpn);
  357. }
  358. protected JccCallImpl createCall(JccAddress callingNumber) {
  359. return new JccCallImpl(this, callingNumber);
  360. }
  361. //NOTE: for test, we are server, we dont care
  362. /* (non-Javadoc)
  363. * @see org.mobicents.protocols.ss7.tcap.api.TCListener#dialogReleased(org.mobicents.protocols.ss7.tcap.api.tc.dialog.Dialog)
  364. */
  365. public void dialogReleased(Dialog d) {
  366. logger.info("Dialog has been released. Id: " + d.getDialogId());
  367. }
  368. /* (non-Javadoc)
  369. * @see org.mobicents.protocols.ss7.tcap.api.TCListener#onInvokeTimeout(org.mobicents.protocols.ss7.tcap.asn.comp.Invoke)
  370. */
  371. public void onInvokeTimeout(Invoke tcInvokeRequest) {
  372. logger.info("Invocation has timed out! ID: " + tcInvokeRequest.getInvokeId());
  373. //this is called for local tc invoke
  374. }
  375. /* (non-Javadoc)
  376. * @see org.mobicents.protocols.ss7.tcap.api.TCListener#onTCBegin(org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCBeginIndication)
  377. */
  378. public void onTCBegin(TCBeginIndication ind) {
  379. logger.info("Received TC Begin! Dialog ID: " + ind.getDialog().getDialogId());
  380. TCHandler handler = new TCHandler(this, this.tcapProvider, ind);
  381. try {
  382. threadPool.execute(handler);
  383. } catch (InterruptedException e) {
  384. }
  385. }
  386. /* (non-Javadoc)
  387. * @see org.mobicents.protocols.ss7.tcap.api.TCListener#onTCContinue(org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCContinueIndication)
  388. */
  389. public void onTCContinue(TCContinueIndication ind) {
  390. logger.info("Received TC Continue! Dialog ID: " + ind.getDialog().getDialogId());
  391. TCHandler handler = new TCHandler(this, this.tcapProvider, ind);
  392. try {
  393. threadPool.execute(handler);
  394. } catch (InterruptedException e) {
  395. }
  396. }
  397. /* (non-Javadoc)
  398. * @see org.mobicents.protocols.ss7.tcap.api.TCListener#onTCEnd(org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCEndIndication)
  399. */
  400. public void onTCEnd(TCEndIndication ind) {
  401. logger.info("Received TC End! Dialog ID: " + ind.getDialog().getDialogId());
  402. TCHandler handler = new TCHandler(this, this.tcapProvider, ind);
  403. try {
  404. threadPool.execute(handler);
  405. } catch (InterruptedException e) {
  406. }
  407. }
  408. /* (non-Javadoc)
  409. * @see org.mobicents.protocols.ss7.tcap.api.TCListener#onTCUni(org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCUniIndication)
  410. */
  411. public void onTCUni(TCUniIndication ind) {
  412. logger.info("Received TC Uni! Dialog ID: " + ind.getDialog().getDialogId());
  413. }
  414. public void onTCPAbort(TCPAbortIndication ind) {
  415. logger.info("Received TC PAbort! Dialog ID: " + ind.getDialog().getDialogId());
  416. TCHandler handler = new TCHandler(this, this.tcapProvider, ind);
  417. try {
  418. threadPool.execute(handler);
  419. } catch (InterruptedException e) {
  420. }
  421. }
  422. public void onTCUserAbort(TCUserAbortIndication ind) {
  423. logger.info("Received TC UAbort! Dialog ID: " + ind.getDialog().getDialogId());
  424. TCHandler handler = new TCHandler(this, this.tcapProvider, ind);
  425. try {
  426. threadPool.execute(handler);
  427. } catch (InterruptedException e) {
  428. }
  429. }
  430. /* (non-Javadoc)
  431. * @see org.mobicents.protocols.ss7.tcap.api.TCListener#onDialogReleased(org.mobicents.protocols.ss7.tcap.api.tc.dialog.Dialog)
  432. */
  433. @Override
  434. public void onDialogReleased(Dialog dialog) {
  435. logger.info("Received Dialog Release! Dialog ID: " + dialog.getDialogId());
  436. }
  437. /* (non-Javadoc)
  438. * @see org.mobicents.protocols.ss7.tcap.api.TCListener#onDialogTimeout(org.mobicents.protocols.ss7.tcap.api.tc.dialog.Dialog)
  439. */
  440. @Override
  441. public void onDialogTimeout(Dialog dialog) {
  442. logger.warn("Received Dialog Timeout! Dialog ID: " + dialog.getDialogId());
  443. }
  444. private class Monitor implements Runnable {
  445. @SuppressWarnings("static-access")
  446. public void run() {
  447. while (!stopped) {
  448. try {
  449. Thread.currentThread().sleep(60000);
  450. logger.info("active calls:" + calls.size() +
  451. ", active connections: " + connections.size());
  452. } catch (InterruptedException e) {
  453. stopped = true;
  454. }
  455. }
  456. }
  457. }
  458. }