/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

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