/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/as/LocalAspFactory.java

http://mobicents.googlecode.com/ · Java · 703 lines · 552 code · 94 blank · 57 comment · 104 complexity · f6603e046a4218c65f879a69808293ea MD5 · raw file

  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.protocols.ss7.m3ua.impl.as;
  23. import java.io.IOException;
  24. import javolution.util.FastList;
  25. import javolution.xml.XMLFormat;
  26. import javolution.xml.stream.XMLStreamException;
  27. import org.apache.log4j.Level;
  28. import org.apache.log4j.Logger;
  29. import org.mobicents.protocols.ss7.m3ua.M3UAProvider;
  30. import org.mobicents.protocols.ss7.m3ua.impl.As;
  31. import org.mobicents.protocols.ss7.m3ua.impl.Asp;
  32. import org.mobicents.protocols.ss7.m3ua.impl.AspFactory;
  33. import org.mobicents.protocols.ss7.m3ua.impl.AspState;
  34. import org.mobicents.protocols.ss7.m3ua.impl.M3UAManagement;
  35. import org.mobicents.protocols.ss7.m3ua.impl.TransitionState;
  36. import org.mobicents.protocols.ss7.m3ua.impl.fsm.UnknownTransitionException;
  37. import org.mobicents.protocols.ss7.m3ua.message.M3UAMessage;
  38. import org.mobicents.protocols.ss7.m3ua.message.MessageClass;
  39. import org.mobicents.protocols.ss7.m3ua.message.MessageType;
  40. import org.mobicents.protocols.ss7.m3ua.message.aspsm.ASPDown;
  41. import org.mobicents.protocols.ss7.m3ua.message.aspsm.ASPDownAck;
  42. import org.mobicents.protocols.ss7.m3ua.message.aspsm.ASPUp;
  43. import org.mobicents.protocols.ss7.m3ua.message.aspsm.ASPUpAck;
  44. import org.mobicents.protocols.ss7.m3ua.message.aspsm.Heartbeat;
  45. import org.mobicents.protocols.ss7.m3ua.message.aspsm.HeartbeatAck;
  46. import org.mobicents.protocols.ss7.m3ua.message.asptm.ASPActive;
  47. import org.mobicents.protocols.ss7.m3ua.message.asptm.ASPActiveAck;
  48. import org.mobicents.protocols.ss7.m3ua.message.asptm.ASPInactiveAck;
  49. import org.mobicents.protocols.ss7.m3ua.message.mgmt.Notify;
  50. import org.mobicents.protocols.ss7.m3ua.message.ssnm.DestinationAvailable;
  51. import org.mobicents.protocols.ss7.m3ua.message.ssnm.DestinationUPUnavailable;
  52. import org.mobicents.protocols.ss7.m3ua.message.ssnm.DestinationUnavailable;
  53. import org.mobicents.protocols.ss7.m3ua.message.ssnm.SignallingCongestion;
  54. import org.mobicents.protocols.ss7.m3ua.message.transfer.PayloadData;
  55. import org.mobicents.protocols.ss7.m3ua.parameter.ASPIdentifier;
  56. import org.mobicents.protocols.ss7.m3ua.parameter.AffectedPointCode;
  57. import org.mobicents.protocols.ss7.m3ua.parameter.CongestedIndication;
  58. import org.mobicents.protocols.ss7.m3ua.parameter.CongestedIndication.CongestionLevel;
  59. import org.mobicents.protocols.ss7.m3ua.parameter.RoutingContext;
  60. import org.mobicents.protocols.ss7.m3ua.parameter.TrafficModeType;
  61. import org.mobicents.protocols.ss7.m3ua.parameter.UserCause;
  62. import org.mobicents.protocols.ss7.mtp.Mtp3PausePrimitive;
  63. import org.mobicents.protocols.ss7.mtp.Mtp3ResumePrimitive;
  64. import org.mobicents.protocols.ss7.mtp.Mtp3StatusPrimitive;
  65. /**
  66. *
  67. * @author amit bhayani
  68. *
  69. */
  70. public class LocalAspFactory extends AspFactory {
  71. private static final Logger logger = Logger.getLogger(LocalAspFactory.class);
  72. private static final String REM_IP = "remIp";
  73. private static final String REM_PORT = "remPort";
  74. //private static final String CHANNEL_CONNECTED = "chanConn";
  75. private static long ASP_ID = 1l;
  76. private String remIp;
  77. private int remPort;
  78. boolean channelConnected = false;
  79. private ASPIdentifier aspid;
  80. public LocalAspFactory() {
  81. super();
  82. }
  83. public LocalAspFactory(String name, String localIp, int localPort, String remIp, int remPort, M3UAProvider provider, M3UAManagement m3uaManagement) {
  84. super(name, localIp, localPort, provider, m3uaManagement);
  85. this.remIp = remIp;
  86. this.remPort = remPort;
  87. this.aspid = this.m3UAProvider.getParameterFactory().createASPIdentifier(this.generateId());
  88. }
  89. public String getRemIp() {
  90. return remIp;
  91. }
  92. public int getRemPort() {
  93. return remPort;
  94. }
  95. @Override
  96. public Asp createAsp() {
  97. AspImpl remAsp = new AspImpl(this.name, this.m3UAProvider, this);
  98. remAsp.setASPIdentifier(aspid);
  99. this.aspList.add(remAsp);
  100. return remAsp;
  101. }
  102. @Override
  103. public void start() {
  104. this.started = true;
  105. }
  106. @Override
  107. public void stop() {
  108. this.started = false;
  109. if (this.channel != null) {
  110. try {
  111. this.channelConnected = this.channel.isConnected();
  112. } catch (IOException e1) {
  113. logger.error(String.format("Error while checking if channel is connected for LocalAspfactory=%s",
  114. this.name));
  115. }
  116. }
  117. if (this.channelConnected) {
  118. ASPDown aspDown = (ASPDown) this.m3UAProvider.getMessageFactory().createMessage(
  119. MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_DOWN);
  120. this.write(aspDown);
  121. for (FastList.Node<Asp> n = aspList.head(), end = aspList.tail(); (n = n.getNext()) != end;) {
  122. Asp asp = n.getValue();
  123. try {
  124. asp.getFSM().signal(TransitionState.ASP_DOWN_SENT);
  125. As as = asp.getAs();
  126. as.aspStateChange(asp, TransitionState.ASP_DOWN);
  127. } catch (UnknownTransitionException e) {
  128. logger.error(e.getMessage(), e);
  129. }
  130. }
  131. } else {
  132. for (FastList.Node<Asp> n = aspList.head(), end = aspList.tail(); (n = n.getNext()) != end;) {
  133. Asp asp = n.getValue();
  134. try {
  135. asp.getFSM().signal(TransitionState.COMM_DOWN);
  136. As as = asp.getAs();
  137. as.aspStateChange(asp, TransitionState.ASP_DOWN);
  138. } catch (UnknownTransitionException e) {
  139. logger.error(e.getMessage(), e);
  140. }
  141. }
  142. }
  143. }
  144. @Override
  145. public void read(M3UAMessage message) {
  146. switch (message.getMessageClass()) {
  147. case MessageClass.MANAGEMENT:
  148. switch (message.getMessageType()) {
  149. case MessageType.ERROR:
  150. logger.error(message);
  151. break;
  152. case MessageType.NOTIFY:
  153. Notify notify = (Notify) message;
  154. this.handleNotify(notify);
  155. break;
  156. default:
  157. logger.error(String.format("Received MGMT with invalid MessageType=%d message=%s",
  158. message.getMessageType(), message));
  159. break;
  160. }
  161. break;
  162. case MessageClass.TRANSFER_MESSAGES:
  163. switch (message.getMessageType()) {
  164. case MessageType.PAYLOAD:
  165. PayloadData payload = (PayloadData) message;
  166. this.handlePayload(payload);
  167. break;
  168. default:
  169. logger.error(String.format("Received Transfer message with invalid MessageType=%d message=%s",
  170. message.getMessageType(), message));
  171. break;
  172. }
  173. break;
  174. case MessageClass.SIGNALING_NETWORK_MANAGEMENT:
  175. switch (message.getMessageType()) {
  176. case MessageType.DESTINATION_UNAVAILABLE:
  177. DestinationUnavailable duna = (DestinationUnavailable) message;
  178. this.handleDestinationUnavailable(duna);
  179. break;
  180. case MessageType.DESTINATION_AVAILABLE:
  181. DestinationAvailable dava = (DestinationAvailable) message;
  182. this.handleDestinationAvailable(dava);
  183. break;
  184. case MessageType.DESTINATION_STATE_AUDIT:
  185. if (logger.isEnabledFor(Level.WARN)) {
  186. logger.warn(String.format("Received DAUD message for AS side. This is error. Message=%s", message));
  187. }
  188. break;
  189. case MessageType.SIGNALING_CONGESTION:
  190. SignallingCongestion scon = (SignallingCongestion) message;
  191. this.handleSignallingCongestion(scon);
  192. break;
  193. case MessageType.DESTINATION_USER_PART_UNAVAILABLE:
  194. DestinationUPUnavailable dupu = (DestinationUPUnavailable) message;
  195. this.handleDestinationUPUnavailable(dupu);
  196. break;
  197. case MessageType.DESTINATION_RESTRICTED:
  198. if (logger.isEnabledFor(Level.WARN)) {
  199. logger.warn(String.format("Received DUPU message for AS side. Not implemented yet", message));
  200. }
  201. break;
  202. default:
  203. logger.error(String.format("Received SSNM with invalid MessageType=%d message=%s",
  204. message.getMessageType(), message));
  205. break;
  206. }
  207. break;
  208. case MessageClass.ASP_STATE_MAINTENANCE:
  209. switch (message.getMessageType()) {
  210. case MessageType.ASP_UP_ACK:
  211. ASPUpAck aspUpAck = (ASPUpAck) message;
  212. this.handleAspUpAck(aspUpAck);
  213. break;
  214. case MessageType.ASP_DOWN_ACK:
  215. ASPDownAck aspDownAck = (ASPDownAck) message;
  216. this.handleAspDownAck(aspDownAck);
  217. break;
  218. case MessageType.HEARTBEAT:
  219. Heartbeat hrtBeat = (Heartbeat) message;
  220. this.handleHeartbeat(hrtBeat);
  221. break;
  222. default:
  223. logger.error(String.format("Received ASPSM with invalid MessageType=%d message=%s",
  224. message.getMessageType(), message));
  225. break;
  226. }
  227. break;
  228. case MessageClass.ASP_TRAFFIC_MAINTENANCE:
  229. switch (message.getMessageType()) {
  230. case MessageType.ASP_ACTIVE_ACK:
  231. ASPActiveAck aspAciveAck = (ASPActiveAck) message;
  232. this.handleAspActiveAck(aspAciveAck);
  233. break;
  234. case MessageType.ASP_INACTIVE_ACK:
  235. ASPInactiveAck aspInaciveAck = (ASPInactiveAck) message;
  236. this.handleAspInactiveAck(aspInaciveAck);
  237. break;
  238. default:
  239. logger.error(String.format("Received ASPTM with invalid MessageType=%d message=%s",
  240. message.getMessageType(), message));
  241. break;
  242. }
  243. break;
  244. case MessageClass.ROUTING_KEY_MANAGEMENT:
  245. logger.error(String.format("Received %s. Handling of RKM message is not supported", message));
  246. break;
  247. default:
  248. logger.error(String.format("Received message with invalid MessageClass=%d message=%s",
  249. message.getMessageClass(), message));
  250. break;
  251. }
  252. }
  253. private void handleDestinationUPUnavailable(DestinationUPUnavailable dupu) {
  254. RoutingContext rcObj = dupu.getRoutingContext();
  255. if (rcObj == null) {
  256. logger.error(String.format("received DUPU but no RoutingContext carried in message. Message=%s", dupu));
  257. // TODO : If no RC defined, should send to all AS?
  258. return;
  259. }
  260. long rc = rcObj.getRoutingContexts()[0];
  261. Asp asp = this.getAsp(rc);
  262. if (asp == null) {
  263. logger.error(String.format("received DUPU for RoutingContext=%d. But no ASP found. Message=%s", rc, dupu));
  264. // TODO : If no RC defined, should send to all AS?
  265. return;
  266. }
  267. if (asp.getState() == AspState.ACTIVE) {
  268. AffectedPointCode affectedPcObjs = dupu.getAffectedPointCode();
  269. int[] affectedPcs = affectedPcObjs.getPointCodes();
  270. int cause = 0;
  271. for (int i = 0; i < affectedPcs.length; i++) {
  272. UserCause userCause = dupu.getUserCause();
  273. cause = userCause.getCause();
  274. Mtp3StatusPrimitive mtpPausePrimi = new Mtp3StatusPrimitive(affectedPcs[i], 1, 0, cause);
  275. asp.getAs().received(mtpPausePrimi);
  276. }
  277. } else {
  278. logger.error(String.format("Received DUPU for RoutingContext=%d. But ASP State=%s. Message=%s", rc,
  279. asp.getState(), dupu));
  280. }
  281. }
  282. private void handleSignallingCongestion(SignallingCongestion scon) {
  283. RoutingContext rcObj = scon.getRoutingContexts();
  284. if (rcObj == null) {
  285. logger.error(String.format("received SCON but no RoutingContext carried in message. Message=%s", scon));
  286. // TODO : If no RC defined, should send to all AS?
  287. return;
  288. }
  289. long rc = rcObj.getRoutingContexts()[0];
  290. Asp asp = this.getAsp(rc);
  291. if (asp == null) {
  292. logger.error(String.format("received SCON for RoutingContext=%d. But no ASP found. Message=%s", rc, scon));
  293. // TODO : If no RC defined, should send to all AS?
  294. return;
  295. }
  296. if (asp.getState() == AspState.ACTIVE) {
  297. AffectedPointCode affectedPcObjs = scon.getAffectedPointCodes();
  298. int[] affectedPcs = affectedPcObjs.getPointCodes();
  299. int cong = 0;
  300. for (int i = 0; i < affectedPcs.length; i++) {
  301. CongestedIndication congeInd = scon.getCongestedIndication();
  302. if (congeInd != null) {
  303. CongestionLevel congLevel = congeInd.getCongestionLevel();
  304. if (congLevel != null) {
  305. cong = congLevel.getLevel();
  306. }
  307. }
  308. Mtp3StatusPrimitive mtpPausePrimi = new Mtp3StatusPrimitive(affectedPcs[i], 2, cong, 0);
  309. asp.getAs().received(mtpPausePrimi);
  310. }
  311. } else {
  312. logger.error(String.format("Received SCON for RoutingContext=%d. But ASP State=%s. Message=%s", rc,
  313. asp.getState(), scon));
  314. }
  315. }
  316. private void handleDestinationUnavailable(DestinationUnavailable duna) {
  317. RoutingContext rcObj = duna.getRoutingContexts();
  318. if (rcObj == null) {
  319. logger.error(String.format("received DUNA but no RoutingContext carried in message. Message=%s", duna));
  320. // TODO : If no RC defined, should send to all AS?
  321. return;
  322. }
  323. long rc = rcObj.getRoutingContexts()[0];
  324. Asp asp = this.getAsp(rc);
  325. if (asp == null) {
  326. logger.error(String.format("received DUNA for RoutingContext=%d. But no ASP found. Message=%s", rc, duna));
  327. // TODO : If no RC defined, should send to all AS?
  328. return;
  329. }
  330. if (asp.getState() == AspState.ACTIVE) {
  331. AffectedPointCode affectedPcObjs = duna.getAffectedPointCodes();
  332. int[] affectedPcs = affectedPcObjs.getPointCodes();
  333. for (int i = 0; i < affectedPcs.length; i++) {
  334. Mtp3PausePrimitive mtpPausePrimi = new Mtp3PausePrimitive(affectedPcs[i]);
  335. asp.getAs().received(mtpPausePrimi);
  336. }
  337. } else {
  338. logger.error(String.format("Received DUNA for RoutingContext=%d. But ASP State=%s. Message=%s", rc,
  339. asp.getState(), duna));
  340. }
  341. }
  342. private void handleDestinationAvailable(DestinationAvailable dava) {
  343. RoutingContext rcObj = dava.getRoutingContexts();
  344. if (rcObj == null) {
  345. logger.error(String.format("received DAVA but no RoutingContext carried in message. Message=%s", dava));
  346. // TODO : If no RC defined, should send to all AS?
  347. return;
  348. }
  349. long rc = rcObj.getRoutingContexts()[0];
  350. Asp asp = this.getAsp(rc);
  351. if (asp == null) {
  352. logger.error(String.format("received DAVA for RoutingContext=%d. But no ASP found. Message=%s", rc, dava));
  353. // TODO : If no RC defined, should send to all AS?
  354. return;
  355. }
  356. if (asp.getState() == AspState.ACTIVE) {
  357. AffectedPointCode affectedPcObjs = dava.getAffectedPointCodes();
  358. int[] affectedPcs = affectedPcObjs.getPointCodes();
  359. for (int i = 0; i < affectedPcs.length; i++) {
  360. Mtp3ResumePrimitive mtpResumePrimi = new Mtp3ResumePrimitive(affectedPcs[i]);
  361. asp.getAs().received(mtpResumePrimi);
  362. }
  363. } else {
  364. logger.error(String.format("Received DAVA for RoutingContext=%d. But ASP State=%s. Message=%s", rc,
  365. asp.getState(), dava));
  366. }
  367. }
  368. private void handlePayload(PayloadData payload) {
  369. // Payload is always for single AS
  370. long rc = payload.getRoutingContext().getRoutingContexts()[0];
  371. Asp asp = this.getAsp(rc);
  372. if (asp == null) {
  373. logger.error(String.format("Rx : PayloadData for RoutingContext=%d. But no ASP found. Message=%s", rc,
  374. payload));
  375. return;
  376. }
  377. if (asp.getState() == AspState.ACTIVE) {
  378. asp.getAs().received(payload);
  379. } else {
  380. logger.error(String.format("Rx : PayloadData for RoutingContext=%d. But ASP State=%s. Message=%s", rc,
  381. asp.getState(), payload));
  382. }
  383. }
  384. private void handleNotify(Notify notify) {
  385. if (!this.started) {
  386. // If management stopped this ASP, ignore Notify
  387. return;
  388. }
  389. long[] rcs = notify.getRoutingContext().getRoutingContexts();
  390. for (int count = 0; count < rcs.length; count++) {
  391. Asp asp = this.getAsp(rcs[count]);
  392. try {
  393. asp.getAs().aspStateChange(asp, TransitionState.getTransition(notify));
  394. } catch (UnknownTransitionException e) {
  395. logger.error(e.getMessage(), e);
  396. }
  397. }
  398. }
  399. private void handleAspUpAck(ASPUpAck aspUpAck) {
  400. if (!this.started) {
  401. // If management stopped this ASP, ignore ASPUpAck
  402. return;
  403. }
  404. for (FastList.Node<Asp> n = aspList.head(), end = aspList.tail(); (n = n.getNext()) != end;) {
  405. Asp asp = n.getValue();
  406. boolean transToActive = this.activate(asp);
  407. if (!transToActive) {
  408. // Transition to INACTIVE
  409. try {
  410. asp.getFSM().signal(TransitionState.ASP_INACTIVE);
  411. } catch (UnknownTransitionException e) {
  412. logger.error(e.getMessage(), e);
  413. }
  414. } else {
  415. // Transition to ACTIVE_SENT
  416. try {
  417. asp.getFSM().signal(TransitionState.ASP_ACTIVE_SENT);
  418. } catch (UnknownTransitionException e) {
  419. logger.error(e.getMessage(), e);
  420. }
  421. }// if..else
  422. }// for
  423. }
  424. private void handleAspDownAck(ASPDownAck aspUpAck) {
  425. if (!this.started) {
  426. for (FastList.Node<Asp> n = aspList.head(), end = aspList.tail(); (n = n.getNext()) != end;) {
  427. Asp asp = n.getValue();
  428. try {
  429. asp.getFSM().signal(TransitionState.ASP_DOWN_ACK);
  430. } catch (UnknownTransitionException e) {
  431. logger.error(e.getMessage(), e);
  432. }
  433. }
  434. // Close Channel if management stopped this
  435. if (channel != null && this.channelConnected) {
  436. try {
  437. channel.close();
  438. if (logger.isDebugEnabled()) {
  439. logger.debug(String.format("Closed the channel for LocalAspFactory name=%s", this.getName()));
  440. }
  441. } catch (IOException e) {
  442. logger.error(String.format("IOException when trying to close channel for LocalAspFactory=%s",
  443. this.name));
  444. }
  445. }
  446. }
  447. }
  448. private void handleHeartbeat(Heartbeat hrtBeat) {
  449. HeartbeatAck hrtBeatAck = (HeartbeatAck) this.m3UAProvider.getMessageFactory().createMessage(
  450. MessageClass.ASP_STATE_MAINTENANCE, MessageType.HEARTBEAT_ACK);
  451. hrtBeatAck.setHeartbeatData(hrtBeat.getHeartbeatData());
  452. this.write(hrtBeatAck);
  453. }
  454. private void handleAspActiveAck(ASPActiveAck aspActiveAck) {
  455. if (!this.started) {
  456. // If management stopped this ASP, ignore ASPActiveAck
  457. return;
  458. }
  459. TrafficModeType trMode = aspActiveAck.getTrafficModeType();
  460. long[] rcs = aspActiveAck.getRoutingContext().getRoutingContexts();
  461. for (int count = 0; count < rcs.length; count++) {
  462. Asp asp = this.getAsp(rcs[count]);
  463. asp.getAs().setTrafficModeType(trMode);
  464. try {
  465. asp.getFSM().signal(TransitionState.ASP_ACTIVE_ACK);
  466. } catch (UnknownTransitionException e) {
  467. logger.error(e.getMessage(), e);
  468. }
  469. }
  470. }
  471. private void handleAspInactiveAck(ASPInactiveAck aspInactiveAck) {
  472. if (!this.started) {
  473. // If management stopped this ASP, ignore ASPInactiveAck
  474. return;
  475. }
  476. long[] rcs = aspInactiveAck.getRoutingContext().getRoutingContexts();
  477. for (int count = 0; count < rcs.length; count++) {
  478. Asp asp = this.getAsp(rcs[count]);
  479. try {
  480. asp.getFSM().signal(TransitionState.ASP_INACTIVE_ACK);
  481. } catch (UnknownTransitionException e) {
  482. logger.error(e.getMessage(), e);
  483. }
  484. }
  485. }
  486. private void handleCommUp() {
  487. this.channelConnected = true;
  488. ASPUp aspUp = (ASPUp) this.m3UAProvider.getMessageFactory().createMessage(MessageClass.ASP_STATE_MAINTENANCE,
  489. MessageType.ASP_UP);
  490. aspUp.setASPIdentifier(this.aspid);
  491. this.write(aspUp);
  492. for (FastList.Node<Asp> n = aspList.head(), end = aspList.tail(); (n = n.getNext()) != end;) {
  493. Asp asp = n.getValue();
  494. try {
  495. asp.getFSM().signal(TransitionState.COMM_UP);
  496. } catch (UnknownTransitionException e) {
  497. logger.error(e.getMessage(), e);
  498. }
  499. }
  500. }
  501. private void handleCommDown() {
  502. logger.warn(String.format("Communication channel down for LocalAspFactroy=%s", this.name));
  503. this.channelConnected = false;
  504. for (FastList.Node<Asp> n = aspList.head(), end = aspList.tail(); (n = n.getNext()) != end;) {
  505. Asp asp = n.getValue();
  506. try {
  507. asp.getFSM().signal(TransitionState.COMM_DOWN);
  508. asp.getAs().aspStateChange(asp, TransitionState.ASP_DOWN);
  509. } catch (UnknownTransitionException e) {
  510. logger.error(e.getMessage(), e);
  511. }
  512. }
  513. if (this.channel != null) {
  514. try {
  515. this.channel.close();
  516. } catch (IOException e) {
  517. logger.error(String.format("Error while closing channel for LocalAspFactory=%s", this.name));
  518. }
  519. }
  520. //If communication is down not because of management reason, try to bring it up again
  521. if(this.started){
  522. this.m3uaManagement.startAsp(this);
  523. }
  524. }
  525. protected boolean activate(Asp asp) {
  526. // If its loadshare, we want to send ASP_ACTIVE else if its
  527. // Override and there is already one ACTIVE, we leave this one
  528. // as INACTIVE
  529. As as = asp.getAs();
  530. // By default we assume Traffic Mode is Loadshare
  531. if (as.getTrafficModeType() == null || as.getTrafficModeType().getMode() == TrafficModeType.Loadshare) {
  532. // Activate this ASP
  533. this.sendAspActive(as);
  534. return true;
  535. } else if (as.getTrafficModeType().getMode() == TrafficModeType.Override) {
  536. for (FastList.Node<Asp> n = as.getAspList().head(), end = as.getAspList().tail(); (n = n.getNext()) != end;) {
  537. Asp asptemp = n.getValue();
  538. if (asptemp.getName().compareTo(asp.getName()) != 0
  539. && (asptemp.getState() == AspState.ACTIVE_SENT || asptemp.getState() == AspState.ACTIVE)) {
  540. return false;
  541. }
  542. }// for
  543. this.sendAspActive(as);
  544. return true;
  545. }
  546. return false;
  547. }
  548. // Private Methods
  549. protected void sendAspActive(As as) {
  550. ASPActive aspActive = (ASPActive) this.m3UAProvider.getMessageFactory().createMessage(
  551. MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE);
  552. aspActive.setRoutingContext(as.getRoutingContext());
  553. aspActive.setTrafficModeType(as.getTrafficModeType());
  554. this.write(aspActive);
  555. }
  556. private Asp getAsp(long rc) {
  557. for (FastList.Node<Asp> n = aspList.head(), end = aspList.tail(); (n = n.getNext()) != end;) {
  558. Asp asp = n.getValue();
  559. if (asp.getAs().getRoutingContext().getRoutingContexts()[0] == rc) {
  560. return asp;
  561. }
  562. }
  563. return null;
  564. }
  565. public void onCommStateChange(CommunicationState state) {
  566. switch (state) {
  567. case UP:
  568. this.handleCommUp();
  569. break;
  570. case SHUTDOWN:
  571. this.handleCommDown();
  572. break;
  573. case LOST:
  574. this.handleCommDown();
  575. break;
  576. }
  577. }
  578. private long generateId() {
  579. ASP_ID++;
  580. if (ASP_ID == 4294967295l) {
  581. ASP_ID = 1l;
  582. }
  583. return ASP_ID;
  584. }
  585. /**
  586. * XML Serialization/Deserialization
  587. */
  588. protected static final XMLFormat<LocalAspFactory> LOCAL_ASP_FACTORY_XML = new XMLFormat<LocalAspFactory>(
  589. LocalAspFactory.class) {
  590. @Override
  591. public void read(javolution.xml.XMLFormat.InputElement xml, LocalAspFactory localAspFactory)
  592. throws XMLStreamException {
  593. ASP_FACTORY_XML.read(xml, localAspFactory);
  594. localAspFactory.remIp = xml.getAttribute(REM_IP).toString();
  595. localAspFactory.remPort = xml.getAttribute(REM_PORT).toInt();
  596. // localAspFactory.channelConnected =
  597. // xml.getAttribute(CHANNEL_CONNECTED).toBoolean();
  598. }
  599. @Override
  600. public void write(LocalAspFactory localAspFactory, javolution.xml.XMLFormat.OutputElement xml)
  601. throws XMLStreamException {
  602. ASP_FACTORY_XML.write(localAspFactory, xml);
  603. xml.setAttribute(REM_IP, localAspFactory.remIp);
  604. xml.setAttribute(REM_PORT, localAspFactory.remPort);
  605. // xml.setAttribute(CHANNEL_CONNECTED,
  606. // localAspFactory.channelConnected);
  607. }
  608. };
  609. }