def mongrel2(app, address, **options): import uuid sys.path.append(os.path.abspath(os.path.dirname(__file__))) from mongrel2 import handler conn = handler.Connection(str(uuid.uuid4()), "tcp://127.0.0.1:9997", "tcp://127.0.0.1:9996") mongrel2_handler(app, conn, debug=False)
import simplejson as json from mongrel2 import handler sender_id = "82209006-86FF-4982-B5EA-D1E29E55D481" conn = handler.Connection(sender_id, "tcp://127.0.0.1:9999", "tcp://127.0.0.1:9998") users = {} user_list = [] while True: try: req = conn.recv_json() except: print "FAILED RECV JSON" continue data = req.data if data["type"] == "join": conn.deliver_json(req.sender, users.keys(), data) users[req.conn_id] = data['user'] user_list = [u[1] for u in users.items()] conn.reply_json(req, {'type': 'userList', 'users': user_list}) elif data["type"] == "disconnect": print "DISCONNECTED", req.conn_id if req.conn_id in users: data['user'] = users[req.conn_id] del users[req.conn_id]
from mongrel2 import handler import json from wsgiref.handlers import SimpleHandler try: import io as StringIO except: import io DEBUG = False # setup connection handler # sender_id is automatically generated # so that each handler instance is uniquely identified conn = handler.Connection(str(uuid4()), "tcp://127.0.0.1:9997", "tcp://127.0.0.1:9996") def simple_app(environ, start_response): """Simplest possible WSGI application object""" status = '200 OK' response_headers = [('Content-type', 'text/plain')] start_response(status, response_headers) return ['Hello world!\n' for i in range(100)] def encode_response(data, encoding='utf-8'): '''Data is a list of strings''' return [d.encode(encoding) for d in data]
from mp3stream import ConnectState, Streamer from mongrel2 import handler import glob sender_id = "9703b4dd-227a-45c4-b7a1-ef62d97962b2" CONN = handler.Connection(sender_id, "tcp://127.0.0.1:9995", "tcp://127.0.0.1:9994") STREAM_NAME = "Mongrel2 Radio" MP3_FILES = glob.glob("*.mp3") print "PLAYING:", MP3_FILES CHUNK_SIZE = 8 * 1024 STATE = ConnectState() STREAMER = Streamer(MP3_FILES, STATE, CONN, CHUNK_SIZE, sender_id) STREAMER.start() HEADERS = {'icy-metaint': CHUNK_SIZE, 'icy-name': STREAM_NAME} while True: req = CONN.recv() if req.is_disconnect(): print "DISCONNECT", req.headers, req.body, req.conn_id STATE.remove(req) else:
os.path.isfile(pidfile) and os.unlink(pidfile) sys.exit(0) signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGABRT, signal_handler) pid = str(os.getpid()) with open(pidfile, 'w') as f: pid = str(os.getpid()) f.write(pid) sender_id = uuid.uuid4().hex conn = handler.Connection(sender_id, send_url, recv_url) worker = worker.Worker(sender_id, recv_url) while True: print "WAITING FOR REQUEST" req = conn.recv() response = "" if req.is_disconnect(): print "DISCONNECT" continue #a globally unique job id job_id = str(uuid.uuid1())