⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udpmessagesocket.java

📁 Java SIP
💻 JAVA
字号:
/* * This file was derived from libdissipate, an open source SIP library. The original file  * was modified on 1/23/2001.  Please see * http://www.div8.net/dissipate for more information. * * Copyright (c) 2000 Billy Biggs <bbiggs@div8.net> * * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at your * option) any later version. *  * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public * License for more details. *  * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB.  If not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * *//** * class UDPMessageSocket *  * This is the UDP implementation of the MessageSocket class.  It is used * for performing operations on a new or existing UDP socket. * * This code has been generated using C2J++ * C2J++ is based on Chris Laffra's C2J (laffra@watson.ibm.com) * Read general disclaimer distributed with C2J++ before using this code * For information about C2J++, send mail to Ilya_Tilevich@ibi.com */package org.mitre.jsip;import java.net.SocketException;import java.net.DatagramSocket;import java.net.DatagramPacket;import java.net.InetAddress;import java.net.UnknownHostException;import java.io.IOException;public class UDPMessageSocket extends MessageSocket{    /**     * UDPMessageSocket     */    public UDPMessageSocket()    {	type = UDP;	didcomplain = false;	// set up the receive socket??????????    }    /**     * connect     * @param portnum     * @return int     */    public int connect(String hostname, int portnum)    {	fSendPort = portnum;		sendSocket = null;	try {	    InetAddress raddr = InetAddress.getByName( hostname );	    sendSocket = new DatagramSocket();	    sendSocket.connect( raddr, portnum );	} catch (UnknownHostException uhe) {	    System.err.println("Unable to connect to host: " + hostname);	    return -1;	} catch (SocketException se) {	    System.err.println("Unable to set up socket to remote host: " + se);	    didcomplain =  true;	    return -1;	}	return 0;    }    /**     * connect     * @param portnum     * @return int     */    public int connect(InetAddress host, int portnum)    {	fSendPort = portnum;		sendSocket = null;	try {	    sendSocket = new DatagramSocket();	    sendSocket.connect( host, portnum );	} catch (SocketException se) {	    System.err.println("Unable to set up socket to remote host: " + se);	    didcomplain =  true;	    return -1;	}	return 0;    }    /**     * send     * @param sendbuffer     * @param length     * @return int     */    public int send(String sendbuffer, int length)    {	int numbytes;	int retries = 0;	DatagramPacket dp = new DatagramPacket(sendbuffer.getBytes(), sendbuffer.length());  	fSendThread = new DatagramSendThread( dp, sendSocket, fRetryTimer );  	fSendThread.start();	return 0;    }    /**     * receive     * @param recvbuffer     * @param maxlength     * @return int     */    public int receive(byte[] recvbuffer, int maxlength)    {	int numbytes = 0;	DatagramPacket dp = null;	try {	    dp = new DatagramPacket(recvbuffer, maxlength);	    fSocket.receive(dp);	} catch (IOException ioe) {	    System.err.println( "UDPMessageSocket::receive(): recvfrom() failed: " + ioe);	    return -1;	}	// not sure about this	// *** LOOK AT *** don't need to send all this stuff to the thread	DatagramReplyThread drt = new DatagramReplyThread( dp, fSocket, fRetryTimer );	drt.start();		return numbytes;    }    /**     * listen     * @param portnum     * @return int     */    public int listen(MessageQueue queue)    {	// setup port on fSocket	bind();	byte[] recvbuffer = new byte[DEFAULT_MAX_DATAGRAM_SIZE];	int maxlength = DEFAULT_MAX_DATAGRAM_SIZE;	DatagramPacket dp = new DatagramPacket( recvbuffer, maxlength );	DatagramReplyThread drt = new DatagramReplyThread( dp, fSocket, queue, fRetryTimer );	drt.start();	return 0;    }    /**     * accept     * @return int     */    public void accept()    {//	return socketfd;    }    /**     * listenOnEvenPort     * @return int     */    public int listenOnEvenPort()    {	return 0;    }        protected void bind()     {	// bind to the specific local port 	try {	    if ( localPort != 0 ) {		fSocket = new DatagramSocket(localPort);	    } else {		fSocket = new DatagramSocket();	    }	} catch (SocketException se) {	    System.err.println( "Unable to bind to port " + localPort + " because " + se );	    System.err.println( "Going to die until you fix this" );	    System.exit( -1 );	}    }    public void close() {	if ( fSocket != null ) fSocket.close();	if ( sendSocket != null ) sendSocket.close();	fSocket = null;	sendSocket = null;    }    // class variables    protected boolean didcomplain;    protected DatagramSocket fSocket, sendSocket;    protected DatagramPacket fSendDatagram, fReceiveDatagram;    protected int fSendPort;                      // fReceivePort = localPort;    protected DatagramReplyThread fReplyThread;    protected DatagramSendThread fSendThread;    protected int fRetryTimer = 10000;    protected int fRetryCount = 0;    public final static int DEFAULT_MAX_DATAGRAM_SIZE = 1024;    public final static int DEFAULT_MAX_RETRIES = 5;}// ***********************************************************************************************/** * A class for dealing with the problems of waiting * for a datagram response. */class DatagramReplyThread extends Thread{    private final static boolean fDebugOn = true;        DatagramPacket fRecvPacket;    DatagramSocket fRecvSocket;    MessageQueue fRecvQueue;    int fWaitTime = 50000;    public  int fReceivedState = 0;//neither received nor failed    public final static int DEFAULT_MAX_DATAGRAM_SIZE = 1024;        /**     * @param recvPacket The packet in which to stuff our results.     * @param recvSocket The socket on which to receive data.     * @param totalWaitTime The amount of time to wait for a response.     */    public DatagramReplyThread(DatagramPacket recvPacket,			       DatagramSocket recvSocket,			       int totalWaitTime)    {	fRecvPacket = recvPacket;	fRecvSocket = recvSocket;	fWaitTime = totalWaitTime;    }//DatagramReplyThread        public DatagramReplyThread(DatagramPacket recvPacket,			       DatagramSocket recvSocket,			       MessageQueue  queue,			       int totalWaitTime) {	fRecvPacket = recvPacket;	fRecvSocket = recvSocket;	fRecvQueue = queue;	fWaitTime = totalWaitTime;    }    /**     * perform all the useful stuff here     */    public synchronized void run()    {	if (fDebugOn)	    System.out.println("DatagramReplySlave running...");	for (;;) {	    DatagramPacket dp = new DatagramPacket( new byte[DEFAULT_MAX_DATAGRAM_SIZE],						    DEFAULT_MAX_DATAGRAM_SIZE );	    try {		if (fDebugOn)		    System.out.println("DatagramReplySlave receiving...");		fRecvSocket.receive( dp );		fReceivedState = 1;//received OK!		if (fDebugOn)		    System.out.println("DatagramReplySlave received OK!");	    }	    catch (IOException receiveEx) {		System.err.println("receive failed! " + receiveEx);		fReceivedState = -1;	    }; //receive failed!		    // see if we can create a new message and put it in the queue	    try {		SipMessage message = new SipMessage( new String( dp.getData() ) );		fRecvQueue.AddQueueItem(message);	    } catch (BadHeaderException bhe) {		System.err.println("Error parsing incoming message. Rejecting because " +				   bhe.toString());	    }	    //	    fRecvPacket.setData( new byte[1024] );	}    } //run    } /* class DatagramReplySlave */// **************************************************************************************/** * a class for dealing with the problems of sending a datagrams */class DatagramSendThread extends Thread{        protected final static boolean fDebugOn = true;        DatagramPacket fSendPacket;    DatagramSocket fSendSocket;    public  int fSendState = 0;//neither received nor failed    protected int fRetryInterval; //in msec        /**     * @param sendPacket     */    public DatagramSendThread(DatagramPacket sendPacket,			      DatagramSocket sendSocket,			      int retryTime)    {	fSendPacket = sendPacket;	fSendSocket = sendSocket;	fRetryInterval = retryTime;	    }//DatagramSendThread            /**     * proceed with all of the time-consuming stuff here     */    public void run()    {	if (fDebugOn)	    System.out.println("DatagramSendThread running...");		try {	    if (fDebugOn)		System.out.println("DatagramSendThread sending " + 				   fSendPacket.getLength() + " bytes...");	    fSendSocket.send(fSendPacket);	    fSendState = 0;//sent OK!	    if (fDebugOn)     	        System.out.println("DatagramSendThread sent ok!");	}	catch (IOException sendEx) {	    if (fDebugOn)		System.err.println("send failed! " + sendEx);	    fSendState = -1;	}  	finally {  	    if ( fSendSocket != null ) fSendSocket.close();  	}    } // run        }/* class DatagramSendThread */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -