def connectToDb(self): """ """ # FIXME: # set-variable=max_connections=500 # set-variable=wait_timeout=200 logging.debug("Connecting to database...") db = None try: if config.get("database", "type") == "mysql": logging.debug("Using MySQL driver") db = DB.connect( host = config.get("database", "host"), port = config.getint("database", "port"), user = config.get("database", "user"), passwd = config.get("database", "password"), db = config.get("database", "database") ) else: logging.debug("Using Firebird driver") db = DB.connect( host = config.get("database", "host"), user = config.get("database", "user"), password = config.get("database", "password"), database = config.get("database", "database") ) self.checkDbStatus(db) logging.debug("Connected to database") except DB.DatabaseError, exc: logging.error("DB connection: " + str(exc))
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 isBlocked(self, uid): rc = True try: c = self.db.cursor() c.execute("select isblocked from users where id = %d" % uid) rs = c.fetchone() if rs is None: raise InnerException(47, ERRORS[47] % uid) else: try: if int(rs[0]) == 0: rc = False elif int(rs[0]) == 1: rc = True else: raise InnerException(45, ERRORS[45]) except Exception, msg: logging.error("Wrong value for isblocked = '%s'" % str(rs[0])) raise Exception(msg) except: raise return rc