Skip to content

Commit

Permalink
isolate view from logic
Browse files Browse the repository at this point in the history
relates #3
  • Loading branch information
albfan committed Jun 18, 2016
1 parent 9ad11bf commit ae516aa
Show file tree
Hide file tree
Showing 13 changed files with 763 additions and 551 deletions.
17 changes: 7 additions & 10 deletions src/main/java/net/sf/sockettest/MyTrustManager.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package net.sf.sockettest;

import net.sf.sockettest.swing.AskView;

import javax.net.ssl.*;
import java.security.cert.*;
import java.awt.*;
import javax.swing.*;
import java.security.*;

Expand All @@ -13,13 +14,13 @@
*/
public class MyTrustManager implements X509TrustManager {

private Component parentComponent;
private AskView view;
private X509TrustManager sunJSSEX509TrustManager;



public MyTrustManager(Component parentComponent) throws Exception {
this.parentComponent = parentComponent;
public MyTrustManager(AskView view) throws Exception {
this.view = view;

TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE");

Expand Down Expand Up @@ -84,13 +85,9 @@ public void checkServerTrusted(X509Certificate[] chain, String authType)
} else {
sb.append("\n Issued By:\n \t"+chain[i].getSubjectDN());
}
}

int option = JOptionPane.showConfirmDialog(parentComponent,
sb.toString(), "Certificate Confirmation",
JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
};

if(option==JOptionPane.NO_OPTION) {
if(view.confirm("Certificate Confirmation", sb.toString(), JOptionPane.NO_OPTION)) {
throw new CertificateException("Not Trusted Certificate!");
}
}
Expand Down
40 changes: 21 additions & 19 deletions src/main/java/net/sf/sockettest/SocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.net.*;
import java.io.*;
import net.sf.sockettest.swing.SocketTestClient;

import net.sf.sockettest.controller.SocketTestClientController;
import net.sf.sockettest.swing.SocketTestClientView;
import org.apache.commons.lang3.StringEscapeUtils;

import javax.xml.bind.DatatypeConverter;
Expand All @@ -14,36 +16,38 @@
public class SocketClient extends Thread {

private static SocketClient socketClient=null;
private SocketTestClientController controller;
private Socket socket=null;
private SocketTestClient parent;
private SocketTestClientView view;
private BufferedInputStream in;
private boolean disconnected =false;

public synchronized void setDisconnected(boolean cr) {
disconnected =cr;
}

private SocketClient(SocketTestClient parent, Socket s) {
private SocketClient(SocketTestClientView view, SocketTestClientController controller, Socket s) {
super("SocketClient");
this.parent = parent;
this.view = view;
this.controller = controller;
socket=s;
setDisconnected(false);
start();
}

public static synchronized SocketClient handle(SocketTestClient parent, Socket s) {
public static synchronized SocketClient handle(SocketTestClientView view, SocketTestClientController controller, Socket s) {
if(socketClient==null)
socketClient=new SocketClient(parent, s);
socketClient=new SocketClient(view, controller, s);
else {
if(socketClient.socket!=null) {
try {
socketClient.socket.close();
} catch (Exception e) {
parent.error(e.getMessage());
view.error(e.getMessage());
}
}
socketClient.socket=null;
socketClient=new SocketClient(parent,s);
socketClient=new SocketClient(view, controller, s);
}
return socketClient;
}
Expand All @@ -59,37 +63,36 @@ public void run() {
} catch(IOException e2) {
System.err.println("Socket not closed :"+e2);
}
parent.error("Could not open socket : "+e.getMessage());
parent.disconnect();
view.error("Could not open socket : "+e.getMessage());
controller.disconnect();
return;
}

while(!disconnected) {
try {
String got = readInputStream(in);
if(got==null) {
parent.disconnect();
controller.disconnect();
break;
}
if(parent.isHexOutput()) {
if(view.isHexOutput()) {
got = DatatypeConverter.printHexBinary(got.getBytes());
}
parent.append(got);
view.appendMessage(got);
} catch(IOException e) {
if(!disconnected) {
parent.error(e.getMessage(),"Connection lost");
parent.disconnect();
view.error(e.getMessage(),"Connection lost");
controller.disconnect();
}
break;
}
}//end of while
}
try {
is.close();
in.close();
//socket.close();
} catch (Exception err) {}
socket=null;
}//end of run
}

private static String readInputStream(BufferedInputStream _in) throws IOException {
String data = "";
Expand All @@ -106,5 +109,4 @@ private static String readInputStream(BufferedInputStream _in) throws IOExceptio
}
return StringEscapeUtils.unescapeJava(data);
}

}
70 changes: 36 additions & 34 deletions src/main/java/net/sf/sockettest/SocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.net.*;
import java.io.*;
import net.sf.sockettest.swing.SocketTestServer;
import net.sf.sockettest.swing.SocketTestServerView;
import org.apache.commons.lang3.StringEscapeUtils;

