Exemple #1
0
 def _check_new_client(self, string):
     if not self.speakThread[string['who']].certificate and self.is_check_new_acceptance['response']:
         log.info('certificate: {}, check response: {}, state: 1'.format(self.speakThread[string['who']].certificate,
                                                                         self.is_check_new_acceptance['response']))
         if string['string'] == self.is_check_new_acceptance['response']:
             self.speakThread[string['who']].certificate = True
             if self.sys_callback:
                 self.sys_callback('NEW CLIENT <{}> at {} ({})'.format(string['who'], time.time(), datetime.now()))
             else:
                 self.data_callback('NEW CLIENT <{}> at {} ({})'.format(string['who'], time.time(), datetime.now()))
             log.info('certificate: {}'.format(self.speakThread[string['who']].certificate))
         else:
             log.info('something Oops!')
             log.info('RECEIVE STRING: {receive}, '
                      'WAIT STRING: {wait}'.format(receive=string['string'],
                                                   wait=self.is_check_new_acceptance['response']))
             log.debug(self.speakThread)
             self.speakThread[string['who']].flag_run = False
             self.speakThread[string['who']].close()
             self.speakThread.pop(string['who'])
             log.debug(self.speakThread)
             return False
     elif not self.speakThread[string['who']].certificate:
         log.info('certificate: {}, check response: {}, state: 2'.format(self.speakThread[string['who']].certificate,
                                                                         self.is_check_new_acceptance['response']))
         self.speakThread[string['who']].certificate = True
         if self.sys_callback:
             self.sys_callback('NEW CLIENT <{}> at {} ({})'.format(string['who'], time.time(), datetime.now()))
         else:
             self.data_callback('NEW CLIENT <{}> at {} ({})'.format(string['who'], time.time(), datetime.now()))
         log.info('certificate: {}'.format(self.speakThread[string['who']].certificate))
     return True
Exemple #2
0
 def _routing_data(self, string):
     if self.data_callback:
         log.info('data_callback: {}'.format(self.data_callback))
         self.data_callback(string['string'])
         Thread4Server._send(whom=self.speakThread[string['who']].connection, what='READY TO NEXT')
         log.info(self.speakThread[string['who']].time)
     else:
         log.debug('from {} response {}'.format(string['who'], string['string']))
Exemple #3
0
 def _recover(self):
     back_up_data = db.select_from_db(self.db_cursor,
                                      name_table_in_db='data')
     log.info(back_up_data)
     for data in back_up_data:
         log.debug('PUT IN QUEUE {}'.format(data[3]))
         CommonQueue.CQ.put(data[3], block=False)
         db.delete_from_table_in_db(self.db_cursor, data[0])
         db.commit_changes(self.db_conn)
     back_up_data = db.select_from_db(self.db_cursor,
                                      name_table_in_db='data')
     log.info(back_up_data)
     self.system_callback({'START': 'RECOVER DONE'})
Exemple #4
0
    def accept_handler(self, value):
        self.sys_callback(str(value))
        log.info('Connecting to {}'.format(value['address']))
        self.speakThread[value['address']] = Thread4Speak(connection=value['connection'], address=value['address'])
        self.speakThread[value['address']].start()

        if self.is_check_new_acceptance['request']:
            log.info('send CHECK')
            Thread4Server._send(whom=self.speakThread[value['address']].connection,
                                what=self.is_check_new_acceptance['request'])
        else:
            self.speakThread[value['address']].certificate = True

        log.debug(self.speakThread)
        log.info('Number of active clients: {}'.format(len(self.speakThread)))
Exemple #5
0
    def cmd_handler(self, value):
        log.debug(value)
        self.acceptThread.flag_run = False
        self.timer.terminate()
        self.flag_run = False

        log.debug(self.speakThread)
        for key in list(self.speakThread.keys()):
            log.debug(self.speakThread[key])
            self.speakThread[key].flag_run = False
            self.speakThread[key].close()
            self.speakThread.pop(key)
        log.debug(self.speakThread)
        self.sock.close()  # .shutdown(socket.SHUT_RDWR)
Exemple #6
0
 def run(self):
     log.info('start for {}'.format(self.address))
     while self.flag_run:
         try:
             log.debug('start listen')
             data = self.connection.recv(10000)
             log.debug('stop listen {}'.format(data))
         except Exception as err:
             log.error('{}\nfrom {}'.format(err, self.address))
             if self.flag_run:
                 CommonQueue.CQ.put({'speak error': {'string': err, 'who': self.address}})
             self.flag_run = False
             self.close()
         else:
             if not data:
                 log.error('No data from {}'.format(self.address))
                 if self.flag_run:
                     CommonQueue.CQ.put({'speak error': {'string': 'No data', 'who': self.address}})
                 self.flag_run = False
                 self.close()
             else:
                 if self.flag_run:
                     # CommonQueue.CQ.put({'speak': {'string': data.decode('cp1251'),
                     #                               'who': self.address,
                     #                               'certificate': self.certificate,
                     #                               'status': self.status,
                     #                               'time': self.time,
                     #                               'ping': self.ping
                     #                               }
                     #                     })
                     self.time = time.time()
                     CommonQueue.CQ.put({'speak': {'string': data.decode('cp1251'),
                                                   'who': self.address,
                                                   'time': self.time,
                                                   'date': datetime.now()
                                                   }
                                         })
Exemple #7
0
 def run(self):
     while self._running:
         time.sleep(self.__time)
         if not self._pause:
             self.__handler()
     log.debug('TIMER STOP !!!')
Exemple #8
0
 def terminate(self):
     self._running = False
     log.debug('self._running: {}'.format(self._running))
Exemple #9
0
 def __del__(self):
     log.debug('DELETED {}'.format(self.address))
Exemple #10
0
    log.info(response)


def system_call(response):
    log.info(response)
    AssistantBot.CommonQueue.CQ.put(response, block=False)


if __name__ == '__main__':
    assistant_start = threading.Thread(target=AssistantBot.on)
    assistant_start.start()

    start_ip = 0
    while not start_ip:
        net_data = get_mac('ifconfig')
        log.debug(net_data)

        if 'VPN' in net_data.keys():
            start_ip = net_data['VPN']['VPN IP']

            serverThread = Thread4Server(ip=start_ip, _port=777,
                                         word_for_check_new_acceptance={'request': 'check', 'response': 'check ok'},
                                         data_cb=test_call, sys_cb=system_call,
                                         _run_timer=True)
            serverThread.start()
        else:
            time.sleep(3)
    log.debug('GO TO ANOTHER WHILE !!!')
    while True:
        net_data = get_mac('ifconfig')
        log.debug(net_data)
Exemple #11
0
def select_all_table(cursor):
    cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
    tables = cursor.fetchall()
    log.debug(tables)
    return tables