예제 #1
0
 def upnp(self):
     upnp = UPNP()
     
     dev = upnp.add_device(self._device)
     
     for name, service in self._services.items():
         dev.add_service(service)
         self._service_abbrev[service.service_id()] = name
     
     self.upnp_ready()
     
     return upnp
예제 #2
0
 def get_ext_ip(self):
     if self.debug:
         return 'ip'
     u = UPNP()
     ip = u.get_external_ip()
     if not ip:
         try:
             ip = urlopen(self.config['IP_CHECKER']).read().strip()
         except:
             self.logger.error('Cannot obtain external ip')
             raise Exception("Cannot obtain external ip")
     self.logger.debug('Obtained external IP - ' + ip)
     return ip
예제 #3
0
class User:
    warship = None
    destroyer = None
    submarine = None
    aes = None
    rsa = None
    upnp = UPNP()

    def __init__(self, user_id, name):
        self.user_id = user_id
        self.name = name
        self.warship = Warship()
        self.destroyer = Destroyer()
        self.submarine = Submarine()
        self.phase = PHASE[0]
        self.is_host = False
        self.port = 5000

    def get_ship_by_ship_id(self, ship_id):
        if ship_id == FIELD_WARSHIP:
            return self.warship
        elif ship_id == FIELD_DESTROYER:
            return self.destroyer
        else:
            return self.submarine

    def is_alive(self):
        if self.warship.hp > 0:
            return True
        if self.destroyer.hp > 0:
            return True
        if self.submarine.hp > 0:
            return True
        return False
예제 #4
0
def add_from_dir(directory, upnp = None):
    import os
    from device import Device
    from service import Service
    
    if upnp is None:
        from upnp import UPNP
        upnp = UPNP()

    path = os.path.abspath(directory)
    device_files = os.listdir(path)
    
    
    for device in device_files:
        device_path = os.path.join(path, device)
        
        if os.path.isdir(device_path):
            device_name = device
            
            device_file = "%s.xml" % device_name
            device_file_path = os.path.join(path, device_file)
            
            # check for service files
            service_files = os.listdir(device_path)
            
            services = dict()
            
            for service_file in service_files:
                service_file_path = os.path.join(device_path, service_file)

                if os.path.isfile(service_file_path) and service_file.endswith(".xml"):
                    services[service_file] = service_file_path
            
            if len(services) > 0:
                new_device = Device("/" + device_file).from_file(device_file_path)
                
                for location, path in services.items():
                    new_device.add_service(Service("/" + location).from_file(path))
                
                upnp.add_device(new_device)
                
    return upnp
예제 #5
0
    def __init__(self, debug=False):
        '''
        buffering_units -> This is a number of video stream units for buffering
        all peers list is saved in DB
        '''
        self.config = ConfigLoader()

        self.logger = logging.getLogger('tamchy')
        self.logger.setLevel(self.config['DEBUG'])
        f = logging.FileHandler(self.config['LOG_FILE'])
        f.setLevel(self.config['DEBUG'])
        formatter = logging.Formatter(
            '%(asctime)s -- %(name)s ( %(filename)s : %(lineno)d) -- %(message)s'
        )
        f.setFormatter(formatter)
        self.logger.addHandler(f)

        #self.peer_id = messages.generate_peer_id()
        self.work = True
        # content_id : Stream Container
        self._streams = {}
        # this dict will hold port:Server instance for this port
        self.ports = {}
        self.debug = debug
        self.logger.info('Client started')

        # getting our external ip
        self.ip = self.get_ext_ip()
        self.http = HTTPEngine(self)
        self.PStorage = PeerStorage()
        #self.Reactor = Reactor(self.PStorage)
        if not debug:
            self.http.start_http_server()

            u = UPNP()
            port = self.config['INCOMING_PORT']
            # we will try to map same external port to internal port
            u.add_port_mapping(port, port)

            self.PStorage.start_serving()