def __init__(self):
		#Constructor for this object
		#Initialize various parameters
		#and open listening sockets


		#set the nodes IP-Address
		Resolve.resolve_ip_address(Config.NODE_INTF)
		#stores ip-addr => entity-reference
		self.nodes={} 

		#store ip of this machine
		#this variable is useful in the 
		#download module
		self.ip_address = Config.NODE_IP_ADDRESS
		#create the UDP Socket
		self.soc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		self.soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
		self.soc.bind((Config.ANY_INTF, Config.UDP_LISTEN_PORT))
		
		#path info updated here dynamically
		self.path_cache={}
		self.query_cache={}
		
		#object to store path-query replies
		self.path_q_reply={}

		print 'Host IP:',Config.NODE_IP_ADDRESS
		print 'Waiting for other hosts...'
		#first get atleast two hosts
		self.getFirstTwoHosts()

		#there is no need of bcast bit from here.
		self.soc.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 0)

		#there is no need of timeout on this socket.
		#should wait indefinitely
		self.soc.settimeout(None)

		#spawn a new listening thread
		print 'Spawned listening thread...'
		thread.start_new_thread(self.listen,())

		print 'First two hosts:'
		print "\n".join(str(x) for x in self.nodes)
		print 'Neighbours:'
		for i in self.nodes:
			print 'via',i,":"
			for j in self.nodes[i].neighbours:
				print j,
			print
Exemple #2
0
    def solver(self,puzzle,algorithm):
        """
        This method uses the algorithm specified to solve the game
        """
        if(algorithm =="recursive"):
            board = sudokustr.SudokuStr(puzzle)
            if(board.verify_string_to_sudoku()):
                self.original = board.get_str()
                puzzle = board.get_str()
                recursivesk = recursive.Recursive(puzzle)
                self.solution=recursivesk.solve_one_sudoku()
                self.solve_time = recursivesk.get_time()
                print(self.human_string(self.solution))
                print self.solve_time

        elif (algorithm =="peter"):
            board = sudokustr.SudokuStr(puzzle)
            if(board.verify_string_to_sudoku()):
                self.original = board.get_str()
                puzzle = board.get_str()
                petersk = peter_algorithm.Peter_algorithm()
                self.solution = petersk.get_game_solution (puzzle)
                self.solve_time = petersk.get_time()
                print(self.human_string(self.solution))
                print self.solve_time


        elif (algorithm =="bactracking"):
            """
            This implements the backtrack algorithm in order to solve the game
            """
            board = sudokustr.SudokuStr(puzzle)
            if(board.verify_string_to_sudoku()):
                self.original = board.get_str()
                puzzle = board.get_str()

                bactrack = Resolve(puzzle)

                sudoku_matrix = bactrack.convert_str_to_matrix(puzzle, 9, 9)
                self.execute = bactrack.resolve(sudoku_matrix)
                self.solution = bactrack.get_solve_game()


                self.solve_time = bactrack.get_time()
                print(self.human_string(self.solution))
                print self.solve_time
        else:
            print ("Other algorithm")
Exemple #3
0
 def run(self):  #执行监听
     capture = pcapy.open_live(self.__device, 65536, 1, 0)  #将网卡设置为混杂模式
     if len(self.__filter) != 0:  #有过滤条件
         try:  #设置过滤规则,并检测输入的过滤规则是否有误
             capture.setfilter(self.__filter)
         except:
             self.__showErrorSignal.emit()  #向主线程发送过滤规则有误信号
             return
     while self.__runTag:  #循环直至runTag为0
         (head, data) = capture.next()  #捕获数据包
         if head != None:
             resolveObj = Resolve()
             res = resolveObj.resolve(head, data)  #交由解析对象解析数据包
             if res != None:
                 res["originalHex"] = data.hex()
                 self.__resList.append(res)
                 self.__getDataSignal.emit()  #通知主界面此时可以获取数据
Exemple #4
0
def app_info_packages(fn):
    """
    given the filename of a package, return which packages (and their sizes)
    still need to be downloaded, in order to install the package.  That is,
    the package itself and it's dependencies.
    Returns a list of tuples (pkg_name, pkg_version, size,
    fetched? True or False).
    """
    from resolve import Resolve

    index = get_index()
    r = Resolve(index)
    res = []
    for fn2 in r.solve([fn2spec(fn)]):
        info = index[fn2]
        res.append((info['name'], info['version'], info['size'],
                    install.is_fetched(config.pkgs_dir, fn2[:-8])))
    return res
Exemple #5
0
    def install(self, arg, mode='recur', force=False, forceall=False):
        req = req_from_anything(arg)
        # resolve the list of eggs that need to be installed
        self._connect()
        resolver = Resolve(self.remote, self.verbose)
        eggs = resolver.install_sequence(req, mode)
        if eggs is None:
             raise EnpkgError("No egg found for requirement '%s'." % req)

        if not forceall:
            # remove already installed eggs from egg list
            rm = lambda eggs: [e for e in eggs if self.find(e) is None]
            if force:
                eggs = rm(eggs[:-1]) + [eggs[-1]]
            else:
                eggs = rm(eggs)

        # fetch eggs
        for egg in eggs:
            self.fetch(egg, force or forceall)

        if not self.hook:
            # remove packages with the same name (from first egg collection
            # only, in reverse install order)
            for egg in reversed(eggs):
                try:
                    self.remove(Req(name_egg(egg)))
                except EnpkgError:
                    pass

        # install eggs
        for egg in eggs:
            extra_info = {}
            repo = self.remote.where_from(egg)
            if repo:
                extra_info['repo_dispname'] = repo.info()['dispname']
            self.ec.install(egg, self.local_dir, extra_info)
        return len(eggs)
