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 {
super(null, MDNS_MULTICAST_ADDR, MDNS_MULTICAST_PORT);
super(MDNS_MULTICAST_ADDR, MDNS_MULTICAST_PORT);
setThread( this );
}

View file

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

View file

@ -83,7 +83,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
public SSDPServer() throws IOException{
super( null, SSDP_MULTICAST_ADDR, SSDP_PORT );
super( SSDP_MULTICAST_ADDR, SSDP_PORT );
super.setThread( this );
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
*
* @param thread is the class that will handle incoming packets
*
* @throws SocketException
*/
public ThreadedUDPNetwork(ThreadedUDPNetworkThread thread) throws SocketException{
public ThreadedUDPNetwork() throws SocketException{
this.type = UDPType.UNICAST;
this.port = -1;
setThread( thread );
socket = new DatagramSocket();
}
/**
* 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
*/
public ThreadedUDPNetwork(ThreadedUDPNetworkThread thread, int port) throws SocketException{
public ThreadedUDPNetwork(int port) throws SocketException{
this.type = UDPType.UNICAST;
this.port = port;
setThread( thread );
socket = new DatagramSocket( port );
}
/**
* 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 multicast_addr is the multicast address that the server will listen on
*
* @param port is the port that the server should listen to
* @param multicastAddr is the multicast address that the server will listen on
* @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.port = port;
setThread( thread );
// init udp socket
MulticastSocket msocket = new MulticastSocket( port );
InetAddress group = InetAddress.getByName( multicast_addr );
InetAddress group = InetAddress.getByName( multicastAddr );
msocket.joinGroup( group );
socket = msocket;
@ -115,7 +109,7 @@ public class ThreadedUDPNetwork extends Thread{
/**
* Sends the given packet
*
* @param packet is the packet to send
* @param packet is the packet to send
* @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
*
* @param thread is the thread
* @param thread is the thread
*/
public void setThread(ThreadedUDPNetworkThread thread){
this.thread = thread;