[vlma-devel] commit: Cleanup of the implementation of the web export of VLC state. ( Adrien Grand )

git version control git at videolan.org
Thu Oct 30 23:17:14 CET 2008


vlma | branch: master | Adrien Grand <jpountz at videolan.org> | Thu Oct 30 23:15:17 2008 +0100| [774bc2ce85f444950bca918748bd5529fc8f34b2] | committer: Adrien Grand 

Cleanup of the implementation of the web export of VLC state.

Use twisted.web.resource.Resource to describe the resource tree.

> http://git.videolan.org/gitweb.cgi/vlma.git/?a=commit;h=774bc2ce85f444950bca918748bd5529fc8f34b2
---

 .../videolan/vlma/retriever/HttpDataRetriever.java |    2 +-
 vlma-watchdog/src/vlc.py                           |   50 +------------
 vlma-watchdog/src/web.py                           |   74 ++++++++++++++++++++
 3 files changed, 78 insertions(+), 48 deletions(-)

diff --git a/vlma-core/src/main/java/org/videolan/vlma/retriever/HttpDataRetriever.java b/vlma-core/src/main/java/org/videolan/vlma/retriever/HttpDataRetriever.java
index 88172bd..704da2b 100644
--- a/vlma-core/src/main/java/org/videolan/vlma/retriever/HttpDataRetriever.java
+++ b/vlma-core/src/main/java/org/videolan/vlma/retriever/HttpDataRetriever.java
@@ -43,7 +43,7 @@ public class HttpDataRetriever implements DataRetriever {
         }
         try {
             URL url = new URL("http://" + server.getIp().getHostName() + ":"
-                    + configuration.getInt("vlc.monitor.http.port") + "/monitor/");
+                    + configuration.getInt("vlc.monitor.http.port") + "/monitor/?format=pb2");
             URLConnection conn;
             conn = url.openConnection();
             String userPassword = configuration.getString("vlc.monitor.http.login") + ":"
diff --git a/vlma-watchdog/src/vlc.py b/vlma-watchdog/src/vlc.py
index 700fb05..5c2f1e0 100644
--- a/vlma-watchdog/src/vlc.py
+++ b/vlma-watchdog/src/vlc.py
@@ -19,11 +19,7 @@ SERVER_PASSWORD = "admin"
 
 # Imports
 
-import logging, os, signal, sys, threading, time
-from twisted.web import server, resource
-from twisted.internet import reactor
-from twisted.web import http
-from data_pb2 import ServerState
+import logging, os, signal, sys, threading, time, web
 
 global vlcInstance
 
@@ -149,7 +145,7 @@ class Monitor(threading.Thread):
           return 0.
         else:
           if NETWORK_INTERFACE in line:
-            return double(line.replace(":", " ").split()[param])
+            return float(line.replace(":", " ").split()[param])
     except Exception, e:
       logger.error(e)
       return 0.
@@ -160,46 +156,6 @@ class Monitor(threading.Thread):
   def getTrafficOut(self):
     return self._getTrafficX(9)
 
-class ServerStateResource(resource.Resource):
-  isLeaf = True
-
-  def __init__(self, monitor):
-    self.monitor = monitor
-
-  def render_GET(self, request):
-    if(request.getUser() != SERVER_LOGIN or request.getPassword() != SERVER_PASSWORD):
-      request.setResponseCode(http.UNAUTHORIZED)
-      request.setHeader('WWW-authenticate', 'basic realm="VLC monitor HTTP interface"')
-      return "Authentication needed"
-    state = ServerState()
-    state.cpuLoad = self.monitor.getCpuLoad()
-    state.vlcCpu = self.monitor.getVlcCpu()
-    state.vlcMem = self.monitor.getVlcMem()
-    state.trafficIn = self.monitor.getTrafficIn()
-    state.trafficOut = self.monitor.getTrafficOut()
-    if request.uri.startswith("/monitor/"):
-      return state.SerializeToString()
-    else:
-      html = "CPU load: " + str(state.cpuLoad)
-      html += "<br />VLC CPU: " + str(state.vlcCpu)
-      html += "<br />VLC Mem: " + str(state.vlcMem)
-      html += "<br />Traffic in: " + str(state.trafficIn)
-      html += "<br />Traffic out: " + str(state.trafficOut)
-      return html
-
-class WebServer(threading.Thread):
-
-  def __init__(self, monitor):
-    self.logger = logging.getLogger("Web Server")
-    threading.Thread.__init__(self)
-    self.monitor = monitor
-    self.setDaemon(True)
-
-  def run(self):
-    site = server.Site(ServerStateResource(self.monitor))
-    reactor.listenTCP(SERVER_PORT, site)
-    reactor.run(installSignalHandlers=False)
-
 
 def signalHandler(signum, frame):
   logger = logging.getLogger("VLC")
