/protocols/ss7/hardware/dialogic/java/src/main/java/org/mobicents/ss7/hardware/dialogic/InterProcessCommunicator.java

http://mobicents.googlecode.com/ · Java · 132 lines · 40 code · 19 blank · 73 comment · 4 complexity · d2eb306b6422e877818a9ad2411467bc 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.ss7.hardware.dialogic;
  23. import java.io.IOException;
  24. /**
  25. *
  26. * @author amit bhayani
  27. * @author Oleg Kulikov
  28. * @author sergey vetyutnev
  29. *
  30. */
  31. public class InterProcessCommunicator {
  32. private final static String LIB_NAME = "mobicents-dialogic-linux";
  33. /** The identifier of the originated module */
  34. private int source;
  35. /** The identifier of the destination module */
  36. private int destination;
  37. private byte[] readBuffer = new byte[1000];
  38. /**
  39. * Creates a new instance of InterProcessCommunicator
  40. *
  41. * @param source
  42. * the integer identifier of the originated module
  43. * @param destination
  44. * the integer idenifier of the destination module.
  45. */
  46. public InterProcessCommunicator(int source, int destination) {
  47. this.source = source;
  48. this.destination = destination;
  49. }
  50. /**
  51. * Receives a datagram from GCT Interprocess.
  52. *
  53. * @return received datagram.
  54. */
  55. public byte[] read() {
  56. //TODO Make JNI use native ByteBuffer
  57. int len = receive(source, readBuffer);
  58. if (len == -1) {
  59. // no messages in queue
  60. return null;
  61. }
  62. byte[] tempBuf = new byte[len];
  63. System.arraycopy(readBuffer, 0, tempBuf, 0, len);
  64. return tempBuf;
  65. }
  66. /**
  67. * Sends datagram to the destination module.
  68. *
  69. * @param packet
  70. * the datagram to be sent.
  71. */
  72. public int write(byte[] msg) throws IOException {
  73. //TODO Make JNI use native ByteBuffer
  74. //TODO I do not sure if "synchronized" needs here, may be dialogic GCT_send() method is thread-safe ?
  75. synchronized (this) {
  76. int status = send(source, destination, msg);
  77. if (status != 0) {
  78. throw new IOException("Dialogic card: Can not send packet by GCT_send() method");
  79. }
  80. return status;
  81. }
  82. }
  83. /**
  84. * Actualy receives datagram using Interprocces communication.
  85. *
  86. * @param source
  87. * indicates the module id wich will receive datagrams.
  88. * @param buffer
  89. * the buffer wich should be used to store recevied message.
  90. * @return the actual length of the received message.
  91. */
  92. private native int receive(int source, byte[] buffer);
  93. /**
  94. * Actualy sends the message using interprocces communication.
  95. *
  96. * @param source
  97. * the module id wich sends message.
  98. * @param destionation
  99. * the module id wich must receive message.
  100. * @param buffer
  101. * the buffer contained message.
  102. * @return the number of actualy bytes sent.
  103. */
  104. private native int send(int source, int destination, byte[] buffer);
  105. static {
  106. try {
  107. System.loadLibrary(LIB_NAME);
  108. System.out.println("Loaded library mobicents-dialogic-linux");
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. }
  112. }
  113. }