<commit_before>#!/usr/bin/python2

from socket import socket, AF_INET, SOCK_STREAM
from health_messages import Health_Message as HM
from health_bench import Health_Bench as HB
from health_bench import Health_CPU as HCPU
import atexit
import health_protocol as HP
import logging
import time
import sys

s = socket(AF_INET, SOCK_STREAM)
connected = False

def invalid_message(msg):
    HP.logger.error("%s is not a valid message for a client" % msg.get_message_type())

def none(socket, msg):
    invalid_message(msg)

def start(socket, msg):
    HP.logger.info("Recevied START")

def stop(socket, msg):
    return

def completed(socket, msg):
    return

def connect(socket, msg):
    invalid_message(msg)

def disconnect(socket, msg):
    HP.logger.info("Disconnecting based on server request")
    cleanup()

def ack(socket, msg):
    return

def nack(socket, msg):
    return

def cpu(socket, msg):
    HP.logger.info("Module CPU (%d sec)" % msg.running_time)
    action(socket, msg, HCPU(msg, socket, HP.logger))
    return

def storage(socket, msg):
    return

def memory(socket, msg):
    return

def network(socket, msg):
    return

def action(socket, msg, hb):
    handlers = { HM.NONE     : hb.none,
                HM.STOP      : hb.stop,
                HM.START     : hb.start,
                HM.COMPLETED : hb.completed,
                HM.NOTCOMPLETED : hb.notcompleted,
    }
    
    HP.logger.info("Received action %s (%d)" % (msg.get_action_type(), msg.action)) 
    handlers[msg.action]()

def module(socket, msg):
    handlers = { HM.NONE    : none, 
                HM.CPU      : cpu,
                HM.STORAGE  : storage,
                HM.MEMORY   : memory,
                HM.NETWORK  : network,
    }
    
    HP.logger.info("Received module %s (%d)" % (msg.get_module_type(), msg.module)) 
    handlers[msg.module](socket, msg)


def connect_to_server(hrdw, hostname):
    global s
    global connected
    try:
        s.connect((hostname, 20000))
    except Exception as e:
        HP.logger.error("Server %s is not available, exiting" % hostname)
        return

    connected = True
    HP.send_hm_message(s, HM(HM.CONNECT), True)
    while True:
        try:
            msg = HP.recv_hm_message(s)
        except Exception as e:
            HP.logger.error("Broken socket, exiting !")
            break;

        if not msg:
            continue;<commit_msg>health: Don't call cleanup directly<commit_after>#!/usr/bin/python2

from socket import socket, AF_INET, SOCK_STREAM
from health_messages import Health_Message as HM
from health_bench import Health_Bench as HB
from health_bench import Health_CPU as HCPU
import atexit
import health_protocol as HP
import logging
import time
import sys

s = socket(AF_INET, SOCK_STREAM)
connected = False

def invalid_message(msg):
    HP.logger.error("%s is not a valid message for a client" % msg.get_message_type())

def none(socket, msg):
    invalid_message(msg)

def start(socket, msg):
    HP.logger.info("Recevied START")

def stop(socket, msg):
    return

def completed(socket, msg):
    return

def connect(socket, msg):
    invalid_message(msg)

def disconnect(socket, msg):
    HP.logger.info("Disconnecting based on server request")
    sys.exit(0)

def ack(socket, msg):
    return

def nack(socket, msg):
    return

def cpu(socket, msg):
    HP.logger.info("Module CPU (%d sec)" % msg.running_time)
    action(socket, msg, HCPU(msg, socket, HP.logger))
    return

def storage(socket, msg):
    return

def memory(socket, msg):
    return

def network(socket, msg):
    return

def action(socket, msg, hb):
    handlers = { HM.NONE     : hb.none,
                HM.STOP      : hb.stop,
                HM.START     : hb.start,
                HM.COMPLETED : hb.completed,
                HM.NOTCOMPLETED : hb.notcompleted,
    }
    
    HP.logger.info("Received action %s (%d)" % (msg.get_action_type(), msg.action)) 
    handlers[msg.action]()

def module(socket, msg):
    handlers = { HM.NONE    : none, 
                HM.CPU      : cpu,
                HM.STORAGE  : storage,
                HM.MEMORY   : memory,
                HM.NETWORK  : network,
    }
    
    HP.logger.info("Received module %s (%d)" % (msg.get_module_type(), msg.module)) 
    handlers[msg.module](socket, msg)


def connect_to_server(hrdw, hostname):
    global s
    global connected
    try:
        s.connect((hostname, 20000))
    except Exception as e:
        HP.logger.error("Server %s is not available, exiting" % hostname)
        sys.exit(1)

    connected = True
    HP.send_hm_message(s, HM(HM.CONNECT), True)
    while True:
        try:
            msg = HP.recv_hm_message(s)
        except Exception as e:
            HP.logger.error("Broken socket, exiting !")
            break;

        if not msg:
            continue;
{'fraction_non_alphanumeric': 0.1091965953913224, 'fraction_numerical': 0.0029063732613659955, 'mean_word_length': 2.4989106753812638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5176550', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1530, 'n_words': 415}