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) {}
|
}
|
|
}
|
|
|
|
|
|
|
}
|