2011-07-13 17:53:17 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
|
* Copyright (c) 2011 Ziver Koc
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
******************************************************************************/
|
2011-02-15 19:37:35 +00:00
|
|
|
package zutil.net.threaded;
|
2010-01-31 18:10:00 +00:00
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.DatagramPacket;
|
|
|
|
|
import java.net.DatagramSocket;
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
|
import java.net.MulticastSocket;
|
|
|
|
|
import java.net.SocketException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-24 23:20:59 +00:00
|
|
|
* * A simple network server that handles UDP communication
|
2010-01-31 18:10:00 +00:00
|
|
|
*
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*/
|
|
|
|
|
public class ThreadedUDPNetwork extends Thread{
|
|
|
|
|
public static final int BUFFER_SIZE = 512;
|
|
|
|
|
|
|
|
|
|
// Type of UDP socket
|
|
|
|
|
enum UDPType{
|
|
|
|
|
MULTICAST,
|
|
|
|
|
UNICAST
|
|
|
|
|
}
|
|
|
|
|
protected final UDPType type;
|
|
|
|
|
protected final int port;
|
|
|
|
|
protected DatagramSocket socket;
|
|
|
|
|
protected ThreadedUDPNetworkThread thread = null;
|
|
|
|
|
|
2010-02-04 19:26:32 +00:00
|
|
|
/**
|
|
|
|
|
* Creates a new unicast Clien instance of the class
|
|
|
|
|
*
|
|
|
|
|
* @param thread is the class that will handle incoming packets
|
|
|
|
|
* @throws SocketException
|
|
|
|
|
*/
|
|
|
|
|
public ThreadedUDPNetwork(ThreadedUDPNetworkThread thread) throws SocketException{
|
|
|
|
|
this.type = UDPType.UNICAST;
|
|
|
|
|
this.port = -1;
|
|
|
|
|
setThread( thread );
|
|
|
|
|
|
|
|
|
|
socket = new DatagramSocket();
|
|
|
|
|
}
|
2010-01-31 18:10:00 +00:00
|
|
|
|
|
|
|
|
/**
|
2010-02-04 19:26:32 +00:00
|
|
|
* Creates a new unicast Server instance of the class
|
2010-01-31 18:10:00 +00:00
|
|
|
*
|
|
|
|
|
* @param thread is the class that will handle incoming packets
|
|
|
|
|
* @param port is the port that the server should listen to
|
|
|
|
|
* @throws SocketException
|
|
|
|
|
*/
|
|
|
|
|
public ThreadedUDPNetwork(ThreadedUDPNetworkThread thread, int port) throws SocketException{
|
|
|
|
|
this.type = UDPType.UNICAST;
|
|
|
|
|
this.port = port;
|
|
|
|
|
setThread( thread );
|
|
|
|
|
|
|
|
|
|
socket = new DatagramSocket( port );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-02-04 19:26:32 +00:00
|
|
|
* Creates a new multicast Server instance of the class
|
2010-01-31 18:10:00 +00:00
|
|
|
*
|
|
|
|
|
* @param thread is the class that will handle incoming packets
|
|
|
|
|
* @param port is the port that the server should listen to
|
|
|
|
|
* @param multicast_addr is the multicast address that the server will listen on
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
2010-02-04 19:26:32 +00:00
|
|
|
public ThreadedUDPNetwork(ThreadedUDPNetworkThread thread, String multicast_addr, int port ) throws IOException{
|
2010-01-31 18:10:00 +00:00
|
|
|
this.type = UDPType.MULTICAST;
|
|
|
|
|
this.port = port;
|
|
|
|
|
setThread( thread );
|
|
|
|
|
|
|
|
|
|
// init udp socket
|
|
|
|
|
MulticastSocket msocket = new MulticastSocket( port );
|
|
|
|
|
InetAddress group = InetAddress.getByName( multicast_addr );
|
|
|
|
|
msocket.joinGroup( group );
|
|
|
|
|
|
|
|
|
|
socket = msocket;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void run(){
|
|
|
|
|
try{
|
|
|
|
|
while(true){
|
|
|
|
|
byte[] buf = new byte[BUFFER_SIZE];
|
|
|
|
|
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
|
|
|
|
socket.receive( packet );
|
|
|
|
|
if( thread!=null )
|
|
|
|
|
thread.receivedPacket( packet, this );
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends the given packet
|
|
|
|
|
*
|
|
|
|
|
* @param packet is the packet to send
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void send( DatagramPacket packet ) throws IOException{
|
|
|
|
|
socket.send(packet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the thread that will handle the incoming packets
|
|
|
|
|
*
|
|
|
|
|
* @param thread is the thread
|
|
|
|
|
*/
|
|
|
|
|
public void setThread(ThreadedUDPNetworkThread thread){
|
|
|
|
|
this.thread = thread;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-05 21:31:22 +00:00
|
|
|
/**
|
|
|
|
|
* Stops the server and interrupts its internal thread.
|
|
|
|
|
* This is a permanent action that will not be able to recover from
|
|
|
|
|
*/
|
|
|
|
|
public void close(){
|
|
|
|
|
this.interrupt();
|
|
|
|
|
socket.close();
|
|
|
|
|
}
|
2011-07-13 17:53:17 +00:00
|
|
|
}
|