Skip to content

Commit

Permalink
Check-in extra files from melkor:
Browse files Browse the repository at this point in the history
- sendfile module
- cometlib.js
- more test apps



git-svn-id: http://chiral.j4cbo.com/svn/trunk@85 a827fe29-2235-0410-a7a9-cacdfe24a5d3
  • Loading branch information
jacob committed Dec 28, 2007
1 parent 4f8e161 commit c022662
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 0 deletions.
8 changes: 8 additions & 0 deletions chaintest.py
@@ -0,0 +1,8 @@
from chiral.core import coroutine

def chain():
yield
raise coroutine.CoroutineRestart(chain())

print "..."
coroutine.Coroutine(chain(), autostart=True)
76 changes: 76 additions & 0 deletions chiral/os/sendfile.py
@@ -0,0 +1,76 @@
"""
epoll() wrapper using ctypes
"""

# Chiral, copyright (c) 2007 Jacob Potter
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2.

import ctypes
from ctypes.util import find_library
import os

libc = ctypes.CDLL(find_library("c"))

try:
getattr(libc, "sendfile")
except AttributeError:
raise ImportError("sendfile not available on this system")

def _sendfile4(out_file, in_file, offset, count):
"""
Wrapper for Linux sendfile(2) system call.
out_file must be a socket; in_file must be a regular file object.
Returns the number of bytes actually written.
"""

offset = ctypes.c_int(offset)

ret = libc.sendfile(out_file.fileno(), in_file.fileno(), ctypes.byref(offset), int(count))

if ret < 0:
err = ctypes.c_int.in_dll(libc, "errno").value
raise OSError(err, os.strerror(err))

return ret

def _sendfile6(out_file, in_file, offset, count):
"""
Wrapper for FreeBSD sendfile(2) system call.
out_file must be a socket; in_file must be a regular file object.
Returns the number of bytes actually written.
"""

count = int(count)
if count == 0:
return 0

sbytes = ctypes.c_int()

ret = libc.sendfile(
out_file.fileno(),
in_file.fileno(),
int(offset),
int(count),
None,
ctypes.byref(sbytes),
0
)

if ret < 0:
err = ctypes.c_int.in_dll(libc, "errno").value
raise OSError(err, os.strerror(err))

return sbytes.value

if os.uname()[0] == "Linux":
sendfile = _sendfile4
elif os.uname()[0] == "FreeBSD":
sendfile = _sendfile6
else:
raise ImportError("Unknown OS.")

__all__ = [ "sendfile" ]
76 changes: 76 additions & 0 deletions chiral/web/cometlib.js
@@ -0,0 +1,76 @@
var chiral = {
version : "$Rev $",
web : {},
log : function(message) { }
}

chiral.web.CometStream = function(source_url, data_callback) {

method = "iframe";

if (source_url.indexOf("?") != -1) {
source_url = source_url + "&_chiral_comet_method=" + method;
} else {
source_url = source_url + "?_chiral_comet_method=" + method;
}

this.run_MXMR = function() {
var xhr = new XMLHttpRequest();
xhr.multipart = true;
xhr.open("GET", source_url);
xhr.onload = function(event) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
data_callback(event.responseText);
} else {
chiral.log("chiral.web.CometStream MXMR failed: " + xhr.statusText);
}
}
}

xhr.send("")
}

this.run_iframe = function() {

/* Create the iframe in which the page will be loaded */
var ifr = document.createElement("iframe");
ifr.src = source_url;

/* To make sure the iframe remains hidden, set both DOM level 1
and CSS properties
*/
ifr.style.visibility = "hidden";
ifr.style.position = "absolute";
ifr.style.border = "0px none";
ifr.style.left = "-10px";
ifr.style.top = "-10px";
ifr.width = 0;
ifr.height = 0;
ifr.marginWidth = 0;
ifr.marginHeight = 0;
ifr.frameBorder = 0;
ifr.scrolling = 0;

ifr.iframe_info = "test";

/* We may be able to attach it now; if not, then add a callback */
if (document.body) {
document.body.appendChild(ifr);
} else if (document.attachEvent) {
document.attachEvent("onload", function() {
document.body.appendChild(ifr);
});
} else {
window.addEventListener("load", function() {
document.body.appendChild(ifr);
}, true);
}
setTimeout(function() { alert(ifr.contentWindow) }, 1000);
}

this["run_" + method]();
}

var cs = new chiral.web.CometStream("/static/c.html", function(s) { alert(s); })

16 changes: 16 additions & 0 deletions dbustest.py
@@ -0,0 +1,16 @@
#/usr/bin/env python2.5

print "Loading modules..."

from chiral.core import stats
from chiral.net import reactor, dbus

print "Initializing..."

sb = dbus.SystemBusConnection()

print "Running..."

reactor.run()

stats.dump()
5 changes: 5 additions & 0 deletions static/c.html
@@ -0,0 +1,5 @@
<html>
<script>
alert(window.iframe_info);
</script>
</html>
7 changes: 7 additions & 0 deletions static/test.html
@@ -0,0 +1,7 @@
<html>
<head>
<script type="text/javascript" src="/cometlib"></script>
</head>
<body>
Comet Test
</body>
27 changes: 27 additions & 0 deletions threadpooltest.py
@@ -0,0 +1,27 @@
#/usr/bin/env python2.5

print "Loading modules..."

from chiral.core import stats, threadpool
from chiral.core.coroutine import as_coro
from chiral.net import reactor, dbus
import time

from chiral.web.httpd import HTTPServer
from chiral.web.introspector import Introspector


print "Running..."

@as_coro
def threadpool_test():
print "sleeping..."
yield threadpool.run_in_thread(time.sleep, 1)
print "done"

#HTTPServer(bind_addr = ('', 8082), application = Introspector())

threadpool_test().start()

reactor.run()
stats.dump()

0 comments on commit c022662

Please sign in to comment.