Alejandro Acuña
2024-10-25 e51f4a713ed6e744c203c9493165584728a29c52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
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;
import java.util.ArrayList;
import java.util.List;
 
 
public class ControllerListenerDebug extends Thread
{
    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> connections = new ArrayList<DebugConnection>();
    private ConfigurationListenerDEBUG configuration = null;
    
    
    public ControllerListenerDebug(ConfigurationListenerDEBUG configuration)
    {
        this.configuration = configuration;
        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();
    }
 
    
 
    public void println(Object object, boolean save)
    {
        mutexConnection.lockRead();
        
        try
        {
            for (DebugConnection connection : connections) 
            {
                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);
                    Shared.controllerDatabase.getHistoricalPersistance().get(0).addObject_asynchronous(tracePersistance);
                    ApplicationRealtime realtime = (ApplicationRealtime)Shared.controllerStatus.getApplication().getDeviceRealtime();
                    realtime.addTrace(trace);
                }
                catch (Exception exception)
                {
                }
            }
        }        
        
        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 (connections.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"), "");
            }
            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
        {
            connections.add(connection);
        }
        catch (Exception e)
        {
        }
        
        mutexConnection.releaseWrite();
    }
    
    
 
    
    private void removeConnection(DebugConnection connection)
    {
        mutexConnection.lockWrite();
        
        try
        {
            connections.remove(connection);
        }
        catch (Exception e)
        {
        }
        
        mutexConnection.releaseWrite();
    }
        
 
    
    
    
    
    private class DebugConnection extends Thread
    {
        private Socket socket = null;
        private PrintStream output = null;
        public List<Object> messages = new ArrayList<Object>();
        public Mutex mutex = new Mutex();
        
        public DebugConnection(Socket socket) throws Exception
        {
            this.socket = socket;
            String address = socket.getInetAddress().getHostAddress();
            String name = address + ":" + socket.getPort();
            this.setName("Logger connection " + name);
            this.output = new PrintStream(socket.getOutputStream());
        }
        
        public DebugConnection(PrintStream output)
        {
            this.setName("Logger screen");
            this.output = output;
        }
        
        
        public void addMessage(Object object)
        {   
            mutex.lockWrite();
            {
               messages.add(object);
            }
            mutex.releaseWrite();
        }
 
 
        
        public void run()
        {   
            try
            {
                while (isInterrupted() == false)
                {
                    if (messages.size() > 0)
                    {
                        Object object = messages.get(0);
                        
                        if (object != null)
                        {
                            print(object);
                        }
                        
                        messages.remove(0);
                    }
                    else
                    {
                        sleep(100);
                    }
                }
            }
            catch (Exception e)
            {
            }
            
            close();
        }
        
 
 
        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) {}
            try { removeConnection(this); }  catch (Exception e) {}
        }            
        
    }
    
    
    
 
    
    
}