bck
Alejandro Acuña
2024-11-11 f1cb4443aede6d4657bdc3396c8914d3a9f4fa93
libraries/server/src/art/servers/controller/ControllerListenerDebug.java
@@ -1,11 +1,13 @@
package art.servers.controller;
import art.library.model.devices.application.ApplicationRealtime;
import art.library.model.transactions.traces.Note;
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.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
@@ -15,23 +17,43 @@
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 static String COLOR_BLACK = "\u001B[0;30;40m";
    private static String COLOR_RED = "\u001B[0;31;40m";
    private static String COLOR_GREEN = "\u001B[0;32;40m";
    private static String COLOR_YELLOW = "\u001B[0;33;40m";
    private static String COLOR_BLUE = "\u001B[0;34;40m";
    private static String COLOR_MAGENTA = "\u001B[0;35;40m";
    private static String COLOR_CYAN = "\u001B[0;36;40m";
    private static String COLOR_WHITE = "\u001B[0;37;40m";
    private static String COLOR_LIGHT_GRAY = "\u001B[0;33;37m";
    private static String COLOR_DARK_GRAY = "\u001B[0;33;90m";
    private static String COLOR_ORANGE = "\u001B[0;33;93m";
    
    
    private String name = null;
    private Mutex mutexConnection = new Mutex();
    private ServerSocket serverSocket = null;
    private List<DebugConnection> lconnection = new ArrayList<DebugConnection>();
    private List<DebugConnection> connections = new ArrayList<DebugConnection>();
    private ConfigurationListenerDEBUG configuration = null;
    
    
    public ControllerListenerDebug(ConfigurationListenerDEBUG configuration)
    {
        this.configuration = configuration;
        this.name = Shared.getMessage("Listener DEBUG");
        initialise();
    }
    private void initialise()
    {
        this.name = Shared.getMessage("Listener LOGGER");
        this.setName(name);
        // Terminal output
        DebugConnection connection = new DebugConnection(System.out);
        this.connections.add(connection);
        connection.start();
    }
    
    
@@ -42,7 +64,7 @@
        
        try
        {
            for (DebugConnection connection : lconnection)
            for (DebugConnection connection : connections)
            {
                connection.addMessage(object);
            }
@@ -63,9 +85,9 @@
                {
                    if (trace.username == null) trace.username = "-";
                    TracePersistance tracePersistance = new TracePersistance(trace);
                    Shared.controllerDatabase.getHistoricalPersistance().get(0).addObject_asynchronous(tracePersistance);
                    ApplicationRealtime realtime = (ApplicationRealtime)Shared.controllerStatus.getApplication().getDeviceRealtime();
                    realtime.addTrace(trace);
                    Shared.controllerDatabase.getHistoricalPersistance().get(0).addObject_asynchronous(tracePersistance);
                }
                catch (Exception exception)
                {
@@ -73,17 +95,11 @@
            }
        }        
        
        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");
