PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

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