Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8
- #
- # Copyright (C) 2011 Benjamin Jemlich <[email protected]>
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU Affero General Public License as
- # published by the Free Software Foundation, either version 3 of the
- # License, or (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU Affero General Public License for more details.
- #
- # You should have received a copy of the GNU Affero General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- import json
- import asyncore
- import socket
- # Settings
- prxstr = "Meta:tcp -h 127.0.0.1 -p 6502"
- serverid = 1
- listen_ip = '0.0.0.0'
- listen_port = 64777
- slicefile = "Murmur.ice"
- def loadUser(u):
- user = {}
- user['session'] = u.session
- user['userid'] = u.userid
- user['mute'] = u.mute
- user['deaf'] = u.deaf
- user['suppress'] = u.suppress
- user['selfMute'] = u.selfMute
- user['selfDeaf'] = u.selfDeaf
- user['channel'] = u.channel
- user['name'] = u.name
- user['onlinesecs'] = u.onlinesecs
- user['idlesecs'] = u.idlesecs
- user['os'] = u.os
- user['release'] = u.release
- user['bytespersec'] = u.bytespersec
- return user
- def loadChannel(c):
- channel = {}
- channel['id'] = c.c.id
- channel['name'] = c.c.name
- channel['parent'] = c.c.parent
- channel['links'] = []
- channel['position'] = c.c.position
- channel['channels'] = [loadChannel(child) for child in c.children]
- channel['users'] = [loadUser(u) for u in c.users]
- return channel
- import Ice
- Ice.loadSlice("", ["-I" + Ice.getSliceDir(), "--checksum", slicefile])
- import Murmur
- #ice = Ice.initialize(['--Ice.Plugin.IceSSL=IceSSL:createIceSSL', '--IceSSL.Ciphers=ADH'])
- ice = Ice.initialize()
- prx = ice.stringToProxy(prxstr)
- m = Murmur.MetaPrx.checkedCast(prx)
- s = m.getServer(serverid)
- class CvpHandler(asyncore.dispatcher_with_send):
- def __init__(self, sock):
- asyncore.dispatcher_with_send.__init__(self, sock)
- def handle_read(self):
- data = self.recv(8192)
- if data.strip() == "json":
- tree = s.getTree()
- version = m.getVersion()
- conf = s.getAllConf()
- defconf = m.getDefaultConf()
- if not 'port' in conf:
- conf['port'] = defconf['port']
- if not 'users' in conf:
- conf['users'] = defconf['users']
- server = {}
- server['name'] = conf["registername"]
- # server['name'] = "GameTracker"
- # server['id'] = tree.c.id
- server['id'] = serverid
- server["x_gtmurmur_connectport"] = conf['port']
- server["x_gtmurmur_max_users"] = conf['users']
- server["x_gtmurmur_server_version"] = "%d.%d.%d" % (version[0], version[1], version[2])
- server['x_uptime'] = s.getUptime()
- server['root'] = loadChannel(tree)
- data = json.dumps(server, indent = 4)
- server["x_gtmurmur_doclen"] = len(data) + 4 + 2 + 18 + 1 + 1 + 1 + 1 + len(str(len(data)))
- # data = json.dumps(server)
- # server["x_gtmurmur_doclen"] = len(data) + 2 + 18 + 1 + 1 + 1 + len(str(len(data)))
- print "sending reply"
- self.send(json.dumps(server, indent = 4))
- # self.send(json.dumps(server))
- def handle_write(self):
- asyncore.dispatcher_with_send.handle_write(self)
- if len(self.out_buffer) == 0:
- self.handle_close()
- def handle_close(self):
- print "Closed connection"
- self.close()
- class CvpServer(asyncore.dispatcher):
- def __init__(self, host, port):
- asyncore.dispatcher.__init__(self)
- self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
- self.set_reuse_addr()
- self.bind((host, port))
- self.listen(5)
- def handle_accept(self):
- pair = self.accept()
- if pair is None:
- pass
- else:
- sock, addr = pair
- print 'Incoming connection from %s' % repr(addr)
- handler = CvpHandler(sock)
- server = CvpServer(listen_ip, listen_port)
- asyncore.loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement