Added BinaryStructWriter class and initial impl off MDNS client

This commit is contained in:
Ziver Koc 2016-03-01 17:20:39 +01:00
parent 3f61db4640
commit 64db4922ee
3 changed files with 101 additions and 7 deletions

View file

@ -24,8 +24,52 @@
package zutil.net.dns;
import zutil.net.threaded.ThreadedUDPNetwork;
import zutil.net.threaded.ThreadedUDPNetworkThread;
import zutil.parser.binary.BinaryStructParser;
import zutil.parser.binary.BinaryStructWriter;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Created by ezivkoc on 2016-02-09.
* Created by Ziver
*/
public class MulticastDNSClient {
public class MulticastDNSClient extends ThreadedUDPNetwork implements ThreadedUDPNetworkThread{
private static final String MDNS_MULTICAST_ADDR = "224.0.0.251";
private static final int MDNS_MULTICAST_PORT = 5353;
public MulticastDNSClient() throws IOException {
super(null, MDNS_MULTICAST_ADDR, MDNS_MULTICAST_PORT);
setThread( this );
}
public void sendProbe() {
try {
BinaryStructWriter out = new BinaryStructWriter();
DNSPacket header = new DNSPacket();
out.write(header);
DatagramPacket packet = null;
packet = new DatagramPacket(
out.getByteArray(), out.getLength(),
InetAddress.getByName( MDNS_MULTICAST_ADDR ),
MDNS_MULTICAST_PORT );
send(packet);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void receivedPacket(DatagramPacket packet, ThreadedUDPNetwork network) {
DNSPacket header = new DNSPacket();
int length = BinaryStructParser.parse(header, packet.getData());
}
}

View file

@ -27,7 +27,7 @@ package zutil.net.ssdp;
import zutil.StringUtil;
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;
@ -88,7 +88,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
services = new HashMap<String, SSDPServiceInfo>();
setChacheTime( DEFAULT_CACHE_TIME );
setCacheTime( DEFAULT_CACHE_TIME );
enableNotify( true );
}
@ -118,7 +118,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
*
* @param time is the time in seconds
*/
public void setChacheTime(int time){
public void setCacheTime(int time){
cache_time = time;
if( isNotifyEnabled() ){
enableNotify(false);
@ -169,8 +169,8 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
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();
HttpHeaderParser headerParser = new HttpHeaderParser( msg );
HttpHeader header = headerParser.read();
// ******* Respond
// Check that the message is an ssdp discovery message

View file

@ -0,0 +1,50 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 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.
*/
package zutil.parser.binary;
/**
* Created by Ziver
*/
/*TODO:*/ public class BinaryStructWriter {
private byte[] data;
private int length;
public void write(BinaryStruct struct) {
}
public byte[] getByteArray() {
return data;
}
public int getLength() {
return length;
}
}