package de.unipaderborn.visuflow.debug.monitoring;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import org.json.JSONObject;

import de.unipaderborn.visuflow.model.DataModel;
import de.unipaderborn.visuflow.util.ServiceUtil;

public class MonitoringServer {

	private ServerSocket serverSocket;
	private Socket clientSocket;
	private Thread t;
	private boolean running = true;
	private DataModel dataModel = ServiceUtil.getService(DataModel.class);

	public void start() {
		System.out.println("Monitoring server starting");
		t = new Thread() {
			@Override
			public void run() {
				try {
					serverSocket = new ServerSocket(6543);
					clientSocket = serverSocket.accept();

					BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
					String line;
					while(running && (line = br.readLine()) != null) {
						JSONObject json = new JSONObject(line);
						String unitFqn = json.getString("unit");
						if(json.has("in")) {
							String in = json.getString("in");
							dataModel.setInSet(unitFqn, "in", in);
						}
						if(json.has("out")) {
							String out = json.getString("out");
							dataModel.setOutSet(unitFqn, "out", out);
						}
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};
		t.setDaemon(true);
		t.setName("");
		t.start();
	}

	public void stop() {
		System.out.println("Monitoring server stopping");
		running = false;
		t.interrupt();

		if(clientSocket != null && !clientSocket.isClosed()) {
			try {
				clientSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(serverSocket != null && !serverSocket.isClosed()) {
			try {
				serverSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

@@ -218,24 +218,106 @@
 SONObject;%0A%0A
+import de.unipaderborn.visuflow.Logger;%0Aimport de.unipaderborn.visuflow.Visuflow;%0A
 import de.un
@@ -627,16 +627,78 @@
 class);%0A
+%09private Logger logger = Visuflow.getDefault().getLogger(); %0A%09
 %0A%09public
@@ -715,34 +715,27 @@
 t() %7B%0A%09%09
-System.out.println
+logger.info
 (%22Monito
@@ -1471,34 +1471,70 @@
  %7B%0A%09%09%09%09%09
-e.printStackTrace(
+logger.error(%22Monitoring server threw an exception%22, e
 );%0A%09%09%09%09%7D
@@ -1627,26 +1627,19 @@
 %7B%0A%09%09
-System.out.println
+logger.info
 (%22Mo
@@ -1828,34 +1828,77 @@
 ) %7B%0A%09%09%09%09
-e.printStackTrace(
+logger.error(%22Couldn't close monitoring server connection%22, e
 );%0A%09%09%09%7D%0A
@@ -2030,26 +2030,69 @@
 %09%09%09%09
-e.printStackTrace(
+logger.error(%22Couldn't close monitoring server connection%22, e
 );%0A%09

Use plug-in logger instead of console out