refactored UDPThreadedNetwork

This commit is contained in:
Ziver Koc 2016-05-31 10:37:29 +02:00
parent 52ab728404
commit 868364a0ca
4 changed files with 53 additions and 60 deletions

View file

@ -55,7 +55,7 @@ public class MulticastDNSClient extends ThreadedUDPNetwork implements ThreadedUD
public MulticastDNSClient() throws IOException { public MulticastDNSClient() throws IOException {
super(null, MDNS_MULTICAST_ADDR, MDNS_MULTICAST_PORT); super(MDNS_MULTICAST_ADDR, MDNS_MULTICAST_PORT);
setThread( this ); setThread( this );
} }

View file

@ -63,7 +63,6 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
* @throws IOException * @throws IOException
*/ */
public SSDPClient() throws IOException{ public SSDPClient() throws IOException{
super(null);
super.setThread(this); super.setThread(this);
services_st = new HashMap<>(); services_st = new HashMap<>();

View file

@ -83,7 +83,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
public SSDPServer() throws IOException{ public SSDPServer() throws IOException{
super( null, SSDP_MULTICAST_ADDR, SSDP_PORT ); super( SSDP_MULTICAST_ADDR, SSDP_PORT );
super.setThread( this ); super.setThread( this );
services = new HashMap<String, SSDPServiceInfo>(); services = new HashMap<String, SSDPServiceInfo>();

24
src/zutil/net/threaded/ThreadedUDPNetwork.java Normal file → Executable file
View file

@ -50,13 +50,11 @@ public class ThreadedUDPNetwork extends Thread{
/** /**
* Creates a new unicast Client instance of the class * Creates a new unicast Client instance of the class
* *
* @param thread is the class that will handle incoming packets
* @throws SocketException * @throws SocketException
*/ */
public ThreadedUDPNetwork(ThreadedUDPNetworkThread thread) throws SocketException{ public ThreadedUDPNetwork() throws SocketException{
this.type = UDPType.UNICAST; this.type = UDPType.UNICAST;
this.port = -1; this.port = -1;
setThread( thread );
socket = new DatagramSocket(); socket = new DatagramSocket();
} }
@ -64,14 +62,12 @@ public class ThreadedUDPNetwork extends Thread{
/** /**
* Creates a new unicast Server instance of the class * Creates a new unicast Server instance of the class
* *
* @param thread is the class that will handle incoming packets * @param port is the port that the server should listen to
* @param port is the port that the server should listen to
* @throws SocketException * @throws SocketException
*/ */
public ThreadedUDPNetwork(ThreadedUDPNetworkThread thread, int port) throws SocketException{ public ThreadedUDPNetwork(int port) throws SocketException{
this.type = UDPType.UNICAST; this.type = UDPType.UNICAST;
this.port = port; this.port = port;
setThread( thread );
socket = new DatagramSocket( port ); socket = new DatagramSocket( port );
} }
@ -79,19 +75,17 @@ public class ThreadedUDPNetwork extends Thread{
/** /**
* Creates a new multicast Server instance of the class * Creates a new multicast Server instance of the class
* *
* @param thread is the class that will handle incoming packets * @param port is the port that the server should listen to
* @param port is the port that the server should listen to * @param multicastAddr is the multicast address that the server will listen on
* @param multicast_addr is the multicast address that the server will listen on
* @throws IOException * @throws IOException
*/ */
public ThreadedUDPNetwork(ThreadedUDPNetworkThread thread, String multicast_addr, int port ) throws IOException{ public ThreadedUDPNetwork(String multicastAddr, int port ) throws IOException{
this.type = UDPType.MULTICAST; this.type = UDPType.MULTICAST;
this.port = port; this.port = port;
setThread( thread );
// init udp socket // init udp socket
MulticastSocket msocket = new MulticastSocket( port ); MulticastSocket msocket = new MulticastSocket( port );
InetAddress group = InetAddress.getByName( multicast_addr ); InetAddress group = InetAddress.getByName( multicastAddr );
msocket.joinGroup( group ); msocket.joinGroup( group );
socket = msocket; socket = msocket;
@ -115,7 +109,7 @@ public class ThreadedUDPNetwork extends Thread{
/** /**
* Sends the given packet * Sends the given packet
* *
* @param packet is the packet to send * @param packet is the packet to send
* @throws IOException * @throws IOException
*/ */
public synchronized void send( DatagramPacket packet ) throws IOException{ public synchronized void send( DatagramPacket packet ) throws IOException{
@ -125,7 +119,7 @@ public class ThreadedUDPNetwork extends Thread{
/** /**
* Sets the thread that will handle the incoming packets * Sets the thread that will handle the incoming packets
* *
* @param thread is the thread * @param thread is the thread
*/ */
public void setThread(ThreadedUDPNetworkThread thread){ public void setThread(ThreadedUDPNetworkThread thread){
this.thread = thread; this.thread = thread;