Skip to content

Commit

Permalink
- Server->Client communication is now JSON
Browse files Browse the repository at this point in the history
- Client->Server communication is still XMLRPC (lazy?)
- Moved more things to ajam.js
- Added requirement of json pytho module (py-json)


git-svn-id: https://semicomplete.googlecode.com/svn/pimp@204 66067f73-fe4c-0410-82e9-b9c6d0c95a22
  • Loading branch information
jordansissel committed Dec 21, 2005
1 parent 3de1f6a commit 6a821d6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 61 deletions.
3 changes: 3 additions & 0 deletions modules/ConnectionHandler.py
Expand Up @@ -10,6 +10,8 @@
from Error404Plug import Error404Plug
from SendContentPlug import SendContentPlug
from GenerateContentPlug import GenerateContentPlug
from JSONRPCPlug import JSONRPCPlug


from MusicDB import MusicDB

Expand Down Expand Up @@ -38,6 +40,7 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
'': RedirectPlug,
'control': ControlWebPlug,
'xmlrpc': ControlXMLRPCPlug,
'json': JSONRPCPlug,
'content': SendContentPlug,
'static': SendContentPlug,
'dynamic': GenerateContentPlug,
Expand Down
2 changes: 1 addition & 1 deletion modules/MP3Stream.py
@@ -1,6 +1,6 @@
from MusicDB import MusicDB

class MP3Stream:
class MP3Stream:
def __init__(self, request, name="Unknown Stream"):
#self.music = MusicList()
self.clients = []
Expand Down
31 changes: 22 additions & 9 deletions modules/RPC.py
@@ -1,19 +1,21 @@

from MusicDB import MusicDB
import xmlrpclib
import json

class RPC:
ofunc = None
def __init__(self):
self.streamlist = MusicDB.instance.streamlist
self.respond = self.json_respond

def call(self, method, params, ofunc):
self.ofunc = ofunc
print "Calling RPC Method %s" % method
func = getattr(self, "call_%s" % method, self.invalidcall)
func(params)

def respond(self, args):
def xmlrpc_respond(self, args):
m = xmlrpclib.Marshaller("US-ASCII", 1)

self.ofunc("<methodResponse>")
Expand All @@ -23,14 +25,9 @@ def respond(self, args):
elif type(args) == type([]):
m.dump_array(args, self.ofunc)
self.ofunc("</methodResponse>")


def call_control(self, params):
print "CONTROL"
hash = {
"result": str(int(params[0]["p1"]) + int(params[0]["p2"]))
}
self.respond(hash)
def json_respond(self, args):
self.ofunc(json.write(args))

def call_search(self, params):
results = []
Expand Down Expand Up @@ -68,8 +65,24 @@ def call_enqueue(self, params):
stream = self.streamlist[params["stream"]]
stream.enqueue(params["list"]);
self.respond("ok")


def call_loadstream(self, params):
print "Loadstream called"
params = params[0]
print params
stream = self.streamlist[params["stream"]]

streaminfo = {
'song': stream.song,
'name': stream.name,
'clients': len(stream.clients),
'queue': stream.queue,
}

self.respond(streaminfo)

def invalidcall(self, params):
print "Invalid call"
print "Params: ", params
self.respond("INVALID CALL")

1 change: 1 addition & 0 deletions pimp.py
Expand Up @@ -4,6 +4,7 @@
import urllib

sys.path.append("./modules")
sys.path.append("./extra")

# stuff I wrote
from template import Template
Expand Down
30 changes: 22 additions & 8 deletions static/ajam.js
Expand Up @@ -4,7 +4,7 @@ function callrpc(method, args, callback, url) {
return
}
if (!url)
url = "/xmlrpc/control";
url = "/json/control";

var xmlrpc = new XMLHttpRequest();

Expand Down Expand Up @@ -36,26 +36,25 @@ function callrpc(method, args, callback, url) {
return
}


