import socket

import zmq
import json
import numpy as np
from queue import Queue
import itertools
import time
from datetime import datetime


class MatrixMultiply:
    def __init__(self, matrix_a, matrix_b):
        self._matrix_a = np.array(matrix_a)
        self._matrix_b = np.array(matrix_b)
        self._matrix_c = [[None] * len(self._matrix_b[0])] * len(self._matrix_a)
        self.cnt = len(self._matrix_b[0]) * len(self._matrix_a)

        self.queue = Queue()
        self.make_queue()
        self.start_time = None
        self.is_started = False

    def get_funcs(self):
        return {
            "get_task": self.get_task,
            "task_done": self.task_done,
            "get_cnt": self.get_cnt,
            "save_result": self.save_result
        }

    def get_cnt(self):
        return self.cnt

    def make_queue(self):
        for i in itertools.product(range(len(self._matrix_a)), range(len(self._matrix_b[0]))):
            self.queue.put(i)

    def get_task(self, request):
        if not self.is_started:
            self.is_started = True
            self.start_time = datetime.now()
        if self.queue.qsize() > 0:
            index = self.queue.get()
            result = {
                "command": "calculate",
                "index": index,

                "arr1": list(map(int, self._matrix_a[index[0]])),
                "arr2": list(map(int, self._matrix_b[:, index[1]]))
            }

            return result
        else:
            return {"command": "stop"}

    def task_done(self, request):
        index = request['index']
        self._matrix_c[index[0]][index[1]] = request['value']
        self.cnt -= 1
        return {'success': True, "command": ""}

    def save_result(self):
        f = open('/app/result/result-{}.txt'.format(time.time()), 'w+')
        for i in self._matrix_c:
            f.write(
                " ".join(
                    list(
                        map(str, i)
                    )
                ) + '\n'
            )
        f.write(str(datetime.now() - self.start_time) + '\n')


class TCPServer:
    def __init__(self, port: [int, str], command_resolver: MatrixMultiply):  # -> None
        self._port = port

        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.REP)
        self.socket.bind("tcp://*:{port}".format(port=str(self._port)))
        self.log = open('/app/logs/{}-{}.txt'.format(int(time.time()), socket.gethostname()), 'w+')
        self.clients = []

        self.resolver = command_resolver
        self.commands = self.resolver.get_funcs()
        self.commands['register'] = self.register_client

    def __del__(self):
        self.log.close()

    def register_client(self, request):
        name = request['client_name']
        if name not in self.clients:
            self.clients.append(name)
            print("Added ", name)
            return {'success': True}
        return {'success': False}

    def listen(self):

        while True:
            if self.commands['get_cnt']() is 0 and len(self.clients) is 0:
                self.commands['save_result']()
                exit(0)

            request = self.socket.recv_json()

            try:
                if request['command'] == 'register':
                    self.log.write(request['client_name'] + " registering...\n")
                    command = self.commands.get(request['command'], None)
                    if command:
                        response = command(request)

                        self.socket.send_json(response)
                        self.log.write(request['client_name'] + " registered...\n")
                        print(response)

                elif request['client_name'] in self.clients:
                    command = self.commands.get(request['command'], None)

                    if command:
                        response = command(request)

                        if response['command'] is "stop":
                            del self.clients[self.clients.index(request['client_name'])]

                        self.socket.send_json(response)
                    else:
                        self.socket.send_json(
                            {
                                "error": {
                                    "message": "Wrong command name"
                                }
                            }
                        )
                else:
                    self.socket.send_json(
                        {
                            "error": {
                                "message": "Register before it"
                            }
                        }
                    )

            except KeyError as e:
                print(e)
                self.socket.send_json(
                    {
                        "error": {
                            "message": "You have a error in your json schema",
                            'error': str(e)
                        }
                    }
                )


def get_matrix(file_name):
    return [
        list(
            map(
                int,
                line.split()
            )
        ) for line in open(file_name)
    ]


if __name__ == '__main__':
    matrix_a = MatrixMultiply(get_matrix('/app/input/matrix-a.txt'),
                              get_matrix('/app/input/matrix-b.txt'))
    srv = TCPServer(port=5555, command_resolver=matrix_a)
    srv.listen()

5e287abc2d7ab9eae25ae9d783716175a3ff498c
<mask token>


class MatrixMultiply:

    def __init__(self, matrix_a, matrix_b):
        self._matrix_a = np.array(matrix_a)
        self._matrix_b = np.array(matrix_b)
        self._matrix_c = [[None] * len(self._matrix_b[0])] * len(self._matrix_a
            )
        self.cnt = len(self._matrix_b[0]) * len(self._matrix_a)
        self.queue = Queue()
        self.make_queue()
        self.start_time = None
        self.is_started = False

    def get_funcs(self):
        return {'get_task': self.get_task, 'task_done': self.task_done,
            'get_cnt': self.get_cnt, 'save_result': self.save_result}
    <mask token>

    def make_queue(self):
        for i in itertools.product(range(len(self._matrix_a)), range(len(
            self._matrix_b[0]))):
            self.queue.put(i)

    def get_task(self, request):
        if not self.is_started:
            self.is_started = True
            self.start_time = datetime.now()
        if self.queue.qsize() > 0:
            index = self.queue.get()
            result = {'command': 'calculate', 'index': index, 'arr1': list(
                map(int, self._matrix_a[index[0]])), 'arr2': list(map(int,
                self._matrix_b[:, index[1]]))}
            return result
        else:
            return {'command': 'stop'}

    def task_done(self, request):
        index = request['index']
        self._matrix_c[index[0]][index[1]] = request['value']
        self.cnt -= 1
        return {'success': True, 'command': ''}
    <mask token>


class TCPServer:

    def __init__(self, port: [int, str], command_resolver: MatrixMultiply):
        self._port = port
        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.REP)
        self.socket.bind('tcp://*:{port}'.format(port=str(self._port)))
        self.log = open('/app/logs/{}-{}.txt'.format(int(time.time()),
            socket.gethostname()), 'w+')
        self.clients = []
        self.resolver = command_resolver
        self.commands = self.resolver.get_funcs()
        self.commands['register'] = self.register_client

    def __del__(self):
        self.log.close()

    def register_client(self, request):
        name = request['client_name']
        if name not in self.clients:
            self.clients.append(name)
            print('Added ', name)
            return {'success': True}
        return {'success': False}

    def listen(self):
        while True:
            if self.commands['get_cnt']() is 0 and len(self.clients) is 0:
                self.commands['save_result']()
                exit(0)
            request = self.socket.recv_json()
            try:
                if request['command'] == 'register':
                    self.log.write(request['client_name'] + ' registering...\n'
                        )
                    command = self.commands.get(request['command'], None)
                    if command:
                        response = command(request)
                        self.socket.send_json(response)
                        self.log.write(request['client_name'] +
                            ' registered...\n')
                        print(response)
                elif request['client_name'] in self.clients:
                    command = self.commands.get(request['command'], None)
                    if command:
                        response = command(request)
                        if response['command'] is 'stop':
                            del self.clients[self.clients.index(request[
                                'client_name'])]
                        self.socket.send_json(response)
                    else:
                        self.socket.send_json({'error': {'message':
                            'Wrong command name'}})
                else:
                    self.socket.send_json({'error': {'message':
                        'Register before it'}})
            except KeyError as e:
                print(e)
                self.socket.send_json({'error': {'message':
                    'You have a error in your json schema', 'error': str(e)}})


<mask token>

<mask token>


class MatrixMultiply:

    def __init__(self, matrix_a, matrix_b):
        self._matrix_a = np.array(matrix_a)
        self._matrix_b = np.array(matrix_b)
        self._matrix_c = [[None] * len(self._matrix_b[0])] * len(self._matrix_a
            )
        self.cnt = len(self._matrix_b[0]) * len(self._matrix_a)
        self.queue = Queue()
        self.make_queue()
        self.start_time = None
        self.is_started = False

    def get_funcs(self):
        return {'get_task': self.get_task, 'task_done': self.task_done,
            'get_cnt': self.get_cnt, 'save_result': self.save_result}

    def get_cnt(self):
        return self.cnt

    def make_queue(self):
        for i in itertools.product(range(len(self._matrix_a)), range(len(
            self._matrix_b[0]))):
            self.queue.put(i)

    def get_task(self, request):
        if not self.is_started:
            self.is_started = True
            self.start_time = datetime.now()
        if self.queue.qsize() > 0:
            index = self.queue.get()
            result = {'command': 'calculate', 'index': index, 'arr1': list(
                map(int, self._matrix_a[index[0]])), 'arr2': list(map(int,
                self._matrix_b[:, index[1]]))}
            return result
        else:
            return {'command': 'stop'}

    def task_done(self, request):
        index = request['index']
        self._matrix_c[index[0]][index[1]] = request['value']
        self.cnt -= 1
        return {'success': True, 'command': ''}

    def save_result(self):
        f = open('/app/result/result-{}.txt'.format(time.time()), 'w+')
        for i in self._matrix_c:
            f.write(' '.join(list(map(str, i))) + '\n')
        f.write(str(datetime.now() - self.start_time) + '\n')


class TCPServer:

    def __init__(self, port: [int, str], command_resolver: MatrixMultiply):
        self._port = port
        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.REP)
        self.socket.bind('tcp://*:{port}'.format(port=str(self._port)))
        self.log = open('/app/logs/{}-{}.txt'.format(int(time.time()),
            socket.gethostname()), 'w+')
        self.clients = []
        self.resolver = command_resolver
        self.commands = self.resolver.get_funcs()
        self.commands['register'] = self.register_client

    def __del__(self):
        self.log.close()

    def register_client(self, request):
        name = request['client_name']
        if name not in self.clients:
            self.clients.append(name)
            print('Added ', name)
            return {'success': True}
        return {'success': False}

    def listen(self):
        while True:
            if self.commands['get_cnt']() is 0 and len(self.clients) is 0:
                self.commands['save_result']()
                exit(0)
            request = self.socket.recv_json()
            try:
                if request['command'] == 'register':
                    self.log.write(request['client_name'] + ' registering...\n'
                        )
                    command = self.commands.get(request['command'], None)
                    if command:
                        response = command(request)
                        self.socket.send_json(response)
                        self.log.write(request['client_name'] +
                            ' registered...\n')
                        print(response)
                elif request['client_name'] in self.clients:
                    command = self.commands.get(request['command'], None)
                    if command:
                        response = command(request)
                        if response['command'] is 'stop':
                            del self.clients[self.clients.index(request[
                                'client_name'])]
                        self.socket.send_json(response)
                    else:
                        self.socket.send_json({'error': {'message':
                            'Wrong command name'}})
                else:
                    self.socket.send_json({'error': {'message':
                        'Register before it'}})
            except KeyError as e:
                print(e)
                self.socket.send_json({'error': {'message':
                    'You have a error in your json schema', 'error': str(e)}})


<mask token>

<mask token>


class MatrixMultiply:

    def __init__(self, matrix_a, matrix_b):
        self._matrix_a = np.array(matrix_a)
        self._matrix_b = np.array(matrix_b)
        self._matrix_c = [[None] * len(self._matrix_b[0])] * len(self._matrix_a
            )
        self.cnt = len(self._matrix_b[0]) * len(self._matrix_a)
        self.queue = Queue()
        self.make_queue()
        self.start_time = None
        self.is_started = False

    def get_funcs(self):
        return {'get_task': self.get_task, 'task_done': self.task_done,
            'get_cnt': self.get_cnt, 'save_result': self.save_result}

    def get_cnt(self):
        return self.cnt

    def make_queue(self):
        for i in itertools.product(range(len(self._matrix_a)), range(len(
            self._matrix_b[0]))):
            self.queue.put(i)

    def get_task(self, request):
        if not self.is_started:
            self.is_started = True
            self.start_time = datetime.now()
        if self.queue.qsize() > 0:
            index = self.queue.get()
            result = {'command': 'calculate', 'index': index, 'arr1': list(
                map(int, self._matrix_a[index[0]])), 'arr2': list(map(int,
                self._matrix_b[:, index[1]]))}
            return result
        else:
            return {'command': 'stop'}

    def task_done(self, request):
        index = request['index']
        self._matrix_c[index[0]][index[1]] = request['value']
        self.cnt -= 1
        return {'success': True, 'command': ''}

    def save_result(self):
        f = open('/app/result/result-{}.txt'.format(time.time()), 'w+')
        for i in self._matrix_c:
            f.write(' '.join(list(map(str, i))) + '\n')
        f.write(str(datetime.now() - self.start_time) + '\n')


class TCPServer:

    def __init__(self, port: [int, str], command_resolver: MatrixMultiply):
        self._port = port
        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.REP)
        self.socket.bind('tcp://*:{port}'.format(port=str(self._port)))
        self.log = open('/app/logs/{}-{}.txt'.format(int(time.time()),
            socket.gethostname()), 'w+')
        self.clients = []
        self.resolver = command_resolver
        self.commands = self.resolver.get_funcs()
        self.commands['register'] = self.register_client

    def __del__(self):
        self.log.close()

    def register_client(self, request):
        name = request['client_name']
        if name not in self.clients:
            self.clients.append(name)
            print('Added ', name)
            return {'success': True}
        return {'success': False}

    def listen(self):
        while True:
            if self.commands['get_cnt']() is 0 and len(self.clients) is 0:
                self.commands['save_result']()
                exit(0)
            request = self.socket.recv_json()
            try:
                if request['command'] == 'register':
                    self.log.write(request['client_name'] + ' registering...\n'
                        )
                    command = self.commands.get(request['command'], None)
                    if command:
                        response = command(request)
                        self.socket.send_json(response)
                        self.log.write(request['client_name'] +
                            ' registered...\n')
                        print(response)
                elif request['client_name'] in self.clients:
                    command = self.commands.get(request['command'], None)
                    if command:
                        response = command(request)
                        if response['command'] is 'stop':
                            del self.clients[self.clients.index(request[
                                'client_name'])]
                        self.socket.send_json(response)
                    else:
                        self.socket.send_json({'error': {'message':
                            'Wrong command name'}})
                else:
                    self.socket.send_json({'error': {'message':
                        'Register before it'}})
            except KeyError as e:
                print(e)
                self.socket.send_json({'error': {'message':
                    'You have a error in your json schema', 'error': str(e)}})


def get_matrix(file_name):
    return [list(map(int, line.split())) for line in open(file_name)]


<mask token>

import socket
import zmq
import json
import numpy as np
from queue import Queue
import itertools
import time
from datetime import datetime


class MatrixMultiply:

    def __init__(self, matrix_a, matrix_b):
        self._matrix_a = np.array(matrix_a)
        self._matrix_b = np.array(matrix_b)
        self._matrix_c = [[None] * len(self._matrix_b[0])] * len(self._matrix_a
            )
        self.cnt = len(self._matrix_b[0]) * len(self._matrix_a)
        self.queue = Queue()
        self.make_queue()
        self.start_time = None
        self.is_started = False

    def get_funcs(self):
        return {'get_task': self.get_task, 'task_done': self.task_done,
            'get_cnt': self.get_cnt, 'save_result': self.save_result}

    def get_cnt(self):
        return self.cnt

    def make_queue(self):
        for i in itertools.product(range(len(self._matrix_a)), range(len(
            self._matrix_b[0]))):
            self.queue.put(i)

    def get_task(self, request):
        if not self.is_started:
            self.is_started = True
            self.start_time = datetime.now()
        if self.queue.qsize() > 0:
            index = self.queue.get()
            result = {'command': 'calculate', 'index': index, 'arr1': list(
                map(int, self._matrix_a[index[0]])), 'arr2': list(map(int,
                self._matrix_b[:, index[1]]))}
            return result
        else:
            return {'command': 'stop'}

    def task_done(self, request):
        index = request['index']
        self._matrix_c[index[0]][index[1]] = request['value']
        self.cnt -= 1
        return {'success': True, 'command': ''}

    def save_result(self):
        f = open('/app/result/result-{}.txt'.format(time.time()), 'w+')
        for i in self._matrix_c:
            f.write(' '.join(list(map(str, i))) + '\n')
        f.write(str(datetime.now() - self.start_time) + '\n')


class TCPServer:

    def __init__(self, port: [int, str], command_resolver: MatrixMultiply):
        self._port = port
        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.REP)
        self.socket.bind('tcp://*:{port}'.format(port=str(self._port)))
        self.log = open('/app/logs/{}-{}.txt'.format(int(time.time()),
            socket.gethostname()), 'w+')
        self.clients = []
        self.resolver = command_resolver
        self.commands = self.resolver.get_funcs()
        self.commands['register'] = self.register_client

    def __del__(self):
        self.log.close()

    def register_client(self, request):
        name = request['client_name']
        if name not in self.clients:
            self.clients.append(name)
            print('Added ', name)
            return {'success': True}
        return {'success': False}

    def listen(self):
        while True:
            if self.commands['get_cnt']() is 0 and len(self.clients) is 0:
                self.commands['save_result']()
                exit(0)
            request = self.socket.recv_json()
            try:
                if request['command'] == 'register':
                    self.log.write(request['client_name'] + ' registering...\n'
                        )
                    command = self.commands.get(request['command'], None)
                    if command:
                        response = command(request)
                        self.socket.send_json(response)
                        self.log.write(request['client_name'] +
                            ' registered...\n')
                        print(response)
                elif request['client_name'] in self.clients:
                    command = self.commands.get(request['command'], None)
                    if command:
                        response = command(request)
                        if response['command'] is 'stop':
                            del self.clients[self.clients.index(request[
                                'client_name'])]
                        self.socket.send_json(response)
                    else:
                        self.socket.send_json({'error': {'message':
                            'Wrong command name'}})
                else:
                    self.socket.send_json({'error': {'message':
                        'Register before it'}})
            except KeyError as e:
                print(e)
                self.socket.send_json({'error': {'message':
                    'You have a error in your json schema', 'error': str(e)}})


def get_matrix(file_name):
    return [list(map(int, line.split())) for line in open(file_name)]


if __name__ == '__main__':
    matrix_a = MatrixMultiply(get_matrix('/app/input/matrix-a.txt'),
        get_matrix('/app/input/matrix-b.txt'))
    srv = TCPServer(port=5555, command_resolver=matrix_a)
    srv.listen()

import socket

import zmq
import json
import numpy as np
from queue import Queue
import itertools
import time
from datetime import datetime


class MatrixMultiply:
    def __init__(self, matrix_a, matrix_b):
        self._matrix_a = np.array(matrix_a)
        self._matrix_b = np.array(matrix_b)
        self._matrix_c = [[None] * len(self._matrix_b[0])] * len(self._matrix_a)
        self.cnt = len(self._matrix_b[0]) * len(self._matrix_a)

        self.queue = Queue()
        self.make_queue()
        self.start_time = None
        self.is_started = False

    def get_funcs(self):
        return {
            "get_task": self.get_task,
            "task_done": self.task_done,
            "get_cnt": self.get_cnt,
            "save_result": self.save_result
        }

    def get_cnt(self):
        return self.cnt

    def make_queue(self):
        for i in itertools.product(range(len(self._matrix_a)), range(len(self._matrix_b[0]))):
            self.queue.put(i)

    def get_task(self, request):
        if not self.is_started:
            self.is_started = True
            self.start_time = datetime.now()
        if self.queue.qsize() > 0:
            index = self.queue.get()
            result = {
                "command": "calculate",
                "index": index,

                "arr1": list(map(int, self._matrix_a[index[0]])),
                "arr2": list(map(int, self._matrix_b[:, index[1]]))
            }

            return result
        else:
            return {"command": "stop"}

    def task_done(self, request):
        index = request['index']
        self._matrix_c[index[0]][index[1]] = request['value']
        self.cnt -= 1
        return {'success': True, "command": ""}

    def save_result(self):
        f = open('/app/result/result-{}.txt'.format(time.time()), 'w+')
        for i in self._matrix_c:
            f.write(
                " ".join(
                    list(
                        map(str, i)
                    )
                ) + '\n'
            )
        f.write(str(datetime.now() - self.start_time) + '\n')


class TCPServer:
    def __init__(self, port: [int, str], command_resolver: MatrixMultiply):  # -> None
        self._port = port

        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.REP)
        self.socket.bind("tcp://*:{port}".format(port=str(self._port)))
        self.log = open('/app/logs/{}-{}.txt'.format(int(time.time()), socket.gethostname()), 'w+')
        self.clients = []

        self.resolver = command_resolver
        self.commands = self.resolver.get_funcs()
        self.commands['register'] = self.register_client

    def __del__(self):
        self.log.close()

    def register_client(self, request):
        name = request['client_name']
        if name not in self.clients:
            self.clients.append(name)
            print("Added ", name)
            return {'success': True}
        return {'success': False}

    def listen(self):

        while True:
            if self.commands['get_cnt']() is 0 and len(self.clients) is 0:
                self.commands['save_result']()
                exit(0)

            request = self.socket.recv_json()

            try:
                if request['command'] == 'register':
                    self.log.write(request['client_name'] + " registering...\n")
                    command = self.commands.get(request['command'], None)
                    if command:
                        response = command(request)

                        self.socket.send_json(response)
                        self.log.write(request['client_name'] + " registered...\n")
                        print(response)

                elif request['client_name'] in self.clients:
                    command = self.commands.get(request['command'], None)

                    if command:
                        response = command(request)

                        if response['command'] is "stop":
                            del self.clients[self.clients.index(request['client_name'])]

                        self.socket.send_json(response)
                    else:
                        self.socket.send_json(
                            {
                                "error": {
                                    "message": "Wrong command name"
                                }
                            }
                        )
                else:
                    self.socket.send_json(
                        {
                            "error": {
                                "message": "Register before it"
                            }
                        }
                    )

            except KeyError as e:
                print(e)
                self.socket.send_json(
                    {
                        "error": {
                            "message": "You have a error in your json schema",
                            'error': str(e)
                        }
                    }
                )


def get_matrix(file_name):
    return [
        list(
            map(
                int,
                line.split()
            )
        ) for line in open(file_name)
    ]


if __name__ == '__main__':
    matrix_a = MatrixMultiply(get_matrix('/app/input/matrix-a.txt'),
                              get_matrix('/app/input/matrix-b.txt'))
    srv = TCPServer(port=5555, command_resolver=matrix_a)
    srv.listen()
