package art.servers.controller; import art.library.model.devices.application.ApplicationRealtime; import art.library.utils.synchro.Mutex; import art.library.model.transactions.traces.Trace; import art.library.model.transactions.traces.TracePersistance; import art.servers.Shared; import art.servers.configuration.ConfigurationListenerDEBUG; import java.net.ServerSocket; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; public class ControllerListenerDebug extends Thread { public static final int TYPE_INFORMATION = 1; public static final int TYPE_WARNING = 2; public static final int TYPE_ERROR = 3; private String name = null; private Mutex mutexConnection = new Mutex(); private ServerSocket serverSocket = null; private List lconnection = new ArrayList(); private ConfigurationListenerDEBUG configuration = null; public ControllerListenerDebug(ConfigurationListenerDEBUG configuration) { this.configuration = configuration; this.name = Shared.getMessage("Listener DEBUG"); this.setName(name); } public void println(Object object, boolean save) { mutexConnection.lockRead(); try { for (DebugConnection connection : lconnection) { connection.addMessage(object); } } catch (Exception e) { } mutexConnection.releaseRead(); if ((save == true) && (Shared.controllerDatabase != null)) { if (object instanceof Trace) { Trace trace = (Trace)object; try { if (trace.username == null) trace.username = "-"; TracePersistance tracePersistance = new TracePersistance(trace); ApplicationRealtime realtime = (ApplicationRealtime)Shared.controllerStatus.getApplication().getDeviceRealtime(); realtime.addTrace(trace); Shared.controllerDatabase.getHistoricalPersistance().get(0).addObject_asynchronous(tracePersistance); } catch (Exception exception) { } } } SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss.SSS")); System.out.println(formato1.format(System.currentTimeMillis()) + " : " + object); if (Shared.window != null) Shared.window.addTrace(object); } public void run() { Shared.traceInformation(name, "Starting"); while (isInterrupted() == false) { try { if (serverSocket == null) { serverSocket = new ServerSocket(configuration.port); serverSocket.setSoTimeout(0); } Socket clientSocket = serverSocket.accept(); connection(clientSocket); } catch (Exception exception) { try { sleep(100); } catch (Exception e) { } } } Shared.traceInformation(name, "Finishing"); } private void connection(Socket clientSocket) { try { if (configuration.allowed(clientSocket.getLocalAddress().getHostAddress())) { if (lconnection.size() < configuration.maximumConnections) { DebugConnection connection = new DebugConnection(clientSocket); addConnection(connection); connection.start(); return; } Shared.traceError(name, "Connecting", Shared.getMessage("Connection rejected due too many connections"), null); } else { Shared.traceWarning(name, "Connecting", clientSocket.getLocalAddress().getHostAddress(), Shared.getMessage("Connection rejected due too many connections")); } } catch (Exception e) { } try { clientSocket.close(); } catch (Exception e) { } } private void addConnection(DebugConnection connection) throws Exception { mutexConnection.lockWrite(); try { lconnection.add(connection); } catch (Exception e) { } mutexConnection.releaseWrite(); } private void removeConnection(DebugConnection connection) { mutexConnection.lockWrite(); try { lconnection.remove(connection); } catch (Exception e) { } mutexConnection.releaseWrite(); } private class DebugConnection extends Thread { private Socket socket = null; public List ltrace = new ArrayList(); public Mutex mutex = new Mutex(); public DebugConnection(Socket socket) { this.socket = socket; String address = socket.getInetAddress().getHostAddress(); String name = address + ":" + socket.getPort(); this.setName("DebugConnection " + name); } public void addMessage(Object object) { mutex.lockWrite(); { ltrace.add(object); } mutex.releaseWrite(); } public void run() { try { while (isInterrupted() == false) { if (ltrace.size() > 0) { String message = ltrace.get(0).toString() + "\r\n"; if (message != null) { socket.getOutputStream().write(message.getBytes()); } ltrace.remove(0); } else { sleep(100); } } } catch (Exception e) { } close(); } public void close() { try { socket.close(); } catch (Exception e) {} try { removeConnection(this); } catch (Exception e) {} } } }