@@ -224,7 +180,7 @@ if __name__ == '__main__':
   vlcInstance.start()
   monitor.start()
   # Start web server
-  webserver = WebServer(monitor)
+  webserver = web.Server(monitor)
   webserver.start()
   # Main thread
   while(True):
diff --git a/vlma-watchdog/src/web.py b/vlma-watchdog/src/web.py
new file mode 100644
index 0000000..34721de
--- /dev/null
+++ b/vlma-watchdog/src/web.py
@@ -0,0 +1,74 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import logging, threading
+from twisted.web import server, resource
+from twisted.internet import reactor
+from twisted.web import http
+from data_pb2 import ServerState
+from vlc import SERVER_PORT, SERVER_LOGIN, SERVER_PASSWORD
+
+class Server(threading.Thread):
+
+  def __init__(self, monitor):
+    self.logger = logging.getLogger("Web Server")
+    threading.Thread.__init__(self)
+    self.monitor = monitor
+    self.setDaemon(True)
+
+  def run(self):
+    root = resource.Resource()
+    root.putChild("", IndexResource())
+    root.putChild("monitor", ServerStateResource(self.monitor))
+    site = server.Site(root)
+    reactor.listenTCP(SERVER_PORT, site)
+    reactor.run(installSignalHandlers=False)
+
+
+class AuthenticationRequiredResource(resource.Resource):
+
+  def isAuthenticated(self, request):
+    if(request.getUser() != SERVER_LOGIN or request.getPassword() != SERVER_PASSWORD):
+      request.setResponseCode(http.UNAUTHORIZED)
+      request.setHeader('WWW-authenticate', 'basic realm="VLC monitor HTTP interface"')
+      request.write("Authentication required")
+      return False
+    return True
+
+
+class IndexResource(AuthenticationRequiredResource):
+  isLeaf = True
+
+  def render_GET(self, request):
+    if(not self.isAuthenticated(request)):
+      return ""
+    html = "<a href=\"monitor/\">VLC state</a>"
+    return html
+
+
+class ServerStateResource(AuthenticationRequiredResource):
+  isLeaf = True
+
+  def __init__(self, monitor):
+    self.monitor = monitor
+
+  def render_GET(self, request):
+    if(not self.isAuthenticated(request)):
+      return ""
+    state = ServerState()
+    state.cpuLoad = self.monitor.getCpuLoad()
+    state.vlcCpu = self.monitor.getVlcCpu()
+    state.vlcMem = self.monitor.getVlcMem()
+    state.trafficIn = self.monitor.getTrafficIn()
+    state.trafficOut = self.monitor.getTrafficOut()
+    if request.args.has_key("format") and request.args["format"][0] == "pb2":
+      return state.SerializeToString()
+    else:
+      html = []
+      html.append("CPU load: " + str(state.cpuLoad))
+      html.append("VLC CPU: " + str(state.vlcCpu))
+      html.append("VLC Mem: " + str(state.vlcMem))
+      html.append("Traffic in: " + str(state.trafficIn))
+      html.append("Traffic out: " + str(state.trafficOut))
+      return "<br />\n".join(html)
+



More information about the vlma-devel mailing list