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.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
|
@ -37,9 +38,11 @@ public class NetLogClient extends Thread{
|
|||
|
||||
private ConcurrentLinkedQueue<NetLogListener> listeners;
|
||||
private Socket s;
|
||||
private ObjectOutputStream out;
|
||||
|
||||
public NetLogClient(String host, int port) throws UnknownHostException, IOException{
|
||||
s = new Socket(host, port);
|
||||
out = new ObjectOutputStream(s.getOutputStream());
|
||||
listeners = new ConcurrentLinkedQueue<NetLogListener>();
|
||||
this.start();
|
||||
}
|
||||
|
|
@ -51,7 +54,7 @@ public class NetLogClient extends Thread{
|
|||
|
||||
public void run(){
|
||||
try{
|
||||
ObjectInputStream in = new ObjectInputStream( s.getInputStream() );
|
||||
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
|
||||
while( true ){
|
||||
Object o = in.readObject();
|
||||
|
||||
|
|
|
|||
|
|
@ -53,35 +53,50 @@ public class NetLogExceptionMessage extends Message {
|
|||
}
|
||||
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
public void setCount(int count) {
|
||||
this.count = count;
|
||||
|
||||
@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 addCount(int add){
|
||||
count += add;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getStackTrace() {
|
||||
return stackTrace;
|
||||
}
|
||||
|
||||
public void setStackTrace(String stackTrace) {
|
||||
this.stackTrace = stackTrace;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,9 +28,6 @@ import java.util.logging.Logger;
|
|||
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.concurrent.Task;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.event.Event;
|
||||
import javafx.fxml.FXML;
|
||||
|
|
@ -43,7 +40,6 @@ import javafx.scene.control.ToggleButton;
|
|||
import javafx.scene.control.TableView;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.cell.PropertyValueFactory;
|
||||
import javafx.scene.layout.Priority;
|
||||
import javafx.util.Callback;
|
||||
|
||||
public class NetLogGuiClientInstance implements Initializable, NetLogListener {
|
||||
|
|
@ -54,13 +50,6 @@ public class NetLogGuiClientInstance implements Initializable, NetLogListener {
|
|||
private NetLogClient net;
|
||||
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
|
||||
@FXML private ToggleButton pauseButton;
|
||||
@FXML private Label logCountLabel;
|
||||
|
|
@ -106,23 +95,26 @@ public class NetLogGuiClientInstance implements Initializable, NetLogListener {
|
|||
exNameColumn.setCellValueFactory(new PropertyValueFactory<NetLogExceptionMessage, String>("name"));
|
||||
exMessageColumn.setCellValueFactory(new PropertyValueFactory<NetLogExceptionMessage, String>("message"));
|
||||
exStackTraceColumn.setCellValueFactory(new PropertyValueFactory<NetLogExceptionMessage, String>("stackTrace"));
|
||||
|
||||
//logTable.setItems(logData);
|
||||
exceptionTable.setItems(exceptionData);
|
||||
}
|
||||
|
||||
/************* NETWORK *****************/
|
||||
public void handleLogMessage(NetLogMessage log) {
|
||||
logTable.getItems().add( log );
|
||||
public void handleLogMessage(NetLogMessage msg) {
|
||||
if(status == Status.RUNNING){
|
||||
logTable.getItems().add(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public void handleExceptionMessage(NetLogExceptionMessage exception) {
|
||||
exceptionTable.getItems().add( exception );
|
||||
public void handleExceptionMessage(NetLogExceptionMessage msg) {
|
||||
if(status == Status.RUNNING){
|
||||
exceptionTable.getItems().remove(msg);
|
||||
exceptionTable.getItems().add(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public void handleStatusMessage(NetLogStatusMessage status) {
|
||||
// TODO Auto-generated method stub
|
||||
public void handleStatusMessage(NetLogStatusMessage msg) {
|
||||
if(status == Status.RUNNING){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*************** GUI *******************/
|
||||
|
|
|
|||
|
|
@ -22,10 +22,10 @@
|
|||
package zutil.log.net;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
|
|
@ -42,12 +42,14 @@ public class NetLogServer extends Handler {
|
|||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
private NetLogNetwork net;
|
||||
private ConcurrentHashMap<NetLogExceptionMessage,NetLogExceptionMessage> exceptions;
|
||||
|
||||
/**
|
||||
* @param port the port the server will listen on
|
||||
*/
|
||||
public NetLogServer(int port) {
|
||||
super();
|
||||
exceptions = new ConcurrentHashMap<NetLogExceptionMessage,NetLogExceptionMessage>();
|
||||
net = new NetLogNetwork(port);
|
||||
net.start();
|
||||
}
|
||||
|
|
@ -61,7 +63,17 @@ public class NetLogServer extends Handler {
|
|||
// Output the formatted data to the file
|
||||
if(record.getThrown() != null){
|
||||
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{
|
||||
NetLogMessage log = new NetLogMessage(record);
|
||||
|
|
@ -86,7 +98,7 @@ public class NetLogServer extends Handler {
|
|||
|
||||
public void sendMessage(Message log){
|
||||
for( NetLogServerThread thread : threads ){
|
||||
thread.queueMessage( log );
|
||||
thread.sendMessage( log );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -94,8 +106,6 @@ public class NetLogServer extends Handler {
|
|||
protected ThreadedTCPNetworkServerThread getThreadInstance(Socket s) {
|
||||
try {
|
||||
NetLogServerThread thread = new NetLogServerThread(s);
|
||||
logger.info("Client connection from: "+s.getInetAddress());
|
||||
threads.add( thread );
|
||||
return thread;
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "Unable to start Client thread", e);
|
||||
|
|
@ -105,33 +115,40 @@ public class NetLogServer extends Handler {
|
|||
|
||||
|
||||
class NetLogServerThread implements ThreadedTCPNetworkServerThread{
|
||||
private Queue<Message> queue;
|
||||
private ObjectOutputStream out;
|
||||
private ObjectInputStream in;
|
||||
private Socket s;
|
||||
|
||||
public NetLogServerThread(Socket s) throws IOException{
|
||||
queue = new LinkedList<Message>();
|
||||
this.s = s;
|
||||
logger.info("Client connected: "+s.getInetAddress());
|
||||
out = new ObjectOutputStream( s.getOutputStream() );
|
||||
in = new ObjectInputStream( s.getInputStream() );
|
||||
|
||||
sendAllExceptions();
|
||||
threads.add( this );
|
||||
}
|
||||
|
||||
public void queueMessage(Message log){
|
||||
synchronized(queue){
|
||||
queue.add( log );
|
||||
queue.notify();
|
||||
public void sendMessage(Message msg){
|
||||
try {
|
||||
out.writeObject( msg );
|
||||
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() {
|
||||
try {
|
||||
while( true ){
|
||||
synchronized(queue){
|
||||
while( !queue.isEmpty() ){
|
||||
Message msg = queue.poll();
|
||||
out.writeObject( msg );
|
||||
}
|
||||
queue.wait();
|
||||
}
|
||||
in.readObject();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
|
|
@ -143,10 +160,10 @@ public class NetLogServer extends Handler {
|
|||
|
||||
public void close(){
|
||||
try {
|
||||
threads.remove(this);
|
||||
logger.info("Client disconnected: "+s.getInetAddress());
|
||||
out.close();
|
||||
s.close();
|
||||
threads.remove(this);
|
||||
queue = null;
|
||||
} catch (IOException 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%);
|
||||
}
|
||||
.WARNING {
|
||||
-fx-control-inner-background: paleyellow;
|
||||
-fx-control-inner-background: yellow;
|
||||
-fx-accent: derive(-fx-control-inner-background, -40%);
|
||||
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
|
||||
}
|
||||
.INFO { }
|
||||
.FINE {
|
||||
-fx-control-inner-background: skyblue;
|
||||
-fx-control-inner-background: lavender;
|
||||
-fx-accent: derive(-fx-control-inner-background, -40%);
|
||||
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
|
||||
}
|
||||
.FINER {
|
||||
-fx-control-inner-background: skyblue;
|
||||
-fx-accent: derive(-fx-control-inner-background, -25%);
|
||||
-fx-control-inner-background: lightblue ;
|
||||
-fx-accent: derive(-fx-control-inner-background, -40%);
|
||||
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
|
||||
}
|
||||
.FINEST {
|
||||
-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%);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue