<commit_before>#!/usr/bin/python3
import os
import sys
sys.path.append('../libs')

import configparser
import socket
import coronaprotocol
import coronalogger
import coronautils
import threading
import time
import signal
import agents
import mysql.connector as mariadb
import uuid

config = configparser.ConfigParser()
config.read('corona-server.conf')

serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host = config['Corona']['ListenAddress']
port = config['Corona']['ListenPort']
serverSocket.bind((host, int(port)))

serverSocket.listen(5)

cp = coronaprotocol.CoronaProtocol()
log = coronalogger.CoronaLogger()
log.setLogTarget('stdout')

try:
    mysqlConnData = mariadb.connect(user=config['MySQL']['User'], password=config['MySQL']['Passwd'], database=config['MySQL']['DbName'])
except mariadb.Error as me:
    log.messageLog("MySQL Error: {}".format(me))
    sys.exit()

connectedAgents = agents.CoronaAgents(log, mysqlConnData)

class agentPingThread (threading.Thread):
    """Thread for pinging all registered agents. If not responding, deregister that agent from agent pool"""

    def __init__(self):
        threading.Thread.__init__(self, name = 'Pinger')
        
    def run(self):
        connectedAgents.populateListFromMysql()
        self.pingAllAgents()
        
    def pingAgent(self, agentIp, agentPort):
        pingerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        pingerSocket.settimeout(float(config['Corona']['PingTimeout']))
        log.messageLog('Agent %s pinged' % agentIp)
        try:
            pingerSocket.connect((agentIp, int(agentPort)))
            message = cp.agentPing(agentIp, coronautils.CoronaUtils().randomStringGenerator(24))
            log.messageLog(message)
            pingerSocket.send(message.encode('ascii'))
            response = pingerSocket.recv(4096)
            responseDecoded = cp.decodeMessage(response.decode('ascii'))
            log.messageLog('DEBUG: Response from agent: %s' % responseDecoded['message'])
            pingerSocket.close()
        except socket.timeout:
            log.messageLog('Socket timeout from agent %s' % agentIp)
            connectedAgents.removeAgent(agentIp)
        except socket.error as e:
            log.messageLog('Socket error %s from agent %s' % (e.strerror, agentIp))
            connectedAgents.removeAgent(agentIp)
        finally:
            pingerSocket.close()
        <commit_msg>Agent is removed from server when wrong HCID value ponged back<commit_after>#!/usr/bin/python3
import os
import sys
sys.path.append('../libs')

import configparser
import socket
import coronaprotocol
import coronalogger
import coronautils
import threading
import time
import signal
import agents
import mysql.connector as mariadb
import uuid

config = configparser.ConfigParser()
config.read('corona-server.conf')

serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host = config['Corona']['ListenAddress']
port = config['Corona']['ListenPort']
serverSocket.bind((host, int(port)))

serverSocket.listen(5)

cp = coronaprotocol.CoronaProtocol()
log = coronalogger.CoronaLogger()
log.setLogTarget('stdout')

try:
    mysqlConnData = mariadb.connect(user=config['MySQL']['User'], password=config['MySQL']['<PASSWORD>'], database=config['MySQL']['DbName'])
except mariadb.Error as me:
    log.messageLog("MySQL Error: {}".format(me))
    sys.exit()

connectedAgents = agents.CoronaAgents(log, mysqlConnData)

class agentPingThread (threading.Thread):
    """Thread for pinging all registered agents. If not responding, deregister that agent from agent pool"""

    def __init__(self):
        threading.Thread.__init__(self, name = 'Pinger')
        
    def run(self):
        connectedAgents.populateListFromMysql()
        self.pingAllAgents()
        
    def pingAgent(self, agentIp, agentPort):
        pingerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        pingerSocket.settimeout(float(config['Corona']['PingTimeout']))
        log.messageLog('Agent %s pinged' % agentIp)
        try:
            pingerSocket.connect((agentIp, int(agentPort)))
            sourceHcid = coronautils.CoronaUtils().randomStringGenerator(24)
            message = cp.agentPing(agentIp, sourceHcid)
            log.messageLog(message)
            pingerSocket.send(message.encode('ascii'))
            response = pingerSocket.recv(4096)
            responseDecoded = cp.decodeMessage(response.decode('ascii'))
            log.messageLog('DEBUG: Response from agent: %s hcid: %s' % (responseDecoded['message'], responseDecoded['parameters']['hcid']))
            pingerSocket.close()
            if sourceHcid != responseDecoded['parameters']['hcid']:
                log.messageLog('Agent %s HCID check failed' % agentIp)
                connectedAgents.removeAgent(agentIp)
        except socket.timeout:
            log.messageLog('Socket timeout from agent %s' % agentIp)
            connectedAgents.removeAgent(agentIp)
        except socket.error as e:
            log.messageLog('Socket error %s from agent %s' % (e.strerror, agentIp))
            connectedAgents.removeAgent(agentIp)
        finally:
            pingerSocket.close()
        
{'fraction_non_alphanumeric': 0.1183642843744138, 'fraction_numerical': 0.0033764772087788407, 'mean_word_length': 3.6812993854258123, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4296777', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1544, 'n_words': 323}