예제 #1
0
    def update(self, message):
        keys = {'type': None, 'txid': None, 'db': None}
        valid, keys = Receiver.buildObj(message, keys)
        self._obj = keys
        if not valid:
            self._obj = {'message': 'neplatny syntax zpravy UPDATE.'}
            return False
        if self._isNode:
            if not isinstance(keys['db'], dict):
                return False
            for key in keys['db']:
                ip = port = None
                try:
                    ip, port = key.split(',', 2)
                except:
                    self._obj = {
                        'message':
                        'neplatny syntax zpravy UPDATE (neplatny zaznam DB RECORD).'
                    }
                    return False

                if not valid_ipv4(ip) or not valid_port(port):
                    self._obj = {
                        'message':
                        'neplatny syntax zpravy UPDATE (neplatny zaznam DB RECORD).'
                    }
                    return False

                if not isinstance(keys['db'][key], dict):
                    self._obj = {
                        'message':
                        'neplatny syntax zpravy UPDATE (neplatny zaznam DB RECORD).'
                    }
                    return False

                peers = list()
                for _, peer in keys['db'][key].items():
                    peerKeys = {'username': None, 'ipv4': None, 'port': None}
                    valid, peerKeys = Receiver.buildObj(peer, peerKeys)
                    if valid:
                        peers.append(
                            Peer(peerKeys['username'], peerKeys['ipv4'],
                                 str(peerKeys['port'])))
                    else:
                        self._obj = {
                            'message':
                            'neplatny syntax zpravy MESSAGE (neplatny zaznam PEER RECORD uvnitr zaznamu DB RECORD).'
                        }
                        return False
                with self._lock:
                    self._obj['db'][key] = Db(ip, port)
                    self._obj['db'][key].update(peers)
        return True
예제 #2
0
 def _stdinPeer( self, username ):
     #signal.signal( signal.SIGINT, self._kill)
     #signal.signal( signal.SIGTERM, self._kill)
     #sys.stdin = open(0)
     while self._running:
         line = None
         if select.select( [ sys.stdin ], [], [], 0.5 )[0]:
             line = sys.stdin.readline().strip()
         else:
             continue
         if line.startswith( '\\' ):
             if line.startswith( '\\w' ): # message \ w _ c _
                 idx1 = find_nth( line, ' ', 1 )
                 idx2 = find_nth( line, ' ', 2 )
                 if idx1 < 0 or idx2 < 0 or idx2+1 >= len( line ):
                     self._append( { 'type' : 'error', 'verbose' : 'Neplatny syntax prikazu. Pouzijte "\\w username message"' } )
                 else:
                     to = line[idx1+1:idx2]
                     message = line[idx2+1:]
                     self._append( { 'type' : 'message', 'from' : username, 'to' : to, 'message' : message } )
             elif line.startswith( '\\r' ): # reconnect
                 idx1 = find_nth( line, ' ', 1 )
                 idx2 = find_nth( line, ' ', 2 )
                 if idx1 < 0 or idx2 < 0 or idx2+1 >= len( line ):
                     self._append( { 'type' : 'error', 'verbose' : 'Neplatny syntax prikazu. Pouzijte "\\r ipv4 port"' } )
                 else:
                     ipv4 = line[idx1+1:idx2]
                     port = line[idx2+1:]
                     if not valid_ipv4( ipv4 ) or not valid_port( port ):
                         self._append( { 'type' : 'error', 'verbose' : 'Neplatna ipv4 adresa nebo port."' } )
                     else:
                         self._append( { 'type' : 'reconnect', 'ipv4' : ipv4, 'port' : int( port ) } )
             elif line == '\\l': # peers
                 self._append( { 'type' : 'getlist' } )
                 self._append( { 'type' : 'peers' } )
             elif line ==  '\\u': # getlist
                 self._append( { 'type' : 'getlist' } )
             elif line.startswith( '\\h' ): # help
                 self._append( { 'type' : 'print', 'verbose' : '[\\l] Vypise seznam znamych uzivatelu\n[\\u] Aktualizuje seznam uzivatelu\n[\\r ipv4 port] pripoji se na zadany uzel\n[\\w username message] odesle zpravu uzivateli\n[\\exit] Ukonci aplikaci' } )
             elif line == '\\exit':
                 self._append( { 'type' : 'exit' } )
                 break
             else:
                 self._append( { 'type' : 'error', 'verbose' : 'Neznamy prikaz, zadejte "\\h" pro zobrazeni napovedy' } )
예제 #3
0
 def buildObj(message, keys):
     for key, item in message.items():
         valid = True
         if key == 'txid':
             valid = isinstance(item, int) and item >= 0
         elif key == 'port':
             valid = isinstance(item, int) and valid_port(item)
         elif key == 'username':
             valid = bool(isinstance(item, str) and item)
         elif key == 'ipv4':
             valid = isinstance(item, str) and valid_ipv4(item)
         elif key == 'verbose':
             valid = bool(isinstance(item, str) and item)
         elif key == 'from':
             valid = bool(isinstance(item, str) and item)
         elif key == 'to':
             valid = bool(isinstance(item, str) and item)
         if valid and (key in keys) and keys[key] is None:
             keys[key] = item
         else:
             return (False, dict())
     return (True, keys)
예제 #4
0
 def _stdinNode( self ):
     #signal.signal( signal.SIGINT, self._kill)
     #signal.signal( signal.SIGTERM, self._kill)
     #sys.stdin = open(0)
     while self._running:
         line = None
         if select.select( [ sys.stdin ], [], [], 0.5 )[0]:
             line = sys.stdin.readline().strip()
         else:
             continue
         if line.startswith( '\\' ):
             if line.startswith( '\\c' ): # connect
                 idx1 = find_nth( line, ' ', 1 )
                 idx2 = find_nth( line, ' ', 2 )
                 if idx1 < 0 or idx2 < 1 or idx2+1 >= len( line ):
                     self._append( { 'type' : 'error', 'verbose' : 'Neplatny syntax prikazu. Pouzijte "\\c ipv4 port"' } )
                 else:
                     ipv4 = line[idx1+1:idx2]
                     port = line[idx2+1:]
                     if not valid_ipv4( ipv4 ) or not valid_port( port ):
                         self._append( { 'type' : 'error', 'verbose' : 'Neplatna ipv4 adresa nebo port.' } )
                     else:
                         self._append( { 'type' : 'connect', 'ipv4' : ipv4, 'port' : int( port ) } )
             elif line == '\\s': # sync
                 self._append( { 'type' : 'sync' } )
             elif line == '\\l': # database
                 self._append( { 'type' : 'database' } )
             elif line == '\\n': # neighbors
                 self._append( { 'type' : 'neighbors' } )
             elif line == '\\h': # help
                 self._append( { 'type' : 'print', 'verbose' : '[\\c ipv4 port] Navaze spojeni se zadanym uzlem\n[\\s] Vynuti synchronizaci s ostatnimy uzly\n[\\l] Vypise aktualni databazi peeru a jejich uzlu\n[\\n] Vypise databazi znamych uzlu\n[\\d] Odpodi se od ostatnich uzlu\n[\\exit] Ukonci aplikace' } )
             elif line == '\\d': # disconnect
                 self._append( { 'type' : 'disconnect' } )
             elif line == '\\exit':
                 self._append( { 'type' : 'exit' } )
                 break
             else:
                 self._append( { 'type' : 'error', 'verbose' : 'Neznamy prikaz, zadejte "\\h" pro zobrazeni napovedy' } )
예제 #5
0
파일: pds18-rpc.py 프로젝트: tehryn/PDS
                'Prikaz message vyzaduje parametry --from, --to a --message\n')
            exit(invalid_arguments)
        with lock:
            with open(filename, 'a') as file:
                file.write('{ "type" : "message", "from": ' +
                           dumps(settings['from'][0]) + ', "to": ' +
                           dumps(settings['to'][0]) + ', "message": ' +
                           dumps(settings['message'][0]) + ' }\n')

    elif settings['cmd'][0] == 'reconnect':
        if 'ipv4' not in settings:
            sys.stderr.write(
                'Prikaz reconnect vyzaduje parametry --ipv4 --port\n')
            exit(invalid_arguments)
        elif not (valid_ipv4(settings['ipv4'][0])
                  and valid_port(settings['port'][0])):
            sys.stderr.write('Neplatna ip adresa nebo port.\n')
            exit(invalid_arguments)
        with lock:
            with open(filename, 'a') as file:
                file.write('{ "type" : "reconnect", "ipv4": "' +
                           settings['ipv4'][0] + '", "port": ' +
                           settings['port'][0] + ' }\n')

    elif settings['cmd'][0] == 'getlist':
        with lock:
            with open(filename, 'a') as file:
                file.write('{ "type" : "getlist" }\n')

    elif settings['cmd'][0] == 'peers':
        with lock: