annotate mcserver/mcserver.py @ 27:c97feb7ef9e9

Strasbourg
author gyorgyf
date Thu, 20 Sep 2012 23:55:48 +0100
parents de603f9b7cfd
children f5ca9dbabe83
rev   line source
gyorgyf@0 1 #!/usr/bin/env python
gyorgyf@0 2 # encoding: utf-8
gyorgyf@0 3 """
gyorgyf@0 4 mcserver.py
gyorgyf@0 5
gyorgyf@0 6 Created by George Fazekas on 2012-06-16.
gyorgyf@0 7 Copyright (c) 2012 . All rights reserved.
gyorgyf@0 8 """
gyorgyf@0 9
gyorgyf@12 10 import os,sys,optparse,signal,time,math
gyorgyf@0 11 import cherrypy as cp
gyorgyf@0 12
gyorgyf@0 13 from cherrypy.lib import static
gyorgyf@0 14 import subprocess as sp
gyorgyf@0 15 from subprocess import Popen as spopen
gyorgyf@0 16
gyorgyf@0 17
gyorgyf@0 18 op = optparse.OptionParser()
gyorgyf@0 19 op.add_option('-u', '--user', action="store", dest="USER", default="server", type="str")
gyorgyf@0 20 options, args = op.parse_args()
gyorgyf@0 21 CONFIG_FILE = "mc%(USER)s.cfg" %options.__dict__
gyorgyf@0 22
gyorgyf@0 23 if not os.path.isfile(CONFIG_FILE) :
gyorgyf@4 24 print >> sys.stderr, "Config file not found: %s" %CONFIG_FILE
gyorgyf@0 25 sys.exit(-1)
gyorgyf@0 26
gyorgyf@0 27
gyorgyf@12 28 class Input(object):
gyorgyf@12 29
gyorgyf@27 30 def __init__(self,x,y,age,dist):
gyorgyf@12 31 self.time = time.time()
gyorgyf@12 32 self.x=float(x)
gyorgyf@12 33 self.y=float(y)
gyorgyf@12 34 self.count = 1
gyorgyf@27 35 self.age = age
gyorgyf@27 36 self.dist = dist
gyorgyf@12 37
gyorgyf@12 38 def __cmp__(self,other):
gyorgyf@12 39 d = math.sqrt( math.pow((self.x-other.x),2) + math.pow((self.y-other.y),2) )
gyorgyf@27 40 if d < self.dist :
gyorgyf@12 41 return 0
gyorgyf@12 42 else :
gyorgyf@12 43 return -1
gyorgyf@12 44
gyorgyf@12 45 def dead(self):
gyorgyf@27 46 return time.time()-self.time > self.age
gyorgyf@12 47
gyorgyf@12 48 def __repr__(self):
gyorgyf@27 49 # return (self.x,self.y,self.count).__repr__()
gyorgyf@27 50 return "(%.4f,%.4f,%i)" %(self.x,self.y,self.count)
gyorgyf@12 51
gyorgyf@12 52 def inc(self):
gyorgyf@12 53 self.time = time.time()
gyorgyf@12 54 self.count += 1
gyorgyf@27 55 # print self.count
gyorgyf@12 56
gyorgyf@12 57
gyorgyf@0 58 class MoodConductor:
gyorgyf@0 59
gyorgyf@5 60 def __init__(self):
gyorgyf@12 61 self.inputs = []
gyorgyf@27 62 self.age = 3
gyorgyf@27 63 self.dist = 0.1
gyorgyf@27 64 self.ninp = 12
gyorgyf@5 65
gyorgyf@27 66 # def index(self):
gyorgyf@27 67 # return str()
gyorgyf@0 68
gyorgyf@0 69 @cp.expose
gyorgyf@0 70 def mood(self,x,y):
gyorgyf@27 71 # print "Received coordinates", x,y, "\n"
gyorgyf@27 72 i = Input(x,y,self.age,self.dist)
gyorgyf@12 73 if i in self.inputs :
gyorgyf@12 74 self.inputs[self.inputs.index(i)].inc()
gyorgyf@12 75 else :
gyorgyf@12 76 self.inputs.insert(0,i)
gyorgyf@27 77 self.inputs = self.inputs[:self.ninp]
gyorgyf@27 78 cp.log.error("%s - %.6s-%.6s" %(str(cp.request.remote.ip),x,y))
gyorgyf@12 79 return str()
gyorgyf@5 80
gyorgyf@5 81 @cp.expose
gyorgyf@5 82 def result(self):
gyorgyf@12 83 for i in self.inputs :
gyorgyf@12 84 if i.dead() : self.inputs.remove(i)
gyorgyf@12 85 return self.inputs.__repr__()
gyorgyf@27 86
gyorgyf@27 87 @cp.expose
gyorgyf@27 88 def config(self,**kwargs):
gyorgyf@27 89 if kwargs.has_key('age') :
gyorgyf@27 90 self.age = float(kwargs['age'])
gyorgyf@27 91 if kwargs.has_key('dist') :
gyorgyf@27 92 self.dist = float(kwargs['dist'])
gyorgyf@27 93 if kwargs.has_key('ninp') :
gyorgyf@27 94 self.ninp = int(kwargs['ninp'])
gyorgyf@27 95 return str()
gyorgyf@27 96
gyorgyf@27 97 @cp.expose
gyorgyf@27 98 def getconf(self):
gyorgyf@27 99 self.config_list = ['age','dist','ninp']
gyorgyf@27 100 return str(map(lambda x: (x,"%.3f" % self.__dict__[x]),self.config_list)) #+ " Sessions: " + str(cp.tools.sessions)
gyorgyf@5 101
gyorgyf@0 102
gyorgyf@0 103
gyorgyf@0 104
gyorgyf@0 105 def getProcessPids(port,kill=False):
gyorgyf@0 106 '''Get the pid of the offending Python process given a port after an unsuccessful restart.'''
gyorgyf@0 107 print "Running lsof -i :"+str(port)," ...\n\n"
gyorgyf@0 108 command = "lsof -i :"+str(port)
gyorgyf@0 109 w = spopen(command,stdout=sp.PIPE,stderr=sp.PIPE,shell=True)
gyorgyf@0 110 se = w.stderr.readlines()
gyorgyf@0 111 result = w.stdout.readlines()
gyorgyf@0 112 exitcode = w.wait()
gyorgyf@0 113 if not result :
gyorgyf@0 114 print "getProcessPid:: Unable to obtain process pid. (lsof returned nothing. exitcode: %s)" %str(exitcode)
gyorgyf@0 115 return False
gyorgyf@0 116 import pprint
gyorgyf@0 117 pprint.pprint(result)
gyorgyf@0 118
gyorgyf@0 119 # get heading:
gyorgyf@0 120 ix = None
gyorgyf@0 121 head = result[0].upper()
gyorgyf@0 122 if 'PID' in head:
gyorgyf@0 123 head = filter(lambda x: x != str(), head.split(' '))
gyorgyf@0 124 head = map(lambda x: x.strip().replace(' ',''), head)
gyorgyf@0 125 if 'PID' in head : ix = head.index('PID')
gyorgyf@0 126 # get process pid
gyorgyf@0 127 pids = []
gyorgyf@0 128 for line in result :
gyorgyf@0 129 if 'python' in line.lower() :
gyorgyf@0 130 line = filter(lambda x: x != str(), line.split(' '))
gyorgyf@0 131 line = map(lambda x: x.strip().replace(' ',''), line)
gyorgyf@0 132 try :
gyorgyf@0 133 if ix :
gyorgyf@0 134 pids.append(int(line[ix]))
gyorgyf@0 135 else:
gyorgyf@0 136 numbers = filter(lambda x: x.isdigit(), line)
gyorgyf@0 137 pids.append(int(numbers[0]))
gyorgyf@0 138 except:
gyorgyf@0 139 print 'Error parsing lsof results.'
gyorgyf@0 140 return False
gyorgyf@0 141 print 'Pids found: ',pids
gyorgyf@0 142 # kill if specified
gyorgyf@0 143 if kill :
gyorgyf@0 144 pids_killed = []
gyorgyf@0 145 import signal
gyorgyf@0 146 for pid in pids:
gyorgyf@0 147 print 'Killing process: ',pid
gyorgyf@0 148 try :
gyorgyf@0 149 os.kill(pid,signal.SIGKILL)
gyorgyf@0 150 pids_killed.append(pid)
gyorgyf@0 151 except :
gyorgyf@0 152 print 'Failed: ',pid
gyorgyf@0 153 if pids_killed :
gyorgyf@0 154 print 'Processes killed:',pids_killed,' Waiting 10 secods...'
gyorgyf@0 155 import time
gyorgyf@0 156 time.sleep(10)
gyorgyf@0 157 return True
gyorgyf@0 158 return False
gyorgyf@0 159
gyorgyf@0 160
gyorgyf@0 161 def main(argv=None):
gyorgyf@0 162
gyorgyf@0 163 # Configure and start
gyorgyf@3 164 cp.config.update(CONFIG_FILE)
gyorgyf@0 165 cp.config.update({'tools.staticdir.root': os.getcwd()})
gyorgyf@3 166 cp.tree.mount(MoodConductor(),script_name="/moodconductor",config=CONFIG_FILE)
gyorgyf@2 167 port = int(cp.server.socket_port)
gyorgyf@3 168 ip = cp.server.socket_host
gyorgyf@3 169 print "Trying to bind: %(ip)s:%(port)s" %locals()
gyorgyf@0 170 getProcessPids(port,kill=True)
gyorgyf@0 171 cp.quickstart()
gyorgyf@0 172
gyorgyf@0 173 if __name__ == "__main__":
gyorgyf@0 174 main()