Exemple #6
0
def remove_features_actions(prefix, index, features):
    linked = install.linked(prefix)
    r = Resolve(index)

    actions = defaultdict(list)
    actions[PREFIX] = prefix
    _linked = [d + '.tar.bz2' for d in linked]
    to_link = []
    for dist in sorted(linked):
        fn = dist + '.tar.bz2'
        if fn not in index:
            continue
        if r.track_features(fn).intersection(features):
            actions[UNLINK].append(dist)
        if r.features(fn).intersection(features):
            actions[UNLINK].append(dist)
            subst = r.find_substitute(_linked, features, fn)
            if subst:
                to_link.append(subst[:-8])

    if to_link:
        actions.update(ensure_linked_actions(to_link, prefix))
    return actions
Exemple #7
0
def install_actions(prefix, index, specs, force=False, only_names=None):
    r = Resolve(index)
    linked = install.linked(prefix)

    if is_root_prefix(prefix):
        specs.append('conda')
    add_defaults_to_specs(r, linked, specs)

    must_have = {}
    for fn in r.solve(specs, [d + '.tar.bz2' for d in linked]):
        dist = fn[:-8]
        name = name_dist(dist)
        if only_names and name not in only_names:
            continue
        must_have[name] = dist

    if is_root_prefix(prefix):
        if not force:
            # ensure conda is in root environment
            assert 'conda' in must_have
    else:
        # discard conda from other environments
        if 'conda' in must_have:
            del must_have['conda']

    smh = sorted(must_have.values())
    if force:
        actions = force_linked_actions(smh, index, prefix)
    else:
        actions = ensure_linked_actions(smh, prefix)

    for dist in sorted(linked):
        name = name_dist(dist)
        if name in must_have and dist != must_have[name]:
            actions[UNLINK].append(dist)

    return actions
 def setUp(self):
     self.max_col = 9
     self.max_row = 9
     self.col = 0
     self.row = 0
     self.new_position = Position(self.max_row,self.max_col)
     
     self.sudoku_str = "273481960000075030048090100059300000367510809124968700001829576685734000092156384"
     self.sudoku_resolved_to_compare = "273481965916275438548693127859347612367512849124968753431829576685734291792156384"
     list_for_matrix = [0] * self.max_col
     self.zero_matrix = [list_for_matrix] * self.max_row
     self.sudoku_matrix = [[2, 7, 3, 4, 8, 1, 9, 6, 0], [0, 0, 0, 0, 7, 5, 0, 3, 0], [0, 4, 8, 0, 9, 0, 1, 0, 0], 
                           [0, 5, 9, 3, 0, 0, 0, 0, 0], [3, 6, 7, 5, 1, 0, 8, 0, 9], [1, 2, 4, 9, 6, 8, 7, 0, 0], 
                           [0, 0, 1, 8, 2, 9, 5, 7, 6], [6, 8, 5, 7, 3, 4, 0, 0, 0], [0, 9, 2, 1, 5, 6, 3, 8, 4]]
     self.new_resolve = Resolve(self.sudoku_str)
Exemple #9
0
    def install_actions(self, arg, mode='recur', force=False, forceall=False):
        """
        Create a list of actions which are required for installing, which
        includes updating, a package (without actually doing anything).

        The first argument may be any of:
          * the KVS key, i.e. the egg filename
          * a requirement object (enstaller.resolve.Req)
          * the requirement as a string
        """
        req = req_from_anything(arg)
        # resolve the list of eggs that need to be installed
        self._connect()
        eggs = Resolve(self.remote, self.verbose).install_sequence(req, mode)
        if eggs is None:
            raise EnpkgError("No egg found for requirement '%s'." % req)

        if not forceall:
            # remove already installed eggs from egg list
            rm = lambda eggs: [e for e in eggs if self.find(e) is None]
            if force:
                eggs = rm(eggs[:-1]) + [eggs[-1]]
            else:
                eggs = rm(eggs)

        res = []
        for egg in eggs:
            res.append(('fetch_%d' % bool(forceall or force), egg))

        if not self.hook:
            # remove packages with the same name (from first egg collection
            # only, in reverse install order)
            for egg in reversed(eggs):
                name = split_eggname(egg)[0].lower()
                index = dict(self.ec.collections[0].query(name=name))
                assert len(index) < 2
                if len(index) == 1:
                    res.append(('remove', index.keys()[0]))
        for egg in eggs:
            res.append(('install', egg))
        return res
