PageRenderTime 18ms CodeModel.GetById 11ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/razpubs/src/org/cybergarage/upnp/ssdp/SSDPNotifySocket.java

http://razpub.googlecode.com/
Java | 135 lines | 70 code | 26 blank | 39 comment | 12 complexity | 9e9eead131c8421174dfdd449d1ee2e1 MD5 | raw file
  1/******************************************************************
  2*
  3*   CyberUPnP for Java
  4*
  5*   Copyright (C) Satoshi Konno 2002-2003
  6*
  7*   File: SSDPNotifySocket.java
  8*
  9*   Revision;
 10*
 11*   11/20/02
 12*       - first revision.
 13*   05/13/03
 14*       - Added support for IPv6.
 15*   02/20/04
 16*       - Inma Marin Lopez <inma@dif.um.es>
 17*       - Added a multicast filter using the SSDP pakcet.
 18*   04/20/05
 19*       - Mikael Hakman <mhakman@dkab.net>
 20*       - Handle receive() returning null.
 21*       - Added close() in stop().
 22*   
 23******************************************************************/
 24
 25package org.cybergarage.upnp.ssdp;
 26
 27import java.net.*;
 28
 29import org.cybergarage.net.*;
 30import org.cybergarage.util.*;
 31import org.cybergarage.http.*;
 32import org.cybergarage.upnp.*;
 33
 34public class SSDPNotifySocket extends HTTPMUSocket implements Runnable
 35{
 36    private boolean useIPv6Address;
 37    
 38    ////////////////////////////////////////////////
 39    //  Constructor
 40    ////////////////////////////////////////////////
 41    
 42    public SSDPNotifySocket(String bindAddr)
 43    {
 44        String addr = SSDP.ADDRESS;
 45        useIPv6Address = false;
 46        if (HostInterface.isIPv6Address(bindAddr) == true) {
 47            addr = SSDP.getIPv6Address();
 48            useIPv6Address = true;
 49        }
 50        open(addr, SSDP.PORT, bindAddr);
 51        setControlPoint(null);
 52    }
 53
 54    ////////////////////////////////////////////////
 55    //  ControlPoint    
 56    ////////////////////////////////////////////////
 57
 58    private ControlPoint controlPoint = null;
 59    
 60    public void setControlPoint(ControlPoint ctrlp)
 61    {
 62        this.controlPoint = ctrlp;
 63    }
 64
 65    public ControlPoint getControlPoint()
 66    {
 67        return controlPoint;
 68    }
 69
 70    ////////////////////////////////////////////////
 71    //  post (SSDPNotifySocket)
 72    ////////////////////////////////////////////////
 73
 74    public boolean post(SSDPNotifyRequest req)
 75    {
 76        String ssdpAddr = SSDP.ADDRESS;
 77        if (useIPv6Address == true)
 78            ssdpAddr = SSDP.getIPv6Address();
 79        req.setHost(ssdpAddr, SSDP.PORT);
 80        return post((HTTPRequest)req);
 81    }
 82
 83    ////////////////////////////////////////////////
 84    //  run 
 85    ////////////////////////////////////////////////
 86
 87    private Thread deviceNotifyThread = null;
 88        
 89    public void run()
 90    {
 91        Thread thisThread = Thread.currentThread();
 92        
 93        ControlPoint ctrlPoint = getControlPoint();
 94        
 95        while (deviceNotifyThread == thisThread) {
 96            Thread.yield();
 97            SSDPPacket packet = receive();
 98            
 99            // Thanks for Mikael Hakman (04/20/05)
100            if (packet == null)
101                continue;
102            
103            // Thanks for Inma (02/20/04)
104            InetAddress maddr = getMulticastInetAddress();
105            InetAddress pmaddr = packet.getHostInetAddress();
106            if (maddr.equals(pmaddr) == false) {
107                Debug.message("Invalidate Multicast Recieved : " + maddr + "," + pmaddr);
108                continue;
109            }
110                                                
111            if (ctrlPoint != null)
112                ctrlPoint.notifyReceived(packet); 
113        }
114    }
115    
116    public void start()
117    {
118        deviceNotifyThread = new Thread(this);
119        
120        /* RazvanCojocaru 2008-03: name the thread for debugging and daemonize it since they don't die easily*/
121  		deviceNotifyThread.setName("CyberSSDPNotify"+deviceNotifyThread.getName());	
122  		deviceNotifyThread.setDaemon(true);
123  		
124        deviceNotifyThread.start();
125    }
126    
127    public void stop()
128    {
129        // Thanks for Mikael Hakman (04/20/05)
130        close();
131        
132        deviceNotifyThread = null;
133    }
134}
135