<commit_before>from __future__ import absolute_import
from __future__ import print_function
import argparse
import json
import os
import pprint
import socket
import sys
from threading import Lock, Thread
import time

import select

import psutil
import subprocess

from aetros.backend import invalid_json_values, EventListener, parse_message, BackendClient


class ServerClient(BackendClient):
    def __init__(self, api_host, api_port, event_listener):
        BackendClient.__init__(self, api_host, api_port, event_listener)
        self.server_api_key = None

    def configure(self, server_api_key):
        self.server_api_key = server_api_key

    def on_connect(self):
        self.send_message({'register_server': self.server_api_key})
        message = self.read_one_message(self.s)

        if isinstance(message, dict) and 'a' in message:
            if "ACCESS_DENIED" in message['a']:
                print('Access denied')
                return False

            if "SERVER_ALREADY_REGISTERED" in message['a']:
                print("Registration failed. This server is already registered.")
                return

            if "SERVER_REGISTERED" in message['a']:
                self.registered = True
                server_id = message['id']
                self.event_listener.fire('registration', server_id)
                print("Connected to %s as server %s" % (self.api_host, server_id))
                return True

        print("Registration failed due to protocol error.")
        return False

    def handle_message(self, messages):
        for message in messages:

            if not isinstance(message, dict):
                continue

            if 'stop' in message:
                self.close()
                self.event_listener.fire('stop')

            if 'type' in message:
                if message['type'] == 'start-jobs':
                    self.event_listener.fire('start-jobs', message['jobs'])

                if message['type'] == 'stop-job':
                    self.event_listener.fire('stop-job', message['id'])


class ServerCommand:
    model = None
    job_model = None

    def __init__(self):
        self.last_utilization = None
        self.last_net = {}
        self.nets = []
        self.server = None
        self.queue = []
        self.queuedMap = {}
        self.job_processes = []
        self.max_parallel_jobs = 2
        self.registered = False
        self.show_stdout = False

    def main(self, args):<commit_msg>Fixed server command ServerClient<commit_after>from __future__ import absolute_import
from __future__ import print_function
import argparse
import json
import os
import pprint
import socket
import sys
from threading import Lock, Thread
import time

import select

import psutil
import subprocess

from aetros.backend import invalid_json_values, EventListener, parse_message, BackendClient


class ServerClient(BackendClient):
    def __init__(self, api_host, api_port, event_listener):
        BackendClient.__init__(self, api_host, api_port, event_listener)
        self.server_api_key = None

    def configure(self, server_api_key):
        self.server_api_key = server_api_key

    def on_connect(self):
        self.send_message({'register_server': self.server_api_key})
        messages = self.wait_for_at_least_one_message(self.s)

        message = messages.pop(0)
        if isinstance(message, dict) and 'a' in message:
            if "ACCESS_DENIED" in message['a']:
                print('Access denied')
                return False

            if "SERVER_ALREADY_REGISTERED" in message['a']:
                print("Registration failed. This server is already registered.")
                return

            if "SERVER_REGISTERED" in message['a']:
                self.registered = True
                server_id = message['id']
                self.event_listener.fire('registration', server_id)
                print("Connected to %s as server %s" % (self.api_host, server_id))
                self.handle_messages(messages)
                return True

        print("Registration failed due to protocol error.")
        return False

    def handle_messages(self, messages):
        for message in messages:
            if not isinstance(message, dict):
                return

            if 'stop' in message:
                self.close()
                self.event_listener.fire('stop')

            if 'type' in message:
                if message['type'] == 'start-jobs':
                    self.event_listener.fire('start-jobs', message['jobs'])

                if message['type'] == 'stop-job':
                    self.event_listener.fire('stop-job', message['id'])


class ServerCommand:
    model = None
    job_model = None

    def __init__(self):
        self.last_utilization = None
        self.last_net = {}
        self.nets = []
        self.server = None
        self.queue = []
        self.queuedMap = {}
        self.job_processes = []
        self.max_parallel_jobs = 2
        self.registered = False
        self.show_stdout = False

    def main(self, args):
{'fraction_non_alphanumeric': 0.08884094920572662, 'fraction_numerical': 0.000588350656991567, 'mean_word_length': 2.213610586011342, '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': '4531454', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1336, 'n_words': 359}