Fixed some network issues
This commit is contained in:
parent
6dd346eee7
commit
bfcab133de
5 changed files with 92 additions and 65 deletions
|
|
@ -23,6 +23,7 @@ package zutil.log.net;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
@ -37,9 +38,11 @@ public class NetLogClient extends Thread{
|
||||||
|
|
||||||
private ConcurrentLinkedQueue<NetLogListener> listeners;
|
private ConcurrentLinkedQueue<NetLogListener> listeners;
|
||||||
private Socket s;
|
private Socket s;
|
||||||
|
private ObjectOutputStream out;
|
||||||
|
|
||||||
public NetLogClient(String host, int port) throws UnknownHostException, IOException{
|
public NetLogClient(String host, int port) throws UnknownHostException, IOException{
|
||||||
s = new Socket(host, port);
|
s = new Socket(host, port);
|
||||||
|
out = new ObjectOutputStream(s.getOutputStream());
|
||||||
listeners = new ConcurrentLinkedQueue<NetLogListener>();
|
listeners = new ConcurrentLinkedQueue<NetLogListener>();
|
||||||
this.start();
|
this.start();
|
||||||
}
|
}
|
||||||
|
|
@ -51,7 +54,7 @@ public class NetLogClient extends Thread{
|
||||||
|
|
||||||
public void run(){
|
public void run(){
|
||||||
try{
|
try{
|
||||||
ObjectInputStream in = new ObjectInputStream( s.getInputStream() );
|
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
|
||||||
while( true ){
|
while( true ){
|
||||||
Object o = in.readObject();
|
Object o = in.readObject();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,35 +53,50 @@ public class NetLogExceptionMessage extends Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public int getCount() {
|
@Override
|
||||||
return count;
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||||
|
result = prime * result + ((message == null) ? 0 : message.hashCode());
|
||||||
|
result = prime * result
|
||||||
|
+ ((stackTrace == null) ? 0 : stackTrace.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
NetLogExceptionMessage other = (NetLogExceptionMessage) obj;
|
||||||
|
if (name.equals(other.name) || message.equals(other.message) ||
|
||||||
|
stackTrace.equals(other.stackTrace)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCount(int count) {
|
public void addCount(int add){
|
||||||
this.count = count;
|
count += add;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCount() {
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMessage() {
|
public String getMessage() {
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMessage(String message) {
|
|
||||||
this.message = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getStackTrace() {
|
public String getStackTrace() {
|
||||||
return stackTrace;
|
return stackTrace;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStackTrace(String stackTrace) {
|
|
||||||
this.stackTrace = stackTrace;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,9 +28,6 @@ import java.util.logging.Logger;
|
||||||
|
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
|
|
||||||
import javafx.collections.FXCollections;
|
|
||||||
import javafx.collections.ObservableList;
|
|
||||||
import javafx.concurrent.Task;
|
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
import javafx.event.Event;
|
import javafx.event.Event;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
|
|
@ -43,7 +40,6 @@ import javafx.scene.control.ToggleButton;
|
||||||
import javafx.scene.control.TableView;
|
import javafx.scene.control.TableView;
|
||||||
import javafx.scene.control.Label;
|
import javafx.scene.control.Label;
|
||||||
import javafx.scene.control.cell.PropertyValueFactory;
|
import javafx.scene.control.cell.PropertyValueFactory;
|
||||||
import javafx.scene.layout.Priority;
|
|
||||||
import javafx.util.Callback;
|
import javafx.util.Callback;
|
||||||
|
|
||||||
public class NetLogGuiClientInstance implements Initializable, NetLogListener {
|
public class NetLogGuiClientInstance implements Initializable, NetLogListener {
|
||||||
|
|
@ -54,13 +50,6 @@ public class NetLogGuiClientInstance implements Initializable, NetLogListener {
|
||||||
private NetLogClient net;
|
private NetLogClient net;
|
||||||
private Status status;
|
private Status status;
|
||||||
|
|
||||||
private final ObservableList<NetLogExceptionMessage> exceptionData =
|
|
||||||
FXCollections.observableArrayList(
|
|
||||||
new NetLogExceptionMessage("java.lang.NullPointerException", "", " at com.example.myproject.Book.getTitle(Book.java:16) \n at com.example.myproject.Author.getBookTitles(Author.java:25) \n at com.example.myproject.Bootstrap.main(Bootstrap.java:14)"),
|
|
||||||
new NetLogExceptionMessage("java.lang.NullPointerException", "", " at com.example.myproject.Book.getTitle(Book.java:16) \n at com.example.myproject.Author.getBookTitles(Author.java:25) \n at com.example.myproject.Bootstrap.main(Bootstrap.java:14)"),
|
|
||||||
new NetLogExceptionMessage("java.io.FileNotFoundException", "fred.txt", " at java.io.FileInputStream.<init>(FileInputStream.java) \n at java.io.FileInputStream.<init>(FileInputStream.java) \n at ExTest.readMyFile(ExTest.java:19) \n at ExTest.main(ExTest.java:7)")
|
|
||||||
);
|
|
||||||
|
|
||||||
// UI elements
|
// UI elements
|
||||||
@FXML private ToggleButton pauseButton;
|
@FXML private ToggleButton pauseButton;
|
||||||
@FXML private Label logCountLabel;
|
@FXML private Label logCountLabel;
|
||||||
|
|
@ -106,23 +95,26 @@ public class NetLogGuiClientInstance implements Initializable, NetLogListener {
|
||||||
exNameColumn.setCellValueFactory(new PropertyValueFactory<NetLogExceptionMessage, String>("name"));
|
exNameColumn.setCellValueFactory(new PropertyValueFactory<NetLogExceptionMessage, String>("name"));
|
||||||
exMessageColumn.setCellValueFactory(new PropertyValueFactory<NetLogExceptionMessage, String>("message"));
|
exMessageColumn.setCellValueFactory(new PropertyValueFactory<NetLogExceptionMessage, String>("message"));
|
||||||
exStackTraceColumn.setCellValueFactory(new PropertyValueFactory<NetLogExceptionMessage, String>("stackTrace"));
|
exStackTraceColumn.setCellValueFactory(new PropertyValueFactory<NetLogExceptionMessage, String>("stackTrace"));
|
||||||
|
|
||||||
//logTable.setItems(logData);
|
|
||||||
exceptionTable.setItems(exceptionData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/************* NETWORK *****************/
|
/************* NETWORK *****************/
|
||||||
public void handleLogMessage(NetLogMessage log) {
|
public void handleLogMessage(NetLogMessage msg) {
|
||||||
logTable.getItems().add( log );
|
if(status == Status.RUNNING){
|
||||||
|
logTable.getItems().add(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleExceptionMessage(NetLogExceptionMessage exception) {
|
public void handleExceptionMessage(NetLogExceptionMessage msg) {
|
||||||
exceptionTable.getItems().add( exception );
|
if(status == Status.RUNNING){
|
||||||
|
exceptionTable.getItems().remove(msg);
|
||||||
|
exceptionTable.getItems().add(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleStatusMessage(NetLogStatusMessage status) {
|
public void handleStatusMessage(NetLogStatusMessage msg) {
|
||||||
// TODO Auto-generated method stub
|
if(status == Status.RUNNING){
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************** GUI *******************/
|
/*************** GUI *******************/
|
||||||
|
|
|
||||||
|
|
@ -22,10 +22,10 @@
|
||||||
package zutil.log.net;
|
package zutil.log.net;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.LinkedList;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.Queue;
|
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
import java.util.logging.Handler;
|
import java.util.logging.Handler;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
@ -40,14 +40,16 @@ import zutil.net.threaded.ThreadedTCPNetworkServerThread;
|
||||||
|
|
||||||
public class NetLogServer extends Handler {
|
public class NetLogServer extends Handler {
|
||||||
private static final Logger logger = LogUtil.getLogger();
|
private static final Logger logger = LogUtil.getLogger();
|
||||||
|
|
||||||
private NetLogNetwork net;
|
private NetLogNetwork net;
|
||||||
|
private ConcurrentHashMap<NetLogExceptionMessage,NetLogExceptionMessage> exceptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param port the port the server will listen on
|
* @param port the port the server will listen on
|
||||||
*/
|
*/
|
||||||
public NetLogServer(int port) {
|
public NetLogServer(int port) {
|
||||||
super();
|
super();
|
||||||
|
exceptions = new ConcurrentHashMap<NetLogExceptionMessage,NetLogExceptionMessage>();
|
||||||
net = new NetLogNetwork(port);
|
net = new NetLogNetwork(port);
|
||||||
net.start();
|
net.start();
|
||||||
}
|
}
|
||||||
|
|
@ -61,7 +63,17 @@ public class NetLogServer extends Handler {
|
||||||
// Output the formatted data to the file
|
// Output the formatted data to the file
|
||||||
if(record.getThrown() != null){
|
if(record.getThrown() != null){
|
||||||
NetLogExceptionMessage exception = new NetLogExceptionMessage(record);
|
NetLogExceptionMessage exception = new NetLogExceptionMessage(record);
|
||||||
net.sendMessage( exception );
|
if(!exceptions.containsKey(exception)){
|
||||||
|
logger.finest("Received new exception: "+exception);
|
||||||
|
exceptions.put(exception, exception);
|
||||||
|
net.sendMessage( exception );
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
exception = exceptions.get(exception);
|
||||||
|
exception.addCount(1);
|
||||||
|
logger.finest("Received known exception(Count: "+exception.getCount()+"): "+exception);
|
||||||
|
net.sendMessage( exception );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
NetLogMessage log = new NetLogMessage(record);
|
NetLogMessage log = new NetLogMessage(record);
|
||||||
|
|
@ -86,7 +98,7 @@ public class NetLogServer extends Handler {
|
||||||
|
|
||||||
public void sendMessage(Message log){
|
public void sendMessage(Message log){
|
||||||
for( NetLogServerThread thread : threads ){
|
for( NetLogServerThread thread : threads ){
|
||||||
thread.queueMessage( log );
|
thread.sendMessage( log );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -94,8 +106,6 @@ public class NetLogServer extends Handler {
|
||||||
protected ThreadedTCPNetworkServerThread getThreadInstance(Socket s) {
|
protected ThreadedTCPNetworkServerThread getThreadInstance(Socket s) {
|
||||||
try {
|
try {
|
||||||
NetLogServerThread thread = new NetLogServerThread(s);
|
NetLogServerThread thread = new NetLogServerThread(s);
|
||||||
logger.info("Client connection from: "+s.getInetAddress());
|
|
||||||
threads.add( thread );
|
|
||||||
return thread;
|
return thread;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.log(Level.SEVERE, "Unable to start Client thread", e);
|
logger.log(Level.SEVERE, "Unable to start Client thread", e);
|
||||||
|
|
@ -105,33 +115,40 @@ public class NetLogServer extends Handler {
|
||||||
|
|
||||||
|
|
||||||
class NetLogServerThread implements ThreadedTCPNetworkServerThread{
|
class NetLogServerThread implements ThreadedTCPNetworkServerThread{
|
||||||
private Queue<Message> queue;
|
|
||||||
private ObjectOutputStream out;
|
private ObjectOutputStream out;
|
||||||
|
private ObjectInputStream in;
|
||||||
private Socket s;
|
private Socket s;
|
||||||
|
|
||||||
public NetLogServerThread(Socket s) throws IOException{
|
public NetLogServerThread(Socket s) throws IOException{
|
||||||
queue = new LinkedList<Message>();
|
|
||||||
this.s = s;
|
this.s = s;
|
||||||
|
logger.info("Client connected: "+s.getInetAddress());
|
||||||
out = new ObjectOutputStream( s.getOutputStream() );
|
out = new ObjectOutputStream( s.getOutputStream() );
|
||||||
|
in = new ObjectInputStream( s.getInputStream() );
|
||||||
|
|
||||||
|
sendAllExceptions();
|
||||||
|
threads.add( this );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void queueMessage(Message log){
|
public void sendMessage(Message msg){
|
||||||
synchronized(queue){
|
try {
|
||||||
queue.add( log );
|
out.writeObject( msg );
|
||||||
queue.notify();
|
out.reset();
|
||||||
|
} catch (Exception e) {
|
||||||
|
this.close();
|
||||||
|
logger.log(Level.SEVERE, "Unable to send message to client: "+s.getInetAddress(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sendAllExceptions(){
|
||||||
|
logger.fine("Sending all exceptions to client: "+s.getInetAddress());
|
||||||
|
for(NetLogExceptionMessage e : exceptions.values())
|
||||||
|
sendMessage(e);
|
||||||
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
while( true ){
|
while( true ){
|
||||||
synchronized(queue){
|
in.readObject();
|
||||||
while( !queue.isEmpty() ){
|
|
||||||
Message msg = queue.poll();
|
|
||||||
out.writeObject( msg );
|
|
||||||
}
|
|
||||||
queue.wait();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.log(Level.SEVERE, null, e);
|
logger.log(Level.SEVERE, null, e);
|
||||||
|
|
@ -143,10 +160,10 @@ public class NetLogServer extends Handler {
|
||||||
|
|
||||||
public void close(){
|
public void close(){
|
||||||
try {
|
try {
|
||||||
|
threads.remove(this);
|
||||||
|
logger.info("Client disconnected: "+s.getInetAddress());
|
||||||
out.close();
|
out.close();
|
||||||
s.close();
|
s.close();
|
||||||
threads.remove(this);
|
|
||||||
queue = null;
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.log(Level.SEVERE, "Unable to close Client Socket", e);
|
logger.log(Level.SEVERE, "Unable to close Client Socket", e);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,24 +5,24 @@
|
||||||
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
|
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
|
||||||
}
|
}
|
||||||
.WARNING {
|
.WARNING {
|
||||||
-fx-control-inner-background: paleyellow;
|
-fx-control-inner-background: yellow;
|
||||||
-fx-accent: derive(-fx-control-inner-background, -40%);
|
-fx-accent: derive(-fx-control-inner-background, -40%);
|
||||||
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
|
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
|
||||||
}
|
}
|
||||||
.INFO { }
|
.INFO { }
|
||||||
.FINE {
|
.FINE {
|
||||||
-fx-control-inner-background: skyblue;
|
-fx-control-inner-background: lavender;
|
||||||
-fx-accent: derive(-fx-control-inner-background, -40%);
|
-fx-accent: derive(-fx-control-inner-background, -40%);
|
||||||
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
|
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
|
||||||
}
|
}
|
||||||
.FINER {
|
.FINER {
|
||||||
-fx-control-inner-background: skyblue;
|
-fx-control-inner-background: lightblue ;
|
||||||
-fx-accent: derive(-fx-control-inner-background, -25%);
|
-fx-accent: derive(-fx-control-inner-background, -40%);
|
||||||
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
|
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
|
||||||
}
|
}
|
||||||
.FINEST {
|
.FINEST {
|
||||||
-fx-control-inner-background: skyblue;
|
-fx-control-inner-background: skyblue;
|
||||||
-fx-accent: derive(-fx-control-inner-background, -10%);
|
-fx-accent: derive(-fx-control-inner-background, -40%);
|
||||||
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
|
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue