<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package distributed.net;

import distributed.util.SettingsProvider;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.jgroups.Address;
import org.jgroups.Message;

/**
 *
 * @author steffen
 */
public class MessageProcessor extends Thread {

    private static MessageProcessor instance;
    private BlockingQueue<Message> jobQueue;
    private boolean isRunning;

    public static synchronized MessageProcessor getInstance() {
        if (instance == null) {
            instance = new MessageProcessor();
            instance.start();
        }

        return instance;
    }

    private MessageProcessor() {
        jobQueue = new LinkedBlockingQueue<>();
        isRunning = true;
    }

    @Override
    public void run() {
        Message msg;
        while (isRunning) {
            try {
                msg = jobQueue.take();
                //TODO Do what ever the fuck you want
                
                //TODO Make use of the new Message types

//                if (msg.getObject() instanceof Address) {
//                    System.out.println("Leader is: " + (Address) msg.getObject());
//                } else if (msg.getObject() instanceof String) {
//                    processStringMessage(msg);
//                } else if (msg.getObject() instanceof Post) {
//                    System.out.println(SettingsProvider.getInstance().getUserName() + ": " + ((Post) msg.getObject()).getMessage());
//                    //Hier Callback fuer Mainframe!!
//                } else if (msg.getObject() instanceof PrivateMessage) {
//
//                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void processStringMessage(Message msg) {
        String message = (String) msg.getObject();
        if (message.equals("update")) {
            System.out.println("Update request");
        } else if (message.equals("close")) {
            DistributedCore.getInstance().closeConnection();
            System.out.println("Logged out");
        }
    }

    public void end() {
        isRunning = false;
        //TODO Save to job queue
        instance = null;
    }

    public boolean addMessage(Message message) {
        if (!isRunning) {
            return false;
        }

        synchronized (jobQueue) {<commit_msg>Message processing (work in progress)<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package distributed.net;

import distributed.dto.*;
import distributed.util.SettingsProvider;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.jgroups.Address;
import org.jgroups.Message;

/**
 *
 * @author steffen
 */
public class MessageProcessor extends Thread {

    private static MessageProcessor instance;
    private BlockingQueue<Message> jobQueue;
    private boolean isRunning;

    public static synchronized MessageProcessor getInstance() {
        if (instance == null) {
            instance = new MessageProcessor();
            instance.start();
        }

        return instance;
    }

    private MessageProcessor() {
        jobQueue = new LinkedBlockingQueue<>();
        isRunning = true;
    }

    @Override
    public void run() {
        Message msg;
        while (isRunning) {
            try {
                msg = jobQueue.take();
                //TODO Do what ever the fuck you want
                
                //TODO Make use of the new Message types

//                if (msg.getObject() instanceof Address) {
//                    System.out.println("Leader is: " + (Address) msg.getObject());
//                } else if (msg.getObject() instanceof String) {
//                    processStringMessage(msg);
//                } else if (msg.getObject() instanceof Post) {
//                    System.out.println(SettingsProvider.getInstance().getUserName() + ": " + ((Post) msg.getObject()).getMessage());
//                    //Hier Callback fuer Mainframe!!
//                } else if (msg.getObject() instanceof PrivateMessage) {
//
//                }
                
                if (msg.getObject() instanceof GroupMessage) {
                    
                } else if (msg.getObject() instanceof PrivateMessage) {
                    // show content of message on your Anzeigetafel
                    // (use GUI components)
                    // (store in local database?)
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void processStringMessage(Message msg) {
        String message = (String) msg.getObject();
        if (message.equals("update")) {
            System.out.println("Update request");
        } else if (message.equals("close")) {
            DistributedCore.getInstance().closeConnection();
            System.out.println("Logged out");
        }
    }

    public void end() {
        isRunning = false;
        //TODO Save to job queue
        instance = null;
    }

    public boolean addMessage(Message message) {
        if (!isRunning) {
            return false;
        }

        synchronized (jobQueue) {
{'fraction_non_alphanumeric': 0.10429007359540478, 'fraction_numerical': 0.0, 'mean_word_length': 1.8501278772378518, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, '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': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '525800', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1330, 'n_words': 387}