Ejemplo n.º 1
0
def main():
    global listenip, listenport
    global track, masterip, masterport
    global groupid, master
    global haystack_path, haystack_index_path
    haystack_logging.init_logger("storage", logging.DEBUG)

    config = {}
    if len(sys.argv) == 1:
        logging.error("needs config file")
        return
    config_file = sys.argv[1]
    execfile(config_file, config)
    haystack_path = config["dbfilename"]
    haystack_index_path = config["dbindexfilename"]
    groupid = config["groupid"]

    if not file_exists(haystack_path) and not haystack.create_store(haystack_path):
        logging.error("create store file fail")
        sys.exit(1)
    if not file_exists(haystack_index_path) and not haystack.create_index(haystack_index_path):
        logging.error("create index file fail")
        sys.exit(1)
    if not haystack.recover(haystack_path, haystack_index_path):
        logging.error("recover haystack store fail")
        sys.exit(1)
    if not haystack.load(haystack_path, haystack_index_path):
        logging.error("load haystack file fail")
        sys.exit(1)

    masterip = config["masterip"] if config.has_key("masterip") else ""
    masterport = config["masterport"] if config.has_key("masterport") else 0
    listenip = config["listenip"]
    listenport = config["listenport"]

    trackip = config["trackip"]
    trackport = config["trackport"]
    track = HaystackTrack()
    track.ip = trackip
    track.port = trackport
    assert track.ip and track.port
    gevent.spawn(track_report)
    if masterip and masterport:
        gevent.spawn(sync_with_master, masterip, masterport)
        master = False
    else:
        master = True

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    address = (listenip, listenport)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind(address)
    s.listen(5)

    while True:
        client_sock, address = s.accept()
        gevent.spawn(handle_client, client_sock)