<commit_before>/*
 *  Project:    moba-server
 *
 *  Copyright (C) 2016 <NAME> <<EMAIL>>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/agpl.txt>.
 *
 */

package moba.server.messages;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import moba.server.com.Dispatcher;
import moba.server.datatypes.enumerations.ErrorId;
import moba.server.datatypes.enumerations.HardwareState;
import moba.server.datatypes.objects.ErrorData;
import moba.server.messages.messageType.ClientMessage;
import moba.server.messages.messageType.InternMessage;
import moba.server.messages.messageType.ServerMessage;
import moba.server.utilities.exceptions.ErrorException;

public class MessageLoop {
    protected static final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

    protected Map<Integer, MessageHandlerA> handlers = new HashMap<>();
    protected Dispatcher dispatcher = null;

    public MessageLoop(Dispatcher dispatcher) {
        this.dispatcher = dispatcher;
    }

    public void addHandler(MessageHandlerA msgHandler) {
        msgHandler.init();
        handlers.put(msgHandler.getGroupId(), msgHandler);
    }

    public Set<Integer> getRegisteredHandlers() {
        return handlers.keySet();
    }

    public boolean loop(MessageQueue in)
    throws InterruptedException {
        while(true) {
            Message msg = in.take();

            if(msg.getGroupId() == InternMessage.GROUP_ID) {
                switch(InternMessage.fromId(msg.getMessageId())) {
                    case SERVER_RESET:
                        in.clear();
                        resetHandler();
                        return true;

                    case SERVER_SHUTDOWN:
                        shutdownHandler();
                        return false;

                    case SET_HARDWARE_STATE:
                        hardwareStateChangedHandler((HardwareState)msg.getData());
                        continue;

                    case CLIENT_SHUTDOWN:
                        continue;
                }
            }

            if(!handlers.containsKey(msg.getGroupId())) {
                ErrorData err = new ErrorData(ErrorId.UNKNOWN_GROUP_ID, "handler for group <" + Integer.toString(msg.getGroupId()) + "> was not registered!");

                MessageLoop.LOGGER.log(Level.WARNING, err.toString());

                dispatcher.dispatch(new Message(ClientMessage.ERROR, err, msg.getEndpoint()));
                continue;
            }
            try {
                handlers.get(msg.getGroupId()).handleMsg(msg);
            } catch(ErrorException e) {
                dispatcher.dispatch(new Message(ClientMessage.ERROR, e.getErrorData(), msg.getEndpoint()));
            }
        }<commit_msg>missing method call added<commit_after>/*
 *  Project:    moba-server
 *
 *  Copyright (C) 2016 <NAME> <<EMAIL>>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/agpl.txt>.
 *
 */

package moba.server.messages;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import moba.server.com.Dispatcher;
import moba.server.datatypes.enumerations.ErrorId;
import moba.server.datatypes.enumerations.HardwareState;
import moba.server.datatypes.objects.ErrorData;
import moba.server.messages.messageType.ClientMessage;
import moba.server.messages.messageType.InternMessage;
import moba.server.messages.messageType.ServerMessage;
import moba.server.utilities.exceptions.ErrorException;

public class MessageLoop {
    protected static final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

    protected Map<Integer, MessageHandlerA> handlers = new HashMap<>();
    protected Dispatcher dispatcher = null;

    public MessageLoop(Dispatcher dispatcher) {
        this.dispatcher = dispatcher;
    }

    public void addHandler(MessageHandlerA msgHandler) {
        msgHandler.init();
        handlers.put(msgHandler.getGroupId(), msgHandler);
    }

    public Set<Integer> getRegisteredHandlers() {
        return handlers.keySet();
    }

    public boolean loop(MessageQueue in)
    throws InterruptedException {
        while(true) {
            Message msg = in.take();

            if(msg.getGroupId() == InternMessage.GROUP_ID) {
                switch(InternMessage.fromId(msg.getMessageId())) {
                    case SERVER_RESET:
                        in.clear();
                        resetHandler();
                        return true;

                    case SERVER_SHUTDOWN:
                        shutdownHandler();
                        return false;

                    case SET_HARDWARE_STATE:
                        hardwareStateChangedHandler((HardwareState)msg.getData());
                        continue;

                    case CLIENT_SHUTDOWN:
                        handleClientClose(msg);
                        continue;
                }
            }

            if(!handlers.containsKey(msg.getGroupId())) {
                ErrorData err = new ErrorData(ErrorId.UNKNOWN_GROUP_ID, "handler for group <" + Integer.toString(msg.getGroupId()) + "> was not registered!");

                MessageLoop.LOGGER.log(Level.WARNING, err.toString());

                dispatcher.dispatch(new Message(ClientMessage.ERROR, err, msg.getEndpoint()));
                continue;
            }
            try {
                handlers.get(msg.getGroupId()).handleMsg(msg);
            } catch(ErrorException e) {
                dispatcher.dispatch(new Message(ClientMessage.ERROR, e.getErrorData(), msg.getEndpoint()));
            }
        }
{'fraction_non_alphanumeric': 0.09073967889908256, 'fraction_numerical': 0.0014334862385321102, 'mean_word_length': 2.3885381253035454, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1365433', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1792, 'n_words': 472}