PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/razpubs/src/razie/upnp/UpnpInventory.scala

http://razpub.googlecode.com/
Scala | 220 lines | 156 code | 39 blank | 25 comment | 12 complexity | a18829e5dcf70cd8529432e405f03a9d MD5 | raw file
  1package razie.upnp
  2
  3import razie.assets._
  4import razie.base._
  5import com.razie.pub.base._
  6import com.razie.pub.lightsoa._
  7import razie.draw._
  8import scala.collection.JavaConversions._
  9
 10/** some statics */
 11object Upnp {
 12   org.cybergarage.util.Debug.on()
 13   final val Device = "Upnp.Device"
 14   final val MutantDevice = "Upnp.MutantDevice"
 15   final val Service = "Upnp.Service"
 16   final val DeviceHasServices = "DeviceHasServices"
 17      
 18   def uc = UpnpControlPoint.instance
 19   
 20   def deviceMeta = new MetaSpec (
 21     new Meta (razie.AI cmdicon(Upnp.Device, "device"), null, 
 22         classOf[UpnpInventory].getName))
 23   def serviceMeta = new MetaSpec (
 24     new Meta (razie.AI cmdicon(Upnp.Device, "device"), null, 
 25         classOf[UpnpInventory].getName))
 26   
 27   def makeDevice (d:UpnpDevice) = d.deviceType match {
 28      case "urn:schemas-upnp-org:device:MediaServer:1" => new AUpnpMediaServer (d)
 29      case "urn:schemas-upnp-org:device:mutant:1" => new AUpnpMutant (d)
 30      case _ => new AUpnpDevice (d)
 31   }
 32}
 33
 34/** stands in for a upnpdevice */
 35@SoaAsset(meta=Upnp.Device, descr="a upnp device")
 36@AssetMeta(name=Upnp.Device, inventory=classOf[UpnpInventory],   descr="a upnp device")
 37class AUpnpDevice (val actual:UpnpDevice) extends AssetBaseImpl (NoAssetBrief) with DrawAsset {
 38   this.brief = new AssetBriefImpl (new AssetKey (Upnp.Device, actual.friendlyName), 
 39         name=actual.friendlyName, icon="device", briefDesc=actual.deviceType)
 40   
 41   val services = for (s <- actual.services) yield new AUpnpService (this, s)
 42   
 43   override def render(t:Technology , stream:DrawStream ) : AnyRef = razie.Draw.seq(
 44      super.render(t,stream),
 45      razie.Draw.text("-------------services--------------"),
 46      AssetMgr.pres().toDrawable(razie.RSJ.list(services.map(_.brief)), null, NoAffordance),
 47      razie.Draw.text("-------------my xml--------------"),
 48      razie.Draw.xml (actual.toUpnpXml.toString),
 49      razie.Draw.text("-------------original xml--------------"),
 50      razie.Draw.xml (actual.cyberDevice.getRootNode().toString),
 51      razie.Draw.text("-------------done--------------")
 52      )
 53}
 54
 55class AUpnpMutant (actual:UpnpDevice) extends AUpnpDevice (actual) {
 56   override def render(t:Technology , stream:DrawStream ) : AnyRef = razie.Draw.seq(
 57      super.render(t,stream)
 58      )
 59}
 60
 61class AUpnpMediaServer (actual:UpnpDevice) extends AUpnpDevice (actual) {
 62   
 63   override def render(t:Technology , stream:DrawStream ) : AnyRef = razie.Draw.seq(
 64      super.render(t,stream),
 65//      razie.Draw.text("-------------services--------------"),
 66//      AssetMgr.pres().toDrawable(razie.RSJ.list(services.map(_.brief)), null, NoAffordance),
 67//      razie.Draw.text("-------------browsing--------------"),
 68//      
 69//      
 70//      
 71//      
 72//      razie.Draw.text("-------------my xml--------------"),
 73//      razie.Draw.xml (actual.toUpnpXml.toString),
 74//      razie.Draw.text("-------------original xml--------------"),
 75//      razie.Draw.xml (actual.cyberDevice.getRootNode().toString),
 76
 77      razie.Draw.text("-------------Browsing--------------"),
 78      
 79      browse ("0"),
 80      
 81      razie.Draw.text("-------------done--------------")
 82      )
 83
 84   def browse (id:String) = {
 85      val seq = razie.Draw.seq()
 86      seq write razie.Draw.text("-------------meta--------------")
 87      val rma = Browse (id, "BrowseMetadata", "0", "100")
 88      seq write rma
 89      
 90      seq write razie.Draw.text("-------------data--------------")
 91      val rda = Browse (id, "BrowseDirectChildren", "0", rma.outArgs.sa("TotalMatches"))
 92      seq write rda
 93      seq
 94   }
 95   
 96   def Browse(ObjectId: String , BrowseFlag:String, StartingIndex:String, RequestedCount:String) = {
 97      val directory = actual.services.filter (_.serviceType == "urn:schemas-upnp-org:service:ContentDirectory:1").head
 98      val action = directory.actions.filter (_.name == "Browse").head
 99      action.inArgs.setAttr("ObjectID", ObjectId, "BrowseFlag", BrowseFlag, "StartingIndex", StartingIndex, "RequestedCount", RequestedCount)   
100      action.execute
101      val aa = action.outArgs 
102      action
103   }
104}
105
106/** stands in for a upnpdevice */
107@SoaAsset(meta=Upnp.Service, descr="a upnp service")
108@AssetMeta(name=Upnp.Service, inventory=classOf[UpnpInventory], descr="a service of a upnp device")
109@AssetAssoc(name=Upnp.DeviceHasServices, a=Upnp.Device, z=Upnp.Service, descr="a upnp service")
110class AUpnpService (val device:AUpnpDevice, val actual:UpnpService) extends AssetBaseImpl (NoAssetBrief) with DrawAsset {
111   this.brief = new AssetBriefImpl (new AssetKey (Upnp.Service, device.key.id+"///"+actual.serviceId), 
112         name=actual.serviceId, icon="service", briefDesc=actual.serviceType)
113   
114   override def render(t:Technology , stream:DrawStream ) : AnyRef = razie.Draw.seq(
115      super.render(t,stream),
116      razie.Draw.text("-------------my xml--------------"),
117      razie.Draw.xml (actual.toFullUpnpXml.toString),
118      razie.Draw.text("-------------original xml--------------"),
119      razie.Draw.xml (new String(actual.cyberService.getSCPDData())),
120      razie.Draw.text("-------------done--------------")
121      )
122}
123
124class UpnpInventory extends BaseInventory {
125
126    override def getAsset(key : AssetKey) = key.meta match {
127       case Upnp.Device  => 
128          Upnp.uc.devices.filter(_.friendlyName == key.id).map(Upnp.makeDevice(_)).head
129       case Upnp.Service => {
130          val pat = """(.*)///(.*)""".r
131          val pat (d,s) = key.id
132          val de = Upnp.uc.devices.filter(_.friendlyName == d).map(Upnp.makeDevice (_)).head
133          val sl = de.services
134          val se = de.services.filter (_.key.id == key.id).head
135         se 
136       }
137       case _ => None
138    }
139   
140    override def getBrief(key : AssetKey) : AssetBrief = 
141       getAsset (key).asInstanceOf[AssetBase].brief
142
143    override def doAction(cmd : String, key : AssetKey, ctx : ScriptContext) : AnyRef = "nothing"
144
145    override def getSupportedActions(key : AssetKey) : Array[ActionItem] = Array()
146
147//    def init (meta : Meta) = proxy.init(meta)
148   
149   /** queries can run in the background, they are multithreaded safe etc */
150   /** default only directs to queryAll ... */
151   override def query(criteria:QueryCriteria, env:AssetLocation , recurse:Boolean , toUse:AssetMap) : AssetMap = {
152      val ret = if (toUse != null) toUse else new AssetMap
153      
154      criteria match {
155         case AllOfType (Upnp.Device) => 
156            Upnp.uc.devices.map (Upnp.makeDevice(_)).foreach (x => ret.put(x.key, x.brief))
157         case ByAssoc (d, Upnp.DeviceHasServices) => 
158            getAsset(d).asInstanceOf[AUpnpDevice].services.foreach  (x => ret.put(x.key, x.brief))
159         case _ => 
160            throw new IllegalArgumentException ("I only support AllOfType queries right now")
161      }
162      ret
163    }
164
165    /** queries can run in the background, they are multithreaded safe etc */
166    override def queryAll(meta:String, env:AssetLocation , recurse:Boolean , toUse:AssetMap) : AssetMap = {
167      val ret = if (toUse != null) toUse else new AssetMap
168//       
169//      meta match {
170//          Upnp.uc.devices.map (new AUpnpDevice (_)).foreach (x => ret.put(x.key, x.brief))
171//       case Upnp.Service => {
172//       }
173//       }
174//       
175       ret
176    }
177}
178
179object RunMe extends Application {
180   org.cybergarage.util.Debug.on()
181   val cp = new UpnpControlPoint()
182   cp.start
183
184     
185   Thread.sleep(2000)
186     
187   println ("Devices: " + cp.devices.mkString("\n"))
188
189   val d = cp.devices.filter (_.friendlyName=="whs : TVersity Media Server").map(Upnp.makeDevice(_)).head
190   val da = d.asInstanceOf[AUpnpMediaServer]
191
192   val bb = { x:String => {
193      println (x+"--------------------")
194      var rma = da.Browse (x, "BrowseMetadata", "0", "100")
195      println (rma.outArgs.sa ("Result"))
196      println ("--------------------")
197      rma = da.Browse (x, "BrowseDirectChildren", "0", "100")
198      println (rma.outArgs.sa ("Result"))
199   }}
200
201   b("0")
202   b("0/3")
203   b("0/3/40")
204   b("0/3/40/41")
205   b("0/3/40/41/42")
206   b("0/3/40/41/42/34")
207
208   cp.stop
209   
210   def b (x:String) = {
211      println (x+"--------------------")
212      var rma = da.Browse (x, "BrowseMetadata", "0", "100")
213      println (rma.outArgs.sa ("Result"))
214      println ("--------------------")
215      rma = da.Browse (x, "BrowseDirectChildren", "0", "100")
216      println (rma.outArgs.sa ("Result"))
217   }
218  
219}
220