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

@ -26,7 +26,7 @@ package zutil.net.ssdp;
import zutil.io.StringOutputStream; import zutil.io.StringOutputStream;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import zutil.net.http.HttpHeader; import zutil.net.http.HttpHeader;
import zutil.net.http.HttpHeaderParser; import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpPrintStream; import zutil.net.http.HttpPrintStream;
import zutil.net.threaded.ThreadedUDPNetwork; import zutil.net.threaded.ThreadedUDPNetwork;
@ -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<>();
@ -190,45 +189,45 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
* Location: http://localhost:80 * Location: http://localhost:80
*/ */
public void receivedPacket(DatagramPacket packet, ThreadedUDPNetwork network) { public void receivedPacket(DatagramPacket packet, ThreadedUDPNetwork network) {
try { try {
String msg = new String(packet.getData(), packet.getOffset(), packet.getLength()); String msg = new String(packet.getData(), packet.getOffset(), packet.getLength());
HttpHeaderParser headerParser = new HttpHeaderParser(msg); HttpHeaderParser headerParser = new HttpHeaderParser(msg);
HttpHeader header = headerParser.read(); HttpHeader header = headerParser.read();
logger.log(Level.FINEST, "Received(from: " + packet.getAddress() + "): " + header); logger.log(Level.FINEST, "Received(from: " + packet.getAddress() + "): " + header);
String usn = header.getHeader("USN"); String usn = header.getHeader("USN");
String st = header.getHeader("ST"); String st = header.getHeader("ST");
boolean newService = false; boolean newService = false;
StandardSSDPInfo service; StandardSSDPInfo service;
// Get existing service // Get existing service
if (services_usn.containsKey(usn)) { if (services_usn.containsKey(usn)) {
service = services_usn.get(usn); service = services_usn.get(usn);
} }
// Add new service // Add new service
else { else {
newService = true; newService = true;
service = new StandardSSDPInfo(); service = new StandardSSDPInfo();
services_usn.put(usn, service); services_usn.put(usn, service);
if (!services_st.containsKey(st)) if (!services_st.containsKey(st))
services_st.put(st, new LinkedList<StandardSSDPInfo>()); services_st.put(st, new LinkedList<StandardSSDPInfo>());
services_st.get(header.getHeader("ST")).add(service); services_st.get(header.getHeader("ST")).add(service);
} }
service.setLocation(header.getHeader("LOCATION")); service.setLocation(header.getHeader("LOCATION"));
service.setST(st); service.setST(st);
service.setUSN(usn); service.setUSN(usn);
service.setInetAddress(packet.getAddress()); service.setInetAddress(packet.getAddress());
if (header.getHeader("Cache-Control") != null) { if (header.getHeader("Cache-Control") != null) {
service.setExpirationTime( service.setExpirationTime(
System.currentTimeMillis() + 1000 * getCacheTime(header.getHeader("Cache-Control"))); System.currentTimeMillis() + 1000 * getCacheTime(header.getHeader("Cache-Control")));
} }
service.readHeaders(header); service.readHeaders(header);
if (listener != null && newService) if (listener != null && newService)
listener.newService(service); listener.newService(service);
} catch (IOException e){ } catch (IOException e){
logger.log(Level.SEVERE, null, e); logger.log(Level.SEVERE, null, e);
} }
} }
private long getCacheTime(String cache_control){ private long getCacheTime(String cache_control){

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>();

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

@ -49,49 +49,43 @@ 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();
} }
/** /**
* 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 );
} }
/** /**
* 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;