import javax.xml.bind.DatatypeConverter;
Expand All @@ -15,13 +16,13 @@ public class SocketServer extends Thread {

private static SocketServer socketServer = null;
private Socket socket = null;
private final SocketTestServerView view;
private final SocketTextServerController controller;
private ServerSocket server = null;
private SocketTestServer parent;
private BufferedInputStream in;
private boolean desonnected=false;
private boolean stop = false;

//disconnect client

public synchronized void setDesconnected(boolean cr) {
if(socket!=null && cr) {
try {
Expand All @@ -33,7 +34,7 @@ public synchronized void setDesconnected(boolean cr) {
desonnected=cr;
//parent.setClientSocket(null);
}
//stop server

public synchronized void setStop(boolean cr) {
stop=cr;
if(server!=null && cr) {
Expand All @@ -45,9 +46,10 @@ public synchronized void setStop(boolean cr) {
}
}

private SocketServer(SocketTestServer parent, ServerSocket s) {
private SocketServer(SocketTestServerView view, SocketTextServerController controller, ServerSocket s) {
super("SocketServer");
this.parent = parent;
this.view = view;
this.controller = controller;
server=s;
setStop(false);
setDesconnected(false);
Expand All @@ -56,10 +58,10 @@ private SocketServer(SocketTestServer parent, ServerSocket s) {



public static synchronized SocketServer handle(SocketTestServer parent,
public static synchronized SocketServer handle(SocketTestServerView view, SocketTextServerController controller,
ServerSocket s) {
if(socketServer==null)
socketServer=new SocketServer(parent, s);
socketServer=new SocketServer(view, controller, s);
else {
if(socketServer.server!=null) {
try {
Expand All @@ -70,12 +72,12 @@ public static synchronized SocketServer handle(SocketTestServer parent,
if(socketServer.server!=null)
socketServer.server.close();
} catch (Exception e) {
parent.error(e.getMessage());
view.error(e.getMessage());
}
}
socketServer.server = null;
socketServer.socket = null;
socketServer=new SocketServer(parent,s);
socketServer=new SocketServer(view, controller, s);
}
return socketServer;
}
Expand All @@ -86,7 +88,7 @@ public void run() {
socket = server.accept();
} catch (Exception e) {
if(!stop) {
parent.error(e.getMessage(),"Error acception connection");
view.error(e.getMessage(),"Error acception connection");
stop=true;
}
continue;
Expand All @@ -99,64 +101,64 @@ public void run() {
System.err.println("Erro closing client socket : "+e);
}
socket=null;
parent.setClientSocket(socket);
controller.setClientSocket(socket);
}
}//end of while
}//end of run
}
}

private void startServer() {
parent.setClientSocket(socket);
controller.setClientSocket(socket);
InputStream is = null;
parent.append("> New Client: "+socket.getInetAddress().getHostAddress());
view.appendMessage("> New Client: "+socket.getInetAddress().getHostAddress());
try {
is = socket.getInputStream();
in = new BufferedInputStream(is);
} catch(IOException e) {
parent.append("> Cound't open input stream on Clinet "+e.getMessage());
view.appendMessage("> Cound't open input stream on Clinet "+e.getMessage());
setDesconnected(true);
return;
}

String rec=null;
String rec;
while(true) {
rec=null;
try {
rec = readInputStream(in);//in.readLine();
try {
rec = readInputStream(in);
} catch (Exception e) {
setDesconnected(true);
if(!desonnected) {
parent.error(e.getMessage(),"Lost Client conection");
parent.append("> Server lost Client conection.");
view.error(e.getMessage(),"Lost Client conection");
view.appendMessage("> Server lost Client conection.");
} else
parent.append("> Server closed Client conection.");
view.appendMessage("> Server closed Client conection.");
break;
}

if (rec != null) {
if(parent.isHexOutput()) {
if(view.isHexOutput()) {
rec = DatatypeConverter.printHexBinary(rec.getBytes());
}
parent.append(rec);
view.appendMessage(rec);
} else {
setDesconnected(true);
parent.append("> Client closed conection.");
view.appendMessage("> Client closed conection.");
break;
}
} //end of while
} //end of startServer
}
}

private static String readInputStream(BufferedInputStream _in)
private static String readInputStream(BufferedInputStream in)
throws IOException {
String data = "";
int s = _in.read();
if(s==-1)
int s = in.read();
if(s == -1) {
return null;
}
data += ""+(char)s;
int len = _in.available();
int len = in.available();
System.out.println("Len got : "+len);
if(len > 0) {
byte[] byteData = new byte[len];
_in.read(byteData);
in.read(byteData);
data += new String(byteData);
}
return StringEscapeUtils.unescapeJava(data);
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/net/sf/sockettest/SocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.awt.*;
import javax.swing.*;

import net.sf.sockettest.controller.SocketTestClientController;
import net.sf.sockettest.swing.About;
import net.sf.sockettest.swing.SocketTestClient;
import net.sf.sockettest.swing.SocketTestServer;
Expand All @@ -24,14 +25,16 @@ public class SocketTest extends JFrame {
/** Creates a new instance of SocketTest */
public SocketTest() {
Container cp = getContentPane();

tabbedPane = new JTabbedPane(JTabbedPane.TOP);
SocketTestClient client = new SocketTestClient(this);
SocketTestServer server = new SocketTestServer(this);
SocketTestUdp udp = new SocketTestUdp(this);
SocketTestClient client = new SocketTestClient().setParent(this);
client.setController(new SocketTestClientController());
SocketTestServer server = new SocketTestServer().setParent(this);
server.setController(new SocketTextServerController());
SocketTestUdp udp = new SocketTestUdp().setParent(this);
About about = new About();

tabbedPane.addTab("Client", ball, (Component)client, "Test any server");
tabbedPane.addTab("Client", ball, client, "Test any server");
tabbedPane.addTab("Server", ball, server, "Test any client");
tabbedPane.addTab("Udp", ball, udp, "Test any UDP Client or Server");
tabbedPane.addTab("About", ball, about, "About SocketTest");
Expand Down
Loading

0 comments on commit ae516aa

Please sign in to comment.