org.cybergarage.upnp.ssdp.SSDPSearchResponseSocket Maven / Gradle / Ivy
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SSDPSearchResponseSocket.java
*
* Revision;
*
* 11/20/02
* - first revision.
* 05/28/03
* - Added post() to send a SSDPSearchRequest.
* 16/01/08
* added debug message and correct thread name (francesco)
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import java.net.DatagramSocket;
import org.cybergarage.upnp.*;
import org.cybergarage.util.Debug;
public class SSDPSearchResponseSocket extends HTTPUSocket implements Runnable
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SSDPSearchResponseSocket()
{
setControlPoint(null);
}
public SSDPSearchResponseSocket(String bindAddr, int port)
{
super(bindAddr, port);
setControlPoint(null);
}
////////////////////////////////////////////////
// ControlPoint
////////////////////////////////////////////////
private ControlPoint controlPoint = null;
public void setControlPoint(ControlPoint ctrlp)
{
this.controlPoint = ctrlp;
}
public ControlPoint getControlPoint()
{
return controlPoint;
}
////////////////////////////////////////////////
// run
////////////////////////////////////////////////
private Thread deviceSearchResponseThread = null;
public void run()
{
Thread thisThread = Thread.currentThread();
ControlPoint ctrlPoint = getControlPoint();
while (deviceSearchResponseThread == thisThread) {
Thread.yield();
SSDPPacket packet = receive();
if (packet == null)
break;
Debug.message("Received M-Search Response:\n"+packet.toString());
if (ctrlPoint != null)
ctrlPoint.searchResponseReceived(packet);
}
}
public void start() {
StringBuffer name = new StringBuffer("Cyber.SSDPSearchResponseSocket/");
DatagramSocket s = getDatagramSocket();
name.append(s.getLocalSocketAddress()).append(':');
name.append(s.getLocalPort());
deviceSearchResponseThread = new Thread(this,name.toString());
deviceSearchResponseThread.start();
}
public void stop()
{
deviceSearchResponseThread = null;
}
////////////////////////////////////////////////
// post
////////////////////////////////////////////////
public boolean post(String addr, int port, SSDPSearchResponse res)
{
return post(addr, port, res.getHeader());
}
////////////////////////////////////////////////
// post
////////////////////////////////////////////////
public boolean post(String addr, int port, SSDPSearchRequest req)
{
return post(addr, port, req.toString());
}
}