org.cybergarage.upnp.ssdp.SSDPNotifySocket Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of upnp-stack-jdk13 Show documentation
Show all versions of upnp-stack-jdk13 Show documentation
A pure Java Open Source implementation of the UPnP stack backported for JDK13
The newest version!
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: SSDPNotifySocket.java
*
* Revision;
*
* 11/20/02
* - first revision.
* 05/13/03
* - Added support for IPv6.
* 02/20/04
* - Inma Marin Lopez
* - Added a multicast filter using the SSDP pakcet.
* 04/20/05
* - Mikael Hakman
* - Handle receive() returning null.
* - Added close() in stop().
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import java.net.*;
import org.cybergarage.net.*;
import org.cybergarage.util.*;
import org.cybergarage.http.*;
import org.cybergarage.upnp.*;
/**
*
* This class identifies a SSDP socket only for notifing packet.
*
* @author Satoshi "skonno" Konno
* @author Stefano "Kismet" Lenzi
* @version 1.8
*
*/
public class SSDPNotifySocket extends HTTPMUSocket implements Runnable
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SSDPNotifySocket(String bindAddr)
{
String addr = SSDP.ADDRESS;
open(addr, SSDP.PORT, bindAddr);
setControlPoint(null);
}
////////////////////////////////////////////////
// ControlPoint
////////////////////////////////////////////////
private ControlPoint controlPoint = null;
public void setControlPoint(ControlPoint ctrlp)
{
this.controlPoint = ctrlp;
}
public ControlPoint getControlPoint()
{
return controlPoint;
}
/**
* This method send a {@link SSDPNotifyRequest} over {@link SSDPNotifySocket}
*
* @param req the {@link SSDPNotifyRequest} to send
* @return true if and only if the trasmission succeced
* Because it rely on UDP doesn't mean that it's also recieved
*/
public boolean post(SSDPNotifyRequest req)
{
String ssdpAddr = SSDP.ADDRESS;
req.setHost(ssdpAddr, SSDP.PORT);
return post((HTTPRequest)req);
}
////////////////////////////////////////////////
// run
////////////////////////////////////////////////
private Thread deviceNotifyThread = null;
public void run()
{
Thread thisThread = Thread.currentThread();
ControlPoint ctrlPoint = getControlPoint();
while (deviceNotifyThread == thisThread) {
Thread.yield();
SSDPPacket packet = receive();
// Thanks for Mikael Hakman (04/20/05)
if (packet == null)
continue;
// Thanks for Inma (02/20/04)
InetAddress maddr = getMulticastInetAddress();
InetAddress pmaddr = packet.getHostInetAddress();
if (maddr.equals(pmaddr) == false) {
Debug.warning("Invalidate Multicast Recieved from IP " + maddr + " on " + pmaddr);
continue;
}
//TODO Must be performed on a different Thread in order to prevent UDP packet losses.
if (ctrlPoint != null)
ctrlPoint.notifyReceived(packet);
}
}
public void start(){
StringBuffer name = new StringBuffer("Cyber.SSDPNotifySocket/");
name.append(this.getLocalAddress()).append(':');
name.append(this.getLocalPort()).append(" -> ");
name.append(this.getMulticastAddress()).append(':');
name.append(this.getMulticastPort());
deviceNotifyThread = new Thread(this,name.toString());
deviceNotifyThread.start();
}
public void stop()
{
// Thanks for Mikael Hakman (04/20/05)
close();
deviceNotifyThread = null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy