Copyright 2019 Cartesi Pte. Ltd.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

import session_registry
import utils

LOGGER = utils.get_new_logger("!!!!!DEFECTIVE!!!!!_" + __name__ +
                              "_!!!!!DEFECTIVE!!!!!")
LOGGER = utils.configure_log(LOGGER)
MAX_CYCLE = 10000001


class SessionRegistryManager(session_registry.SessionRegistryManager):
    def run_session(self, session_id, final_cycles):

        #Saturating required cycles on MAX_CYCLE
        modified_cycles = []

        for cycle in final_cycles:
            if cycle >= MAX_CYCLE:
                modified_cycles.append(MAX_CYCLE)
            else:
                modified_cycles.append(int(cycle))
import machine_discovery_pb2
import machine_manager_pb2_grpc
import machine_manager_pb2
import cartesi_machine_pb2
import utils
from session_registry import SessionIdException, AddressException, RollbackException


# docker graceful shutdown, raise a KeyboardInterrupt in case of SIGTERM
def handle_sigterm(*args):
    raise KeyboardInterrupt()


signal.signal(signal.SIGTERM, handle_sigterm)

LOGGER = utils.get_new_logger(__name__)
LOGGER = utils.configure_log(LOGGER)

LISTENING_ADDRESS = 'localhost'
LISTENING_PORT = 50051
SLEEP_TIME = 5
DEFECTIVE = False


class _MachineManager(machine_manager_pb2_grpc.MachineManagerServicer):
    def __init__(self, session_registry_manager):
        self.session_registry_manager = session_registry_manager

    def ServerShuttingDown(self, context):
        if self.session_registry_manager.shutting_down:
            context.set_details(