Пример #1
0
    def __init__(self,
                 addr=args.addr,
                 port=args.port,
                 givenbootstraplist=args.bootstrap,
                 debugoption=args.debug,
                 objectname=args.objectname,
                 logger=args.logger,
                 writetodisk=args.writetodisk):
        self.addr = addr if addr else findOwnIP()
        self.port = port
        self.debug = debugoption
        self.durable = writetodisk
        self.isnameserver = args.domain != ''
        self.domain = args.domain
        self.useroute53 = args.route53
        if objectname == '':
            parser.print_help()
            self._graceexit(1)
        self.objectname = objectname
        # initialize receive queue
        self.receivedmessages_semaphore = Semaphore(0)
        self.receivedmessages = []
        # lock to synchronize message handling
        self.lock = Lock()

        # create server socket and bind to a port
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        self.socket.setblocking(0)
        if self.port:
            try:
                self.socket.bind((self.addr, self.port))
            except socket.error as e:
                print "Cannot bind to port %d" % self.port
                print "Socket Error: ", e
                self._graceexit(1)
        else:
            for i in range(50):
                self.port = random.randint(14000, 15000)
                try:
                    self.socket.bind((self.addr, self.port))
                    break
                except socket.error as e:
                    print "Socket Error: ", e
        self.socket.listen(10)
        self.connectionpool = ConnectionPool()

        try:
            self.connectionpool.epoll = select.epoll()
        except AttributeError as e:
            # the os doesn't support epoll
            self.connectionpool.epoll = None

        # set the logger
        if logger:
            LOGGERNODE = logger
        else:
            LOGGERNODE = None

        # Initialize replicas
        # Keeps {peer:outofreachcount}
        self.replicas = {}
        # Nameserver state
        if self.isnameserver:
            self.type = NODE_NAMESERVER
            try:
                self.nameserver = Nameserver(self.addr, self.domain,
                                             self.useroute53, self.replicas,
                                             self.debug)
            except Exception as e:
                print "Error:", e
                print "Could not start Replica as a Nameserver, exiting."
                self._graceexit(1)
        else:
            self.type = NODE_REPLICA

        self.alive = True
        self.me = Peer(self.addr, self.port, self.type)
        # set id
        self.id = '%s:%d' % (self.addr, self.port)
        # add self to connectionpool
        self.connectionpool.add_connection_to_self(
            self.me,
            SelfConnection(self.receivedmessages,
                           self.receivedmessages_semaphore))
        self.logger = Logger("%s-%s" % (node_names[self.type], self.id),
                             lognode=LOGGERNODE)
        if self.isnameserver:
            self.nameserver.add_logger(self.logger)
        print "%s-%s connected." % (node_names[self.type], self.id)

        # Keeps the liveness of the nodes
        self.nodeliveness = {}
        self.bootstrapset = set()
        # connect to the bootstrap node
        if givenbootstraplist:
            self.discoverbootstrap(givenbootstraplist)
            self.connecttobootstrap()

        self.stateuptodate = False
Пример #2
0
    def __init__(self,
                 nodetype,
                 addr=args.addr,
                 port=args.port,
                 givenbootstraplist=args.bootstrap,
                 debugoption=args.debug,
                 objectname=args.objectname,
                 instantiateobj=False,
                 configpath=args.configpath,
                 logger=args.logger,
                 writetodisk=args.writetodisk):
        self.addr = addr if addr else findOwnIP()
        self.port = port
        self.type = nodetype
        self.debug = debugoption
        self.durable = writetodisk
        if instantiateobj:
            if objectname == '':
                parser.print_help()
                self._graceexit(1)
            self.objectname = objectname
        # initialize receive queue
        self.receivedmessages_semaphore = Semaphore(0)
        self.receivedmessages = []
        # lock to synchronize message handling
        self.lock = Lock()
        # create server socket and bind to a port
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        self.socket.setblocking(0)
        if self.port:
            try:
                self.socket.bind((self.addr, self.port))
            except socket.error as e:
                print "Cannot bind to port %d" % self.port
                print "Socket Error: ", e
                self._graceexit(1)
        else:
            for i in range(50):
                self.port = random.randint(14000, 15000)
                try:
                    self.socket.bind((self.addr, self.port))
                    break
                except socket.error as e:
                    print "Socket Error: ", e
                    pass
        self.socket.listen(10)
        self.connectionpool = ConnectionPool()
        try:
            self.connectionpool.epoll = select.epoll()
        except AttributeError as e:
            # the os doesn't support epoll
            self.connectionpool.epoll = None
        self.alive = True
        self.me = Peer(self.addr, self.port, self.type)
        # set id
        self.id = '%s:%d' % (self.addr, self.port)

        # set path for additional configuration data
        self.configpath = configpath
        # set the logger
        try:
            LOGGERNODE = load_configdict(self.configpath)['LOGGERNODE']
        except KeyError as e:
            if logger:
                LOGGERNODE = logger
            else:
                LOGGERNODE = None
        self.logger = Logger("%s-%s" % (node_names[self.type], self.id),
                             lognode=LOGGERNODE)
        print "%s-%s connected." % (node_names[self.type], self.id)
        # Initialize groups
        # Keeps {peer:outofreachcount}
        self.replicas = {}
        self.acceptors = {}
        self.nameservers = {}
        self.groups = {
            NODE_REPLICA: self.replicas,
            NODE_ACCEPTOR: self.acceptors,
            NODE_NAMESERVER: self.nameservers
        }
        self.groups[self.me.type][self.me] = 0

        # Keeps the liveness of the nodes
        self.nodeliveness = {}
        # connect to the bootstrap node
        if givenbootstraplist:
            self.bootstraplist = []
            self.discoverbootstrap(givenbootstraplist)
            self.connecttobootstrap()
        if self.type == NODE_REPLICA or self.type == NODE_NAMESERVER:
            self.stateuptodate = False