if len(sys.argv) > 2: SERVER_PORT = int(sys.argv[2]) MDIR = MDIR + "_" + str(SERVER_PORT) if os.path.exists(MDIR) == False: try: os.mkdir(MDIR) except Exception, err: __LOG__.Exception(err) sys.exit() CONF_FILE = sys.argv[1] if os.path.exists(CONF_FILE) == False: print 'Error : %s not exists' % CONF_FILE sys.exit() conf = ConfigParser.ConfigParser() conf.read(CONF_FILE) if conf.has_section("CONF") and conf.has_option("CONF", "LOGFILE"): fileName = conf.get("CONF", "LOGFILE") Log.Init(Log.CRotatingLog(fileName, 10000000, 3)) else: Log.Init(Log.CStandardErrorLog()) try: main() except: __LOG__.Exception()
def main(): global SHUTDOWN try: optStr = sfp.getOptStr() optStr += "f:" optList, args = getopt.getopt(sys.argv[1:], optStr, [ "deny=", ]) if len(args) != 2: raise Exception opts = {} for optKey, optVal in optList: opts[optKey] = optVal except: print 'VERSION: %s' % VERSION print 'usage : %s [-options] servIP servPort' % (sys.argv[0]) sfp.prnOpt() print " -f[logfilePath] : logfilePath" print " --deny=host.deny : host deny ip" sys.exit() key1Hash = {} key2Hash = {} keyBothHash = {'*,*': []} if opts.has_key("-f"): # -f logfilePath fileName = opts["-f"] Log.Init(Log.CRotatingLog(fileName, 10000000, 3)) else: # opts Log.Init(Log.CStandardErrorLog()) pType = 1 # default protocol type 1 if opts.has_key("-p"): try: pType = int(opts["-p"]) except: pType = 1 denyIpHash = {} if opts.has_key("--deny"): fileName = opts["--deny"] readDenyFile(fileName, denyIpHash) servIP = args[0] servPort = int(args[1]) servsock = socket(AF_INET, SOCK_STREAM) servsock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) servsock.bind(('', int(servPort))) servsock.listen(10) #servsock.setblocking(False) # non block global inSockList, sockConnectorHash inSockList = [servsock] sockConnectorHash = {} conList = [] __LOG__.Trace("[%s:%s] Start ---" % (servIP, servPort)) startSec = time.time() qryCnt = 1 while SHUTDOWN == False: try: #{ try: inputReady,outputReady,exceptReady = \ select.select(inSockList,[],[], 0.1) except Exception, err: __LOG__.Exception() raise sfp.SFPDiscon, err if len(inputReady) == 0: aliveSec = time.time() - startSec if aliveSec >= 60: __LOG__.Trace( "[%s:%s] ready, conList=[%s], qryCnt=[%s]" \ % (servIP, servPort, len(conList), qryCnt) ) startSec = time.time() for th in conList: if th.heartBeatSec != 0: th.ansHeart() if qryCnt % 100 == 0: for th in conList: if th.heartBeatSec != 0: th.ansHeart() if qryCnt % 10000 == 0: __LOG__.Trace( "[%s:%s] ready, conList=[%s], qryCnt=[%s]" \ % (servIP, servPort, len(conList), qryCnt) ) idx = 0 for sock in inputReady: #{ idx += 1 __LOG__.Trace("inputReady, [%d/%d]" % (idx, len(inputReady))) qryCnt += 1 if sock == servsock: cSock, addr = servsock.accept() # 접속 거부 if denyIpHash.has_key(addr[0]): cSock.close() __LOG__.Trace("deny ip [%s]" % addr[0]) continue inSockList.append(cSock) addr = str(addr) connector = Connector(cSock, addr, key1Hash, key2Hash, keyBothHash, conList, **opts) sockConnectorHash[cSock] = connector __LOG__.Trace("connected, addr=%s" % addr) else: connector = sockConnectorHash[sock] try: # try idx = 0 readData(sock, key1Hash, key2Hash, keyBothHash) while pType == 6: if (not readData(sock, key1Hash, key2Hash, keyBothHash, True)): __LOG__.Trace( "+++++ readData END TIMEOUT ++++++ %s,fd:[%d]" % (addr, sock.fileno())) break idx += 1 if idx >= 100: __LOG__.Trace( "+++++ readData END 100 ++++++ %s,fd:[%d]" % (addr, sock.fileno())) break except Exception, err: __LOG__.Exception() try: inSockList.remove(sock) except: pass try: del (sockConnectorHash[sock]) except: pass connector.deregister() sock.close() continue