@@ -119,6 +135,7 @@
    
    
    private void connection(Socket clientSocket)
    {
        try
@@ -126,7 +143,7 @@
            
            if (configuration.allowed(clientSocket.getLocalAddress().getHostAddress()))
            {
                if (lconnection.size() < configuration.maximumConnections)
                if (connections.size() < configuration.maximumConnections)
                {
                    DebugConnection connection = new DebugConnection(clientSocket);
                    addConnection(connection);
@@ -134,7 +151,7 @@
                    return;
                }
                
                Shared.traceError(name, "Connecting", Shared.getMessage("Connection rejected due too many connections"), null);
                Shared.traceError(name, "Connecting", Shared.getMessage("Connection rejected due too many connections"), "");
            }
            else
            {
@@ -158,16 +175,13 @@
    
    
    
    private void addConnection(DebugConnection connection) throws Exception
    {
        mutexConnection.lockWrite();
        
        try
        {
            lconnection.add(connection);
            connections.add(connection);
        }
        catch (Exception e)
        {
@@ -179,15 +193,13 @@
    
    
    private void removeConnection(DebugConnection connection)
    {
        mutexConnection.lockWrite();
        
        try
        {
            lconnection.remove(connection);
            connections.remove(connection);
        }
        catch (Exception e)
        {
@@ -204,15 +216,23 @@
    private class DebugConnection extends Thread
    {
        private Socket socket = null;
        public List<Object> ltrace = new ArrayList<Object>();
        private PrintStream output = null;
        public List<Object> messages = new ArrayList<Object>();
        public Mutex mutex = new Mutex();
        
        public DebugConnection(Socket socket)
        public DebugConnection(Socket socket) throws Exception
        {
            this.socket = socket;
            String address = socket.getInetAddress().getHostAddress();
            String name = address + ":" + socket.getPort();
            this.setName("DebugConnection " + name);
            this.setName("Logger connection " + name);
            this.output = new PrintStream(socket.getOutputStream());
        }
        public DebugConnection(PrintStream output)
        {
            this.setName("Logger screen");
            this.output = output;
        }
        
        
@@ -220,7 +240,7 @@
        {   
            mutex.lockWrite();
            {
               ltrace.add(object);
               messages.add(object);
            }
            mutex.releaseWrite();
        }
@@ -233,16 +253,16 @@
            {
                while (isInterrupted() == false)
                {
                    if (ltrace.size() > 0)
                    if (messages.size() > 0)
                    {
                        String message = ltrace.get(0).toString() + "\r\n";
                        Object object = messages.get(0);
                        
                        if (message != null)
                        if (object != null)
                        {
                            socket.getOutputStream().write(message.getBytes());
                            print(object);
                        }
                        
                        ltrace.remove(0);
                        messages.remove(0);
                    }
                    else
                    {
@@ -259,6 +279,84 @@
        
        
        
        private void print(Object object)
        {
            String color1 = COLOR_BLACK;
            String color2 = COLOR_BLACK;
            //if (out != System.out)
            {
                color1 = COLOR_WHITE;
                color2 = COLOR_WHITE;
            }
            if (object instanceof Trace)
            {
                Trace trace = (Trace)object;
                switch (trace.type)
                {
                    case Trace.TRACE_INFORMATION: color2 = COLOR_CYAN; break;
                    case Trace.TRACE_WARNING: color2 = COLOR_YELLOW; break;
                    case Trace.TRACE_ERROR: color2 = COLOR_RED; break;
                    case Trace.TRACE_SUCCESS: color2 = COLOR_GREEN; break;
                }
                print(trace, color1, color2);
            }
            else if (object instanceof Note)
            {
                Note note = (Note)object;
                switch (note.type)
                {
                    case Trace.TRACE_INFORMATION: color2 = COLOR_CYAN; break;
                    case Trace.TRACE_WARNING: color2 = COLOR_YELLOW; break;
                    case Trace.TRACE_ERROR: color2 = COLOR_RED; break;
                    case Trace.TRACE_SUCCESS: color2 = COLOR_GREEN; break;
                }
                print(note, color1, color2);
            }
        }
        private void print(Trace trace, String color1, String color2)
        {
            SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss.SSS"));
            output.println(color1 + formato1.format(trace.timestamp));
            output.print("{");
            if (trace.sourceComputer != null) output.print("\r\n\t" + color1 + "Source computer" + " : " + color2 + trace.sourceComputer);
            if (trace.destinationComputer != null) output.print("\r\n\t" + color1 + "Destination computer" + " : " + color2 + trace.destinationComputer);
            if (trace.username != null) output.print("\r\n\t" + color1 + "User" + " : " + color2 + trace.username);
            if (trace.application != null) output.print("\r\n\t" + color1 + "Application" + " : " + color2 + trace.application);
            if (trace.service != null) output.print("\r\n\t" + color1 + "Service" + " : " + color2 + trace.service);
            if (trace.action != null) output.print("\r\n\t" + color1 + "Action" + " : " + color2 + trace.action);
            if (trace.resource != null) output.print("\r\n\t" + color1 + "Resource" + " : " + color2 + trace.resource);
            if (trace.result != null) output.print("\r\n\t" + color1 + "Result" + " : " + color2 + trace.result);
            if ((trace.stack != null) && (trace.stack.length() > 0)) output.print("\r\n\t" + color1 + "Stack" + " : " + color2 + trace.stack);
            output.print(color1 + "\r\n}\r\n\r\n");
        }
        private void print(Note note, String color1, String color2)
        {
            SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss.SSS"));
            if (note.service != null)
            {
                output.println(color1 + formato1.format(note.timestamp) + " : " + COLOR_LIGHT_GRAY + note.service + " | " + color2 + note.message);
            }
            else
            {
                output.println(color1 + formato1.format(note.timestamp) + " : " + color2 + note.message);
            }
        }
        public void close()
        {
            try { socket.close(); }  catch (Exception e) {}
@@ -268,4 +366,8 @@
    }
    
    
}