debug("Type: " + type)
if (type == "text/xml") {
debug("xml processing");
if (doc.childNodes[0].tagName != "methodResponse") {
debug("UNEXPECTED NON-XMLRPC RESPONSE FROM SERVER");
return;
}
var hash = rpcparam2hash(doc.childNodes[0]);
xmlrpc.mycallback(hash)
} else if (type == "application/xhtml+xml") {
//Object.dpDump(xmlrpc)
xmlrpc.mycallback(xmlrpc)
} else if (type == "text/html") {
xmlrpc.mycallback(xmlrpc)
} else if (type = "text/json") {
debug("JSON processing")
var params;
eval("params = " + xmlrpc.responseText);
xmlrpc.mycallback(params)
} else if (type = "text/plain") {
xmlrpc.mycallback(xmlrpc.responseText)
} else {
debug("UNEXPECTED NON XML/HTML/PLAIN RESPONSE FROM SERVER");
}

}
}

Expand Down Expand Up @@ -187,3 +186,18 @@ function delete_children(element, recursive) {
}
}

var debugcnt = 0
function debug(val) {
debugcnt++;
var list = document.getElementById("debug");
var foo = mkelement("div");
var text = mktext(debugcnt + ": " + val);
foo.style.fontWeight="bold";
foo.appendChild(text);
list.appendChild(foo);
}

function cleardebug() {
delete_children(document.getElementById("debug"))
}

56 changes: 15 additions & 41 deletions static/newpimp.js
Expand Up @@ -53,13 +53,13 @@ function call_updatestreams() {
}

function liststreams_callback(params) {
var idx = 0
var idx = 0;
var i;

var updates = []

for (i in params[0]) {
updatestream(i, params[0][i], idx)
var updates = [];

for (i in params) {
updatestream(i, params[i], idx)
updates.push("stream:" + i)
idx++;
}
Expand All @@ -70,16 +70,13 @@ function liststreams_callback(params) {
var el = table.childNodes[i];
if (el.id) {
var found = 0
//debug(table.childNodes[i].tagName + ": " + table.childNodes[i].id)
for (var x = 0; x < updates.length && !found; x++) {
if (updates[x] == el.id) {
if (updates[x] == el.id)
found = 1;
}
}

if (!found) {
if (!found)
table.removeChild(el);
}

}
}
Expand Down Expand Up @@ -213,23 +210,6 @@ function searchclick_callback(params) {
showsearchresults(params[0]);
}


function cleardebug() {
// Delete children
//document.getElementById("debug").innerHTML = "";
delete_children(document.getElementById("debug"))
}

function debug(val) {
var list = document.getElementById("debug");
var foo = mkelement("div");
var text = mktext(val);
foo.style.fontWeight="bold";
foo.appendChild(text)
list.appendChild(foo)
//list.style.display="none";
}

function stream_drilldown() {
// Show the searchbar now that we've selected a stream
//document.getElementById("searchbar").style.display="block";
Expand All @@ -238,10 +218,7 @@ function stream_drilldown() {
debug("Drilling into " + this.id.substr(7));

/* Ask pimp to generate us a stream entry page */
//callrpc("load_stream", {"stream":this.id}, loadstream, "/dynamic/streaminfo?"+this.id);

loader = loadstream
document.getElementById("loaderframe").src="http://kenya.csh.rit.edu:8003/"
callrpc("load_stream", {"stream":this.id}, loadstream);
}

function populate_stream_pane(streamname) {
Expand Down Expand Up @@ -451,16 +428,13 @@ function enqueue_callback() {
}

function loadstream() {
var ifr = document.getElementById("loaderframe")

//Object.dpDump(ifr)
//for (a in ifr) {
//debug("ifr: " + a + " => " + ifr[a])
//}
var p = document.getElementById("container")
delete_children(p)
//p.appendChild(doc.responseXML.childNodes[0])
p.appendChild(ifr.contentDocument.childNodes[0])
/* Ask the server about the stream */
callrpc("loadstream", {'stream': pimp["currentstream"]}, loadstream_callback)
}

function loadstream_callback(params) {
debug("LOADSTREAM")
Object.dpDump(params)
}

function pageload() {
Expand Down
2 changes: 0 additions & 2 deletions templates/layout.html
Expand Up @@ -43,8 +43,6 @@ <h4>Pimp 4.0</h4>
</div>
</div>


<iframe id="loaderframe" width="0" height="0" style="display:none" onload="pageload()"/>
<div id="debug"/>
</body>
</html>

0 comments on commit 6a821d6

Please sign in to comment.