def __init__(self, HOST = '', PORT = 2468, rootPassword = ''): """ Constructor of the server Defines many variables @HOST = Which Adress to listen to, Default : listens on all available interfaces @PORT = Which Port to listen on, Default : 50007 @rootPassword = Defines the root password, to administrate the server (shutdown, ...) <Returns nothing""" # Version self.version = version # Root password self.rootPassword = rootPassword # Connection informations self.HOST = HOST self.PORT = PORT # Connected clients and available rooms self.allNickNames = {} self.allRooms = {} # DB connection flag self.dbOk = False try: asyncore.dispatcher.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.set_reuse_addr() self.bind((self.HOST, self.PORT)) self.listen(5) except: logging.error("Error while starting: Network address and port already in use ?") sys.exit() if self.HOST == "": showHost = "All interfaces" else: showHost = self.HOST logging.info("Running on: %s" % showHost) logging.info("Port: %s" % self.PORT) self.db = self.connectToDb() if self.db is None: self.serverShutDown() self.timer = threading.Thread(target = self.timerThread, args=(self,)) self.timer.start()
def stop(self, pid, restart, msg = None): logging.info("Stopping Palabre...") if not pid: if self.daemon: sys.stderr.write("Could not stop, Palabre is not running (pid file '%s' is missing).\n" % self.pidfile) logging.warning("Could not stop, Palabre is not running (pid file '%s' is missing)." % self.pidfile) if not restart: logging.shutdown() logfile.close() sys.exit(1) else: ## There should be a better way, but i'm too lazy to search... but ## i'm sure there's a better way # if we're not a daemon we should clean up before committing suicide if not self.daemon: os.remove(self.pidfile) logging.shutdown() logfile.close() try: #global topServer #topServer.notifyStop() #time.sleep(1) print "sending signal..." os.kill(pid, signal.SIGUSR1) #os.system("kill -USR1 %d" % pid) print "signal sent" while True: print "in the signal loop", pid os.kill(pid,SIGTERM) os.kill(pid,signal.SIGKILL) time.sleep(1) except OSError, e: if e.strerror.find("No such process") >= 0: os.remove(self.pidfile) if not restart: logging.info("...stopped.") logging.shutdown() logfile.close() sys.exit(0) else: logging.error("%s" % e.strerror) logging.shutdown() logfile.close() sys.exit(1) except Exception, e: logging.error("%s" % e.strerror) logging.shutdown() logfile.close() sys.exit(1)
def _clientLeft(self, uid): self.db.commit() self._showSessions() self.db.begin() c = self.db.cursor() s = "select id, sesid from sessions where userid = %d" % uid c.execute(s) rs = c.fetchone() if rs is None: logging.error("Disconnect was requested by user which does not have associated session (id='%d', sesId='unknown')" % (uid)) return s = "delete from sessions where id = %d" % int(rs[0]) c.execute(s) self.db.commit() self._showSessions() logging.info("User #%d successfully logged off" % uid)
def serverClientQuit(self, nickName, uid = None): """Method to handle a client deconnection @nickName : Nickname of the client to disconnect. <Returns nothing""" if uid is not None: self.setLastIP(uid) self._clientLeft(uid) self.leaveAllRooms(uid) logging.info("Client left: %s" % nickName) # Check if it really exists if nickName != "" and self.allNickNames.has_key(nickName): del self.allNickNames[nickName] # Notify every rooms # We should only notify its rooms, # But PalabreClient Instance might already be detroyed # So we lost all informations .... for p in self.allRooms.values(): p.roomRemoveClient(nickName)
def start(self, msg = None): logging.info("Starting Palabre...") if not __debug__ and os.path.exists(self.pidfile): if self.daemon: sys.stderr.write("Could not start, Palabre is already running (pid file '%s' exists).\n" % self.pidfile) logging.warning("Could not start, Palabre is already running (pid file '%s' exists)." % self.pidfile) logging.shutdown() logfile.close() sys.exit(1) else: if self.daemon: # Do first fork. try: pid = os.fork () if pid > 0: # Exit first parent. sys.exit (0) except OSError, e: sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # Decouple from parent environment. os.chdir("/") os.umask(0) os.setsid() # Do second fork. try: pid = os.fork() if pid > 0: # Exit second parent. sys.exit(0) except OSError, e: sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1)