Beispiel #1
0
    def connect_to_sm(self, ip, port, server_conn=False):
        """ connect to specific IP:PORT
        """
        logger.debug('Try to connect to Site Manager and establish DTN connection')

        conn_send = DTN._tcp_connect(ip, port)

        if conn_send is not None:

            # send information
            conn_send.send('%s %s\n' % (self.sh, self.target))

            data = conn_send.recv(1024) 
            port_recv = 0

            if data == EXIST_ERR:
                logger.debug('DTN connection already exists between these two Site Managers')
                return False

            sh = data.split()[0]
            port_recv = int(data.split()[1])
            tar = ' '.join(data.split()[2:])
            logger.debug('SH: %s, Port: %d, Target: %s' % (sh, port_recv, tar))

            # check if site manager is connected
            if self.dtn.has_key(sh) and self.dtn[sh] is not None:
                conn_send.send(EXIST_ERR)
                logger.debug('DTN connection already exists between these two Site Managers')
                return False
            else:
                conn_send.send(SUCCESS_INFO)

            # connect to ip using another port
            conn_recv = DTN._tcp_connect(ip, port_recv)

            if conn_recv is not None:

                # Ready
                dtn_conn = DTNConnection(conn_send, conn_recv, self.sh, sh, tar, self, server_conn=server_conn)

                self.dtn[sh] = dtn_conn
                if not self.last_hash.has_key(sh):
                    self.last_hash[sh] = ''

                logger.info('New DTN connection established')
                dtn_conn.start()

                return True

        return False
Beispiel #2
0
    def connect_to_sm(self, ip, port, server_conn=False):
        """ connect to specific IP:PORT
        """
        logger.debug(
            'Try to connect to Site Manager and establish DTN connection')

        conn_send = DTN._tcp_connect(ip, port)

        if conn_send is not None:

            # send information
            conn_send.send('%s %s\n' % (self.sh, self.target))

            data = conn_send.recv(1024)
            port_recv = 0

            if data == EXIST_ERR:
                logger.debug(
                    'DTN connection already exists between these two Site Managers'
                )
                return False

            sh = data.split()[0]
            port_recv = int(data.split()[1])
            tar = ' '.join(data.split()[2:])
            logger.debug('SH: %s, Port: %d, Target: %s' % (sh, port_recv, tar))

            # check if site manager is connected
            if self.dtn.has_key(sh) and self.dtn[sh] is not None:
                conn_send.send(EXIST_ERR)
                logger.debug(
                    'DTN connection already exists between these two Site Managers'
                )
                return False
            else:
                conn_send.send(SUCCESS_INFO)

            # connect to ip using another port
            conn_recv = DTN._tcp_connect(ip, port_recv)

            if conn_recv is not None:

                # Ready
                dtn_conn = DTNConnection(conn_send,
                                         conn_recv,
                                         self.sh,
                                         sh,
                                         tar,
                                         self,
                                         server_conn=server_conn)

                self.dtn[sh] = dtn_conn
                if not self.last_hash.has_key(sh):
                    self.last_hash[sh] = ''

                logger.info('New DTN connection established')
                dtn_conn.start()

                return True

        return False
Beispiel #3
0
    def handle_dtn_listen(self, s):

        logger.info('DTN connection request')
        conn_recv, remote = s.accept()

        data = conn_recv.recv(1024)
        sh = data.split()[0]
        tar = ' '.join(data.split()[1:])
        logger.debug('SH: %s, Target: %s' % (sh, tar))

        if self.dtn.has_key(sh):
            logger.debug(self.dtn[sh])

        if self.dtn.has_key(sh) and self.dtn[sh] is not None:
            conn_recv.send(EXIST_ERR)
            logger.debug('DTN connection already exists between these two Site Managers')
            return False

        # Generate a random port for listening
        random_n = 1
        listener = None
        while True:
            try:
                listener = DTN._tcp_listen(self.my_ip, self.dtn_port+random_n)
            except:
                random_n += 1
                continue

            break
        conn_recv.send('%s %d %s' % (self.sh, self.dtn_port+random_n, self.target))
        data = conn_recv.recv(1024)

        if data == EXIST_ERR:
            logger.debug('DTN connection already exists between these two Site Managers')
            return False

        # wait for connection
        listener.settimeout(5)
        try:
            conn_send, remote = listener.accept()
        except socket.timeout:
            logger.debug('timeout')
            return False

        # remote[0] is IP
        # remote[1] is PORT
        #conn_send = DTN._tcp_connect(remote[0], port)

        if conn_send is not None:
            # send my SH info
            #conn_send.send('%s %s' % (self.sh, self.target))

            #data = conn_send.recv(1024) 
            #if data == EXIST_ERR:
                #logger.debug('DTN connection already exists between these two Site Managers')
                #return 
            #else:
                #logger.debug('Good')

            # Ready
            dtn_conn = DTNConnection(conn_send, conn_recv, self.sh, sh, tar, self)
            self.dtn[sh] = dtn_conn
            if not self.last_hash.has_key(sh):
                self.last_hash[sh] = ''
            #if sh == SERVER_SH_INFO:
                #self.server_connected = True
            logger.info('New DTN connection established')
            dtn_conn.start()
            return True

        return False
Beispiel #4
0
    def handle_dtn_listen(self, s):

        logger.info('DTN connection request')
        conn_recv, remote = s.accept()

        data = conn_recv.recv(1024)
        sh = data.split()[0]
        tar = ' '.join(data.split()[1:])
        logger.debug('SH: %s, Target: %s' % (sh, tar))

        if self.dtn.has_key(sh):
            logger.debug(self.dtn[sh])

        if self.dtn.has_key(sh) and self.dtn[sh] is not None:
            conn_recv.send(EXIST_ERR)
            logger.debug(
                'DTN connection already exists between these two Site Managers'
            )
            return False

        # Generate a random port for listening
        random_n = 1
        listener = None
        while True:
            try:
                listener = DTN._tcp_listen(self.my_ip,
                                           self.dtn_port + random_n)
            except:
                random_n += 1
                continue

            break
        conn_recv.send('%s %d %s' %
                       (self.sh, self.dtn_port + random_n, self.target))
        data = conn_recv.recv(1024)

        if data == EXIST_ERR:
            logger.debug(
                'DTN connection already exists between these two Site Managers'
            )
            return False

        # wait for connection
        listener.settimeout(5)
        try:
            conn_send, remote = listener.accept()
        except socket.timeout:
            logger.debug('timeout')
            return False

        # remote[0] is IP
        # remote[1] is PORT
        #conn_send = DTN._tcp_connect(remote[0], port)

        if conn_send is not None:
            # send my SH info
            #conn_send.send('%s %s' % (self.sh, self.target))

            #data = conn_send.recv(1024)
            #if data == EXIST_ERR:
            #logger.debug('DTN connection already exists between these two Site Managers')
            #return
            #else:
            #logger.debug('Good')

            # Ready
            dtn_conn = DTNConnection(conn_send, conn_recv, self.sh, sh, tar,
                                     self)
            self.dtn[sh] = dtn_conn
            if not self.last_hash.has_key(sh):
                self.last_hash[sh] = ''
            #if sh == SERVER_SH_INFO:
            #self.server_connected = True
            logger.info('New DTN connection established')
            dtn_conn.start()
            return True

        return False