class UnitTestBacktrak(unittest.TestCase):
    
    def setUp(self):
        self.max_col = 9
        self.max_row = 9
        self.col = 0
        self.row = 0
        self.new_position = Position(self.max_row,self.max_col)
        
        self.sudoku_str = "273481960000075030048090100059300000367510809124968700001829576685734000092156384"
        self.sudoku_resolved_to_compare = "273481965916275438548693127859347612367512849124968753431829576685734291792156384"
        list_for_matrix = [0] * self.max_col
        self.zero_matrix = [list_for_matrix] * self.max_row
        self.sudoku_matrix = [[2, 7, 3, 4, 8, 1, 9, 6, 0], [0, 0, 0, 0, 7, 5, 0, 3, 0], [0, 4, 8, 0, 9, 0, 1, 0, 0], 
                              [0, 5, 9, 3, 0, 0, 0, 0, 0], [3, 6, 7, 5, 1, 0, 8, 0, 9], [1, 2, 4, 9, 6, 8, 7, 0, 0], 
                              [0, 0, 1, 8, 2, 9, 5, 7, 6], [6, 8, 5, 7, 3, 4, 0, 0, 0], [0, 9, 2, 1, 5, 6, 3, 8, 4]]
        self.new_resolve = Resolve(self.sudoku_str)
    
    def test_verify_it_can_set_row(self):
        self.new_position.set_row(4)
        self.assertEqual(4,self.new_position.get_row())
        
    
    def test_verify_it_can_set_row_equal_to_0_if_it_is_minor_to_0(self):
        self.new_position.set_row(-1)
        self.assertEqual(0,self.new_position.get_row())
    
    def test_verify_it_cat_set_row_equal_to_minus_1_if_it_is_mayor_to_max_row(self):
        self.new_position.set_row(11)
        self.assertEqual(-1,self.new_position.get_row())
    
    def test_verify_it_can_set_col(self):
        self.new_position.set_col(5)
        self.assertEqual(5,self.new_position.get_col())
    
    def test_verify_it_can_set_col_equal_to_0_if_it_is_minor_to_0(self):
        self.new_position.set_col(-2)
        self.assertEqual(0,self.new_position.get_col())
    
    def test_verify_it_cat_set_col_equal_to_minus_1_if_it_is_mayor_to_max_col(self):
        self.new_position.set_col(20)
        self.assertEqual(-1,self.new_position.get_col())
    
    def test_verify_it_can_get_row(self):
        self.new_position.set_row(8)
        self.assertEqual(8,self.new_position.get_row())
    
    def test_verify_it_can_get_col(self):
        self.new_position.set_col(4)
        self.assertEqual(4,self.new_position.get_col())
    
    def test_verify_it_is_end_of_the_matrix_it_should_return_minus_1(self):
        self.new_position.set_row(self.max_col)
        self.new_position.set_col(self.max_row)
        self.assertTrue(self.new_position.end_matrix())
    
    def test_verify_it_can_reset_to_zero_the_row_and_col(self):
        self.new_position.reset()
        self.assertEqual(0,self.new_position.get_col())
        self.assertEqual(0,self.new_position.get_row())
    
    def test_verify_it_can_move_to_next_vale_of_a_matrix(self):
        """
        if initial position is row = 0, col = 1
        the next position should be row = 0, col = 2
        """
        self.new_position.set_row(0)
        self.new_position.set_col(1)
        self.new_position.next_position()
        self.assertEqual(0,self.new_position.get_row())
        self.assertEqual(2,self.new_position.get_col())
    
    def test_verify_it_can_not_move__next_row_if_it_is_mayor_to_max_row(self):
        self.new_position.set_row(self.max_row + 1)
        self.new_position.next_position()
        self.assertEqual(-1,self.new_position.get_row())
        
    def test_verify_it_can_move_to_next_col_if_it_is_mayor_to_max_col(self):
        self.new_position.set_col(self.max_col + 1)
        self.new_position.next_position()
        self.assertEqual(0,self.new_position.get_col())
    
    def test_verify_it_can_not_move_to_next_value_if_it_is_end_of_the_matrix(self):
        self.new_position.set_row(self.max_row)
        self.new_position.set_col(self.max_col)
        self.new_position.next_position()
        self.assertTrue(self.new_position.end_matrix())
    
    def test_verify_it_can_get_a_position_of_a_matrix(self):
        self.new_position.set_row(3)
        self.new_position.set_col(3)
        self.assertEqual([3,3],self.new_position.get_position())
    
    def test_verify_it_could_resolve_a_sudoku(self):
        self.new_resolve.resolve(self.sudoku_matrix)
        self.assertEqual(self.sudoku_resolved_to_compare,self.new_resolve.get_solve_game())
    
    def test_verify_it_could_generate_a_zero_matrix(self):
        self.assertEqual(self.zero_matrix, self.new_resolve.generate_matrix(self.max_row, self.max_col))
        
    def test_verify_it_could_convert_an_string_to_a_matrix(self):
        self.assertEqual(self.sudoku_matrix, self.new_resolve.convert_str_to_matrix(self.sudoku_str, self.max_row, self.max_col))

    
#if __name__ == '__main__':
#    unittest.main()