diff --git a/.gitignore b/.gitignore index 3094469..fd02173 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store build +target/ diff --git a/build.xml b/build.xml index 9f899fe..b8af99d 100644 --- a/build.xml +++ b/build.xml @@ -5,16 +5,16 @@ - + - + - + diff --git a/compile b/compile new file mode 100755 index 0000000..b422696 --- /dev/null +++ b/compile @@ -0,0 +1,4 @@ +#!/bin/bash + +mvn clean install +mvn dependency:copy-dependencies diff --git a/execute b/execute new file mode 100755 index 0000000..58dac53 --- /dev/null +++ b/execute @@ -0,0 +1,3 @@ +#!/bin/bash + +$JAVA_HOME/bin/java -cp target/SocketTest-3.0-SNAPSHOT.jar:target/dependency/* net.sf.sockettest.SocketTest diff --git a/lib/metouia.jar b/lib/metouia.jar deleted file mode 100644 index 16afe7b..0000000 Binary files a/lib/metouia.jar and /dev/null differ diff --git a/lib/swt-awt-win32-3111.dll b/lib/swt-awt-win32-3111.dll deleted file mode 100644 index 3144c83..0000000 Binary files a/lib/swt-awt-win32-3111.dll and /dev/null differ diff --git a/lib/swt-win32-3111.dll b/lib/swt-win32-3111.dll deleted file mode 100644 index bfe7054..0000000 Binary files a/lib/swt-win32-3111.dll and /dev/null differ diff --git a/lib/swt.jar b/lib/swt.jar deleted file mode 100644 index f3cec51..0000000 Binary files a/lib/swt.jar and /dev/null differ diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml deleted file mode 100644 index 1be3df8..0000000 --- a/nbproject/build-impl.xml +++ /dev/null @@ -1,1041 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set src.dir - Must set test.src.dir - Must set build.dir - Must set dist.dir - Must set build.classes.dir - Must set dist.javadoc.dir - Must set build.test.classes.dir - Must set build.test.results.dir - Must set build.classes.excludes - Must set dist.jar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set javac.includes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set JVM to use for profiling in profiler.info.jvm - Must set profiler agent JVM arguments in profiler.info.jvmargs.agent - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select some files in the IDE or set javac.includes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - To run this application from the command line without Ant, try: - - - - - - - java -cp "${run.classpath.with.dist.jar}" ${main.class} - - - - - - - - - - - - - - - - - - - - - - - - - To run this application from the command line without Ant, try: - - java -jar "${dist.jar.resolved}" - - - - - - - - - - - - - - - - - - - - - - - - - Must select one file in the IDE or set run.class - - - - Must select one file in the IDE or set run.class - - - - - - - - - - - - - - - - - - - - - - - Must select one file in the IDE or set debug.class - - - - - Must select one file in the IDE or set debug.class - - - - - Must set fix.includes - - - - - - - - - - - - - - - - - Must select one file in the IDE or set profile.class - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select some files in the IDE or set javac.includes - - - - - - - - - - - - - - - - - - - - Some tests failed; see details above. - - - - - - - - - Must select some files in the IDE or set test.includes - - - - Some tests failed; see details above. - - - - - Must select one file in the IDE or set test.class - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select one file in the IDE or set applet.url - - - - - - - - - Must select one file in the IDE or set applet.url - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties deleted file mode 100644 index 47f9678..0000000 --- a/nbproject/genfiles.properties +++ /dev/null @@ -1,8 +0,0 @@ -build.xml.data.CRC32=cba644ff -build.xml.script.CRC32=74386eec -build.xml.stylesheet.CRC32=ba5d3624 -# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. -# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=94bef83f -nbproject/build-impl.xml.script.CRC32=c8383d25 -nbproject/build-impl.xml.stylesheet.CRC32=0c01fd8e@1.43.1.45 diff --git a/nbproject/private/cache/.refactorit/cache.bin b/nbproject/private/cache/.refactorit/cache.bin deleted file mode 100644 index 9b53429..0000000 Binary files a/nbproject/private/cache/.refactorit/cache.bin and /dev/null differ diff --git a/nbproject/private/config.properties b/nbproject/private/config.properties deleted file mode 100644 index e69de29..0000000 diff --git a/nbproject/private/private.properties b/nbproject/private/private.properties deleted file mode 100644 index fbceaf3..0000000 --- a/nbproject/private/private.properties +++ /dev/null @@ -1,9 +0,0 @@ -application.args= -compile.on.save=false -do.depend=false -do.jar=true -javac.debug=true -javadoc.preview=true -user.properties.file=C:\\Users\\akshath\\.netbeans\\7.0\\build.properties -default.javac.source=1.3 -default.javac.target=1.3 diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml deleted file mode 100644 index c1f155a..0000000 --- a/nbproject/private/private.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/nbproject/project.properties b/nbproject/project.properties deleted file mode 100644 index 4282d60..0000000 --- a/nbproject/project.properties +++ /dev/null @@ -1,85 +0,0 @@ -annotation.processing.enabled=true -annotation.processing.enabled.in.editor=false -annotation.processing.run.all.processors=true -annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output -application.args= -application.title=SocketTest -application.vendor=Akshathkumar Shetty -build.classes.dir=${build.dir}/classes -build.classes.excludes=**/*.java,**/*.form -# This directory is removed when the project is cleaned: -build.dir=build -build.generated.dir=${build.dir}/generated -build.generated.sources.dir=${build.dir}/generated-sources -# Only compile against the classpath explicitly listed here: -build.sysclasspath=ignore -build.test.classes.dir=${build.dir}/test/classes -build.test.results.dir=${build.dir}/test/results -debug.classpath=\ - ${run.classpath} -debug.test.classpath=\ - ${run.test.classpath} -# This directory is removed when the project is cleaned: -dist.dir=dist -dist.jar=${dist.dir}/SocketTest.jar -dist.javadoc.dir=${dist.dir}/javadoc -endorsed.classpath= -excludes= -file.reference.metouia.jar=lib/metouia.jar -file.reference.swt.jar=lib/swt.jar -includes=** -jar.archive.disabled=${jnlp.enabled} -jar.compress=false -jar.index=${jnlp.enabled} -javac.classpath=\ - ${file.reference.swt.jar}:\ - ${file.reference.metouia.jar} -# Space-separated list of extra javac options -javac.compilerargs= -javac.deprecation=false -javac.processorpath=\ - ${javac.classpath} -javac.source=1.3 -javac.target=1.1 -javac.test.classpath=\ - ${javac.classpath}:\ - ${build.classes.dir}:\ - ${libs.junit.classpath} -javac.test.processorpath=${javac.test.classpath} -javadoc.additionalparam= -javadoc.author=false -javadoc.encoding= -javadoc.noindex=false -javadoc.nonavbar=false -javadoc.notree=false -javadoc.private=false -javadoc.splitindex=true -javadoc.use=true -javadoc.version=false -javadoc.windowtitle= -jnlp.codebase.type=no.codebase -jnlp.descriptor=application -jnlp.enabled=false -jnlp.mixed.code=default -jnlp.offline-allowed=false -jnlp.signed=false -jnlp.signing= -jnlp.signing.alias= -jnlp.signing.keystore= -main.class=net.sf.sockettest.SocketTest -manifest.file=manifest.mf -meta.inf.dir=${src.dir}/META-INF -mkdist.disabled=false -platform.active=default_platform -run.classpath=\ - ${javac.classpath}:\ - ${build.classes.dir} -# Space-separated list of JVM arguments used when running the project -# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value -# or test-sys-prop.name=value to set system properties for unit tests): -run.jvmargs= -run.test.classpath=\ - ${javac.test.classpath}:\ - ${build.test.classes.dir} -src.dir=src -test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml deleted file mode 100644 index d79e72f..0000000 --- a/nbproject/project.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - org.netbeans.modules.java.j2seproject - - - SocketTest - - - - - - - - - diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a22a0b1 --- /dev/null +++ b/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + + net.sf.sockettest + SocketTest + 3.0-SNAPSHOT + jar + + SocketTest + http://maven.apache.org + + + UTF-8 + + + + + + net.sf.metouia + metouia + 1.0.0 + + + org.apache.commons + commons-lang3 + 3.4 + + + junit + junit + 4.11 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + diff --git a/src/net/sf/sockettest/MyTrustManager.java b/src/main/java/net/sf/sockettest/MyTrustManager.java similarity index 87% rename from src/net/sf/sockettest/MyTrustManager.java rename to src/main/java/net/sf/sockettest/MyTrustManager.java index d6446eb..5f98519 100644 --- a/src/net/sf/sockettest/MyTrustManager.java +++ b/src/main/java/net/sf/sockettest/MyTrustManager.java @@ -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.*; @@ -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"); @@ -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!"); } } diff --git a/src/net/sf/sockettest/PortModel.java b/src/main/java/net/sf/sockettest/PortModel.java similarity index 100% rename from src/net/sf/sockettest/PortModel.java rename to src/main/java/net/sf/sockettest/PortModel.java diff --git a/src/net/sf/sockettest/SocketClient.java b/src/main/java/net/sf/sockettest/SocketClient.java similarity index 56% rename from src/net/sf/sockettest/SocketClient.java rename to src/main/java/net/sf/sockettest/SocketClient.java index c07ea59..fb12534 100644 --- a/src/net/sf/sockettest/SocketClient.java +++ b/src/main/java/net/sf/sockettest/SocketClient.java @@ -2,7 +2,13 @@ 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; + /** * * @author Akshathkumar Shetty @@ -10,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 desonnected=false; + private boolean disconnected =false; - public synchronized void setDesonnected(boolean cr) { - desonnected=cr; + 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; - setDesonnected(false); + 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; } @@ -55,38 +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(!desonnected) { + while(!disconnected) { try { - String got = readInputStream(in); //in.readLine(); + String got = readInputStream(in); if(got==null) { - //parent.error("Connection closed by client"); - parent.disconnect(); + controller.disconnect(); break; } - //got = got.replaceAll("\n",""); - //got = got.replaceAll("\r",""); - //parent.append("R: "+got); - parent.appendnoNewLine(got); + if(view.isHexOutput()) { + got = DatatypeConverter.printHexBinary(got.getBytes()); + } + view.appendMessage(got); } catch(IOException e) { - if(!desonnected) { - parent.error(e.getMessage(),"Connection lost"); - parent.disconnect(); + if(!disconnected) { + 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 = ""; @@ -101,7 +107,6 @@ private static String readInputStream(BufferedInputStream _in) throws IOExceptio _in.read(byteData); data += new String(byteData); } - return data; + return StringEscapeUtils.unescapeJava(data); } - } diff --git a/src/net/sf/sockettest/SocketServer.java b/src/main/java/net/sf/sockettest/SocketServer.java similarity index 58% rename from src/net/sf/sockettest/SocketServer.java rename to src/main/java/net/sf/sockettest/SocketServer.java index 3c436ba..5ac238c 100644 --- a/src/net/sf/sockettest/SocketServer.java +++ b/src/main/java/net/sf/sockettest/SocketServer.java @@ -3,6 +3,10 @@ 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; /** * @@ -12,28 +16,28 @@ 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 setDesonnected(boolean cr) { - if(socket!=null && cr==true) { + + public synchronized void setDesconnected(boolean cr) { + if(socket!=null && cr) { try { socket.close(); } catch (Exception e) { - System.err.println("Error closing clinet : setDesonnected : "+e); + System.err.println("Error closing clinet : setDesconnected : "+e); } } desonnected=cr; //parent.setClientSocket(null); } - //stop server + public synchronized void setStop(boolean cr) { stop=cr; - if(server!=null && cr==true) { + if(server!=null && cr) { try { server.close(); } catch (Exception e) { @@ -42,37 +46,38 @@ 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); - setDesonnected(false); + setDesconnected(false); start(); } - 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 { - socketServer.setDesonnected(true); + socketServer.setDesconnected(true); socketServer.setStop(true); if(socketServer.socket!=null) socketServer.socket.close(); 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; } @@ -83,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; @@ -96,66 +101,66 @@ 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()); - setDesonnected(true); + 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) { - setDesonnected(true); + 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) { - //rec = rec.replaceAll("\n",""); - //rec = rec.replaceAll("\r",""); - //parent.append("R: "+rec); - parent.appendnoNewLine(rec); + if(view.isHexOutput()) { + rec = DatatypeConverter.printHexBinary(rec.getBytes()); + } + view.appendMessage(rec); } else { - setDesonnected(true); - parent.append("> Client closed conection."); + setDesconnected(true); + 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 data; + return StringEscapeUtils.unescapeJava(data); } } diff --git a/src/net/sf/sockettest/SocketTest.java b/src/main/java/net/sf/sockettest/SocketTest.java similarity index 82% rename from src/net/sf/sockettest/SocketTest.java rename to src/main/java/net/sf/sockettest/SocketTest.java index d0ad4d8..48e3837 100644 --- a/src/net/sf/sockettest/SocketTest.java +++ b/src/main/java/net/sf/sockettest/SocketTest.java @@ -1,11 +1,9 @@ package net.sf.sockettest; import java.awt.*; -import java.applet.*; -import java.awt.event.*; import javax.swing.*; -import javax.swing.event.*; -import javax.swing.border.*; + +import net.sf.sockettest.controller.SocketTestClientController; import net.sf.sockettest.swing.About; import net.sf.sockettest.swing.SocketTestClient; import net.sf.sockettest.swing.SocketTestServer; @@ -27,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"); @@ -50,7 +50,6 @@ public static void main(String[] args) { try { UIManager.setLookAndFeel("net.sourceforge.mlf.metouia.MetouiaLookAndFeel"); } catch(Exception e) { - //e.printStackTrace(); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception ee) { diff --git a/src/main/java/net/sf/sockettest/SocketTextServerController.java b/src/main/java/net/sf/sockettest/SocketTextServerController.java new file mode 100644 index 0000000..70a3078 --- /dev/null +++ b/src/main/java/net/sf/sockettest/SocketTextServerController.java @@ -0,0 +1,174 @@ +package net.sf.sockettest; + +import net.sf.sockettest.swing.SocketTestServerView; +import org.apache.commons.lang3.StringEscapeUtils; + +import javax.swing.*; +import javax.xml.bind.DatatypeConverter; +import java.io.BufferedWriter; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; + +public class SocketTextServerController { + private Socket socket; + private ServerSocket server; + private SocketServer socketServer; + private PrintWriter out; + + private SocketTestServerView view; + + public void setView(SocketTestServerView view) { + this.view = view; + } + + public void connect(String ip, String port) { + if(server!=null) { + stop(); + return; + } + if(ip ==null || ip.equals("")) { + view.error("No IP Address. Please enter IP Address", + "Error connecting"); + view.focusOnIp(); + return; + } + if(port ==null || port.equals("")) { + view.error("No Port number. Please enter Port number", + "Error connecting"); + view.focusOnPort(); + return; + } + view.startWaitInfo(); + if(!Util.checkHost(ip)) { + view.error("Bad IP Address", + "Error connecting"); + view.focusOnIp(); + view.stopWaitInfo(); + return; + } + int portNo; + try { + portNo=Integer.parseInt(port); + } catch (Exception e) { + view.error("Bad Port number. Please enter Port number", + "Error connecting"); + view.focusOnPort(); + view.stopWaitInfo(); + return; + } + try { + InetAddress bindAddr; + if(!ip.equals("0.0.0.0")) + bindAddr = InetAddress.getByName(ip); + else + bindAddr = null; + server = new ServerSocket(portNo,1,bindAddr); + + view.connected(); + } catch (Exception e) { + view.error(e.getMessage(), "Starting Server at "+portNo); + view.stopWaitInfo(); + return; + } + view.stopWaitInfo(); + view.appendMessage("> Server Started on Port: "+portNo); + view.appendMessage("> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + socketServer = SocketServer.handle(view, this, server); + } + + public void sendMessage(String s) { + view.startWaitInfo(); + try { + if(out==null) { + out = new PrintWriter(new BufferedWriter( + new OutputStreamWriter(socket.getOutputStream())), true); + } + if (view.isHexOutput()) { + view.appendMessage("S: " + DatatypeConverter.printHexBinary(s.getBytes())); + } else { + view.appendMessage("S: " + s); + } + out.print(StringEscapeUtils.escapeJava(s)); + out.flush(); + view.resetSendField(); + view.stopWaitInfo(); + } catch (Exception e) { + view.stopWaitInfo(); + view.error(e.getMessage(),"Error Sending Message"); + disconnect(); + } + } + + public void buildMessage(String text) { + String msg; + if (view.isHexInput()) { + try { + msg = new String(DatatypeConverter.parseHexBinary(text)); + } catch (Exception ex) { + view.error(ex.getMessage()); + return; + } + } else { + msg = StringEscapeUtils.unescapeJava(text); + } + + if(!msg.equals("")) + sendMessage(msg); + else { + if (view.confirm("Send Data To Client", "Send Blank Line ?", JOptionPane.YES_OPTION)) { + sendMessage(msg); + } + } + } + + public void saveFile(String text) { + if (text.equals("")) { + view.error("Nothing to save", "Save to file"); + return; + } + String fileName = view.chooseFile(); + if (fileName != null) { + try { + Util.writeFile(fileName, text); + } catch (Exception ioe) { + view.error("" + ioe.getMessage(), + "Error saving to file.."); + } + } + } + + public synchronized void disconnect() { + try { + socketServer.setDesconnected(true); + } catch (Exception e) {} + } + + public synchronized void stop() { + try { + disconnect(); //close any client + socketServer.setStop(true); + } catch (Exception e) {} + server=null; + view.stopped(); + view.appendMessage("> Server stopped"); + view.appendMessage("> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + } + + public synchronized void setClientSocket(Socket s) { + + if(s==null) { + out=null; + socket = null; + view.showConnectionInfo("NONE"); + view.socketSet(false); + } else { + socket = s; + view.showConnectionInfo(" "+socket.getInetAddress().getHostName()+ + " ["+socket.getInetAddress().getHostAddress()+"] "); + view.socketSet(true); + } + } +} diff --git a/src/net/sf/sockettest/UdpServer.java b/src/main/java/net/sf/sockettest/UdpServer.java similarity index 99% rename from src/net/sf/sockettest/UdpServer.java rename to src/main/java/net/sf/sockettest/UdpServer.java index 980b22b..b7f5d4b 100644 --- a/src/net/sf/sockettest/UdpServer.java +++ b/src/main/java/net/sf/sockettest/UdpServer.java @@ -1,7 +1,7 @@ package net.sf.sockettest; import java.net.*; -import java.io.*; + import net.sf.sockettest.swing.SocketTestUdp; /** * diff --git a/src/net/sf/sockettest/Util.java b/src/main/java/net/sf/sockettest/Util.java similarity index 100% rename from src/net/sf/sockettest/Util.java rename to src/main/java/net/sf/sockettest/Util.java diff --git a/src/main/java/net/sf/sockettest/controller/SocketTestClientController.java b/src/main/java/net/sf/sockettest/controller/SocketTestClientController.java new file mode 100644 index 0000000..716977c --- /dev/null +++ b/src/main/java/net/sf/sockettest/controller/SocketTestClientController.java @@ -0,0 +1,176 @@ +package net.sf.sockettest.controller; + +import net.sf.sockettest.MyTrustManager; +import net.sf.sockettest.SocketClient; +import net.sf.sockettest.Util; +import net.sf.sockettest.swing.SocketTestClientView; +import org.apache.commons.lang3.StringEscapeUtils; + +import javax.net.ssl.KeyManager; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.swing.*; +import javax.xml.bind.DatatypeConverter; +import java.io.BufferedWriter; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.Socket; +import java.security.SecureRandom; + +import static sun.management.Agent.error; + +public class SocketTestClientController { + private boolean secure; + private Socket socket; + private PrintWriter out; + private SocketClient socketClient; + private SocketTestClientView view; + + public SocketTestClientController() { + secure = false; + } + + public void setView(SocketTestClientView view) { + this.view = view; + } + + public void toggleSecure() { + secure = !secure; + } + + public void connect(String ip, String port) { + if(socket!=null) { + disconnect(); + return; + } + if(ip ==null || ip.equals("")) { + error("No IP Address. Please enter IP Address", + "Error connecting"); + view.focusOnIp(); + return; + } + if(port ==null || port.equals("")) { + error("No Port number. Please enter Port number", + "Error connecting"); + view.focusOnPort(); + return; + } + view.startWaitInfo(); + if(!Util.checkHost(ip)) { + error("Bad IP Address", + "Error connecting"); + view.focusOnIp(); + view.stopWaitInfo(); + return; + } + int portNo; + try { + portNo=Integer.parseInt(port); + } catch (Exception e) { + error("Bad Port number. Please enter Port number", + "Error connecting"); + view.focusOnPort(); + view.stopWaitInfo(); + return; + } + try { + if(!secure) { + System.out.println("Connecting in normal mode : "+ ip +":"+portNo); + socket = new Socket(ip,portNo); + } else { + System.out.println("Connecting in secure mode : "+ ip +":"+portNo); + //SocketFactory factory = SSLSocketFactory.getDefault(); + + TrustManager[] tm = new TrustManager[] { new MyTrustManager(view) }; + + SSLContext context = SSLContext.getInstance("TLS"); + context.init(new KeyManager[0], tm, new SecureRandom()); + + SSLSocketFactory factory = context.getSocketFactory(); + socket = factory.createSocket(ip,portNo); + } + + view.connected(); + } catch (Exception e) { + e.printStackTrace(); + error(e.getMessage(), "Opening connection"); + view.stopWaitInfo(); + return; + } + view.showConectionInfo(" "+socket.getInetAddress().getHostName()+ + " ["+socket.getInetAddress().getHostAddress()+"] "); + view.stopWaitInfo(); + view.clearMessages(); + socketClient = SocketClient.handle(view, this, socket); + view.focusSendField(); + } + + public synchronized void disconnect() { + try { + socketClient.setDisconnected(true); + socket.close(); + } catch (Exception e) { + System.err.println("Error closing client : "+e); + } + socket=null; + out=null; + view.disconnected(); + } + + public void buildMessage(String text, boolean hexInput) { + String msg; + if (hexInput) { + try { + msg = new String(DatatypeConverter.parseHexBinary(text)); + } catch (Exception ex) { + error(ex.getMessage()); + return; + } + } else { + msg = StringEscapeUtils.unescapeJava(text); + } + if(!msg.equals("")) + sendMessage(msg); + else { + if (view.confirm("Send Data To Server", "Send Blank Line ?", JOptionPane.YES_OPTION)) { + sendMessage(msg); + } + } + } + + public void sendMessage(String s) { + view.startWaitInfo(); + try { + if(out==null) { + out = new PrintWriter(new BufferedWriter( + new OutputStreamWriter(socket.getOutputStream())), true); + } + if (view.isHexOutput()) { + view.appendMessage("S: " + DatatypeConverter.printHexBinary(s.getBytes())); + } else { + view.appendMessage("S: "+s); + } + out.print(StringEscapeUtils.escapeJava(s)); + out.flush(); + view.resetSend(); + view.stopWaitInfo(); + } catch (Exception e) { + view.stopWaitInfo(); + error(e.getMessage(),"Error Sending Message"); + disconnect(); + } + } + + public void saveMessages() { + String text = view.getMessages(); + if(text.equals("")) { + error("Nothing to save","Save to file"); + return; + } + String fileName = view.chooseFile(); + if (fileName != null) { + view.saveText(text, fileName); + } + } +} diff --git a/src/net/sf/sockettest/swing/About.java b/src/main/java/net/sf/sockettest/swing/About.java similarity index 96% rename from src/net/sf/sockettest/swing/About.java rename to src/main/java/net/sf/sockettest/swing/About.java index 2b107cb..0e37b36 100644 --- a/src/net/sf/sockettest/swing/About.java +++ b/src/main/java/net/sf/sockettest/swing/About.java @@ -1,10 +1,7 @@ package net.sf.sockettest.swing; import java.awt.*; -import java.awt.event.*; import javax.swing.*; -import javax.swing.event.*; -import javax.swing.border.*; import java.io.IOException; import net.sf.sockettest.*; @@ -12,7 +9,7 @@ * About Tab * @author Akshathkumar Shetty */ -public class About extends JPanel /*JFrame*/ { +public class About extends JPanel { private ClassLoader cl = getClass().getClassLoader(); public ImageIcon logo = new ImageIcon( cl.getResource("icons/logo.gif")); @@ -139,7 +136,7 @@ public About() { centerPanel = new JPanel(); readme.setText("Loading... readme"); try { - String cont = Util.readFile("readme.txt",(Object)About.this); + String cont = Util.readFile("readme.txt", About.this); readme.setText(cont); } catch (IOException e){ System.err.println("Error reading readme.txt "+e); diff --git a/src/main/java/net/sf/sockettest/swing/AskView.java b/src/main/java/net/sf/sockettest/swing/AskView.java new file mode 100644 index 0000000..ec98513 --- /dev/null +++ b/src/main/java/net/sf/sockettest/swing/AskView.java @@ -0,0 +1,9 @@ +package net.sf.sockettest.swing; + +public interface AskView { + boolean confirm(String title, String message, int option); + + void error(String error); + + void error(String error, String heading); +} diff --git a/src/net/sf/sockettest/swing/PortDialog.java b/src/main/java/net/sf/sockettest/swing/PortDialog.java similarity index 94% rename from src/net/sf/sockettest/swing/PortDialog.java rename to src/main/java/net/sf/sockettest/swing/PortDialog.java index e34eb84..69319ec 100644 --- a/src/net/sf/sockettest/swing/PortDialog.java +++ b/src/main/java/net/sf/sockettest/swing/PortDialog.java @@ -1,9 +1,8 @@ package net.sf.sockettest.swing; import javax.swing.*; -import javax.swing.table.*; import java.awt.*; -import java.io.IOException; + import net.sf.sockettest.PortModel; import net.sf.sockettest.*; diff --git a/src/net/sf/sockettest/swing/SocketTestClient.java b/src/main/java/net/sf/sockettest/swing/SocketTestClient.java similarity index 51% rename from src/net/sf/sockettest/swing/SocketTestClient.java rename to src/main/java/net/sf/sockettest/swing/SocketTestClient.java index d656398..f9a03f7 100644 --- a/src/net/sf/sockettest/swing/SocketTestClient.java +++ b/src/main/java/net/sf/sockettest/swing/SocketTestClient.java @@ -3,28 +3,22 @@ import java.awt.*; import java.awt.event.*; import javax.swing.*; -import javax.swing.event.*; import javax.swing.border.*; -import java.security.SecureRandom; - -import java.net.*; import java.io.*; -import javax.net.*; -import javax.net.ssl.*; - import net.sf.sockettest.*; +import net.sf.sockettest.controller.SocketTestClientController; + /** * * @author Akshathkumar Shetty */ -public class SocketTestClient extends JPanel { +public class SocketTestClient extends JPanel implements SocketTestClientView { private final String NEW_LINE = "\r\n"; private ClassLoader cl = getClass().getClassLoader(); - public ImageIcon logo = new ImageIcon( - cl.getResource("icons/logo.gif")); + public ImageIcon logo = new ImageIcon(cl.getResource("icons/logo.gif")); private JPanel topPanel; private JPanel toPanel; @@ -44,7 +38,7 @@ public class SocketTestClient extends JPanel { private JButton connectButton = new JButton("Connect"); private JLabel convLabel = new JLabel("Conversation with host"); - private Border connectedBorder = BorderFactory.createTitledBorder(new EtchedBorder(),"Connected To < NONE >"); + private Border connectedBorder; private JTextArea messagesField = new JTextArea(); private JLabel sendLabel = new JLabel("Message"); @@ -55,19 +49,25 @@ public class SocketTestClient extends JPanel { private JButton clearButton = new JButton("Clear"); private JCheckBox secureButton = new JCheckBox("Secure"); - private boolean isSecure = false; + private JCheckBox hexInputCheckBox = new JCheckBox("Hex Input"); + private JCheckBox hexOutputCheckBox = new JCheckBox("Hex Output"); private GridBagConstraints gbc = new GridBagConstraints(); - - private Socket socket; - private PrintWriter out; - private SocketClient socketClient; - protected final JFrame parent; - - public SocketTestClient(final JFrame parent) { - //Container cp = getContentPane(); - this.parent = parent; + private JFrame parent; + private SocketTestClientController controller; + + public SocketTestClient() { + buildGUI(); + showConectionInfo("NONE"); + } + + public void setController(SocketTestClientController controller) { + this.controller = controller; + controller.setView(this); + } + + private void buildGUI() { Container cp = this; - + topPanel = new JPanel(); toPanel = new JPanel(); toPanel.setLayout(new GridBagLayout()); @@ -81,39 +81,38 @@ public SocketTestClient(final JFrame parent) { gbc.anchor = GridBagConstraints.EAST; gbc.fill = GridBagConstraints.NONE; toPanel.add(ipLabel, gbc); - + gbc.weightx = 1.0; //streach gbc.gridx = 1; gbc.gridwidth = 4; gbc.fill = GridBagConstraints.HORIZONTAL; - ActionListener ipListener = new ActionListener() { + ipField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { portField.requestFocus(); } - }; - ipField.addActionListener(ipListener); + }); toPanel.add(ipField, gbc); - + gbc.weightx = 0.0; gbc.gridy = 1; gbc.gridx = 0; gbc.gridwidth = 1; gbc.fill = GridBagConstraints.NONE; toPanel.add(portLabel, gbc); - + gbc.weightx = 1.0; gbc.gridy = 1; gbc.gridx = 1; gbc.gridwidth = 1; gbc.fill = GridBagConstraints.HORIZONTAL; - ActionListener connectListener = new ActionListener() { + ActionListener connectActionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { - connect(); + controller.connect(ipField.getText(), portField.getText()); } }; - portField.addActionListener(connectListener); + portField.addActionListener(connectActionListener); toPanel.add(portField, gbc); - + gbc.weightx = 0.0; gbc.gridy = 1; gbc.gridx = 2; @@ -121,15 +120,14 @@ public void actionPerformed(ActionEvent e) { gbc.fill = GridBagConstraints.NONE; portButton.setMnemonic('P'); portButton.setToolTipText("View Standard Ports"); - ActionListener portButtonListener = new ActionListener() { + portButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - PortDialog dia = new PortDialog(parent, PortDialog.TCP); - dia.show(); + PortDialog dialog = new PortDialog(parent, PortDialog.TCP); + dialog.setVisible(true); } - }; - portButton.addActionListener(portButtonListener); + }); toPanel.add(portButton, gbc); - + gbc.weightx = 0.0; gbc.gridy = 1; gbc.gridx = 3; @@ -137,25 +135,24 @@ public void actionPerformed(ActionEvent e) { gbc.fill = GridBagConstraints.NONE; connectButton.setMnemonic('C'); connectButton.setToolTipText("Start Connection"); - connectButton.addActionListener(connectListener); + connectButton.addActionListener(connectActionListener); toPanel.add(connectButton, gbc); - - + + gbc.weightx = 0.0; gbc.gridy = 1; gbc.gridx = 4; gbc.gridwidth = 1; gbc.fill = GridBagConstraints.NONE; secureButton.setToolTipText("Set Has Secure"); - secureButton.addItemListener( - new ItemListener() { + secureButton.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { - isSecure = !isSecure; + controller.toggleSecure(); } }); toPanel.add(secureButton, gbc); - - + + toPanel.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Connect To")); topPanel.setLayout(new BorderLayout(10,0)); topPanel.add(toPanel); @@ -163,8 +160,8 @@ public void itemStateChanged(ItemEvent e) { logoLabel.setHorizontalTextPosition(JLabel.CENTER); topPanel.add(logoLabel,BorderLayout.EAST); topPanel.setBorder(BorderFactory.createEmptyBorder(10,10,5,10)); - - + + textPanel = new JPanel(); textPanel.setLayout(new BorderLayout(0,5)); textPanel.add(convLabel,BorderLayout.NORTH); @@ -172,7 +169,8 @@ public void itemStateChanged(ItemEvent e) { JScrollPane jsp = new JScrollPane(messagesField); textPanel.add(jsp); textPanel.setBorder(BorderFactory.createEmptyBorder(3,3,0,3)); - + textPanel.add(hexOutputCheckBox, BorderLayout.SOUTH); + sendPanel = new JPanel(); sendPanel.setLayout(new GridBagLayout()); gbc.weighty = 0.0; @@ -196,17 +194,9 @@ public void itemStateChanged(ItemEvent e) { sendButton.setToolTipText("Send text to host"); ActionListener sendListener = new ActionListener() { public void actionPerformed(ActionEvent e) { - String msg = sendField.getText(); - if(!msg.equals("")) - sendMessage(msg); - else { - int value = JOptionPane.showConfirmDialog( - SocketTestClient.this, "Send Blank Line ?", - "Send Data To Server", - JOptionPane.YES_NO_OPTION); - if (value == JOptionPane.YES_OPTION) - sendMessage(msg); - } + String text = sendField.getText(); + boolean hexInput = hexInputCheckBox.isSelected(); + controller.buildMessage(text, hexInput); } }; sendButton.addActionListener(sendListener); @@ -216,7 +206,10 @@ public void actionPerformed(ActionEvent e) { new CompoundBorder( BorderFactory.createEmptyBorder(0,0,0,3), BorderFactory.createTitledBorder("Send"))); - + + gbc.gridx = 1; + gbc.gridy = 1; + sendPanel.add(hexInputCheckBox, gbc); buttonPanel = new JPanel(); buttonPanel.setLayout(new GridBagLayout()); gbc.weighty = 0.0; @@ -237,26 +230,7 @@ public void actionPerformed(ActionEvent e) { saveButton.setMnemonic('S'); ActionListener saveListener = new ActionListener() { public void actionPerformed(ActionEvent e) { - String text = messagesField.getText(); - if(text.equals("")) { - error("Nothing to save","Save to file"); - return; - } - String fileName=""; - JFileChooser chooser = new JFileChooser(); - chooser.setCurrentDirectory(new File(".")); - int returnVal = chooser.showSaveDialog(SocketTestClient.this); - if(returnVal == JFileChooser.APPROVE_OPTION) { - fileName=chooser.getSelectedFile().getAbsolutePath(); - try { - Util.writeFile(fileName,text); - } catch (Exception ioe) { - JOptionPane.showMessageDialog(SocketTestClient.this, - ""+ioe.getMessage(), - "Error saving to file..", - JOptionPane.ERROR_MESSAGE); - } - } + controller.saveMessages(); } }; saveButton.addActionListener(saveListener); @@ -266,145 +240,95 @@ public void actionPerformed(ActionEvent e) { clearButton.setMnemonic('C'); ActionListener clearListener = new ActionListener() { public void actionPerformed(ActionEvent e) { - messagesField.setText(""); + clearMessages(); } }; clearButton.addActionListener(clearListener); buttonPanel.add(clearButton, gbc); buttonPanel.setBorder(BorderFactory.createEmptyBorder(3,0,0,3)); - + centerPanel = new JPanel(); centerPanel.setLayout(new BorderLayout(0,10)); centerPanel.add(buttonPanel,BorderLayout.SOUTH); centerPanel.add(textPanel,BorderLayout.CENTER); - + CompoundBorder cb=new CompoundBorder( BorderFactory.createEmptyBorder(5,10,10,10), connectedBorder); centerPanel.setBorder(cb); - + cp.setLayout(new BorderLayout(10,0)); cp.add(topPanel,BorderLayout.NORTH); cp.add(centerPanel,BorderLayout.CENTER); } - - /* - public static void main(String args[]) { - SocketTestClient client=new SocketTestClient(); - client.setTitle("SocketTest Client"); - //client.pack(); - client.setSize(500,400); - Util.centerWindow(client); - client.setDefaultCloseOperation(EXIT_ON_CLOSE); - client.setIconImage(client.logo.getImage()); - client.setVisible(true); - } - */ - - ///////////////// - //action methods - ////////////////// - private void connect() { - if(socket!=null) { - disconnect(); - return; - } - String ip=ipField.getText(); - String port=portField.getText(); - if(ip==null || ip.equals("")) { - JOptionPane.showMessageDialog(SocketTestClient.this, - "No IP Address. Please enter IP Address", - "Error connecting", JOptionPane.ERROR_MESSAGE); - ipField.requestFocus(); - ipField.selectAll(); - return; - } - if(port==null || port.equals("")) { - JOptionPane.showMessageDialog(SocketTestClient.this, - "No Port number. Please enter Port number", - "Error connecting", JOptionPane.ERROR_MESSAGE); - portField.requestFocus(); - portField.selectAll(); - return; - } - setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); - if(!Util.checkHost(ip)) { - JOptionPane.showMessageDialog(SocketTestClient.this, - "Bad IP Address", - "Error connecting", JOptionPane.ERROR_MESSAGE); - ipField.requestFocus(); - ipField.selectAll(); - setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - return; - } - int portNo = 0; - try { - portNo=Integer.parseInt(port); - } catch (Exception e) { - JOptionPane.showMessageDialog(SocketTestClient.this, - "Bad Port number. Please enter Port number", - "Error connecting", JOptionPane.ERROR_MESSAGE); - portField.requestFocus(); - portField.selectAll(); - setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - return; + + public void clearMessages() { + messagesField.setText(""); + } + + public String chooseFile() { + String fileName = null; + JFileChooser chooser = new JFileChooser(); + chooser.setCurrentDirectory(new File(".")); + int returnVal = chooser.showSaveDialog(SocketTestClient.this); + if(returnVal == JFileChooser.APPROVE_OPTION) { + fileName = chooser.getSelectedFile().getAbsolutePath(); } + return fileName; + } + + public void saveText(String text, String fileName) { try { - if(isSecure==false) { - System.out.println("Connectig in normal mode : "+ip+":"+portNo); - socket = new Socket(ip,portNo); - } else { - System.out.println("Connectig in secure mode : "+ip+":"+portNo); - //SocketFactory factory = SSLSocketFactory.getDefault(); - - TrustManager[] tm = new TrustManager[] { new MyTrustManager(SocketTestClient.this) }; - - SSLContext context = SSLContext.getInstance("TLS"); - context.init(new KeyManager[0], tm, new SecureRandom()); - - SSLSocketFactory factory = context.getSocketFactory(); - socket = factory.createSocket(ip,portNo); - } - - ipField.setEditable(false); - portField.setEditable(false); - connectButton.setText("Disconnect"); - connectButton.setMnemonic('D'); - connectButton.setToolTipText("Stop Connection"); - sendButton.setEnabled(true); - sendField.setEditable(true); - } catch (Exception e) { - e.printStackTrace(); - error(e.getMessage(), "Opening connection"); - setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - return; + Util.writeFile(fileName, text); + } catch (Exception ioe) { + error(""+ioe.getMessage(), "Error saving to file.."); } - changeBorder(" "+socket.getInetAddress().getHostName()+ - " ["+socket.getInetAddress().getHostAddress()+"] "); - setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - messagesField.setText(""); - socketClient=SocketClient.handle(this,socket); + } + + public String getMessages() { + return messagesField.getText(); + } + + public SocketTestClient setParent(JFrame parent) { + this.parent = parent; + return this; + } + + public boolean confirm(String title, String message, int option) { + return JOptionPane.showConfirmDialog(this, message, + title, + JOptionPane.YES_NO_OPTION) == option; + } + + public void focusSendField() { sendField.requestFocus(); } - - public synchronized void disconnect() { - try { - socketClient.setDesonnected(true); - socket.close(); - } catch (Exception e) { - System.err.println("Error closing client : "+e); - } - socket=null; - out=null; - changeBorder(null); - ipField.setEditable(true); - portField.setEditable(true); - connectButton.setText("Connect"); - connectButton.setMnemonic('C'); - connectButton.setToolTipText("Start Connection"); - sendButton.setEnabled(false); - sendField.setEditable(false); + + public void connected() { + ipField.setEditable(false); + portField.setEditable(false); + connectButton.setText("Disconnect"); + connectButton.setMnemonic('D'); + connectButton.setToolTipText("Stop Connection"); + sendButton.setEnabled(true); + sendField.setEditable(true); + } + + public void focusOnPort() { + portField.requestFocus(); + portField.selectAll(); + } + + public void focusOnIp() { + ipField.requestFocus(); + ipField.selectAll(); + } + + public void startWaitInfo() { + setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); } + + @Override public void error(String error) { if(error==null || error.equals("")) return; @@ -412,6 +336,7 @@ public void error(String error) { error, "Error", JOptionPane.ERROR_MESSAGE); } + @Override public void error(String error, String heading) { if(error==null || error.equals("")) return; @@ -421,42 +346,16 @@ public void error(String error, String heading) { public void append(String msg) { messagesField.append(msg+NEW_LINE); - messagesField.setCaretPosition(messagesField.getText().length()); - } - - public void appendnoNewLine(String msg) { - messagesField.append(msg); - messagesField.setCaretPosition(messagesField.getText().length()); + messagesField.setCaretPosition(getMessages().length()); } - - public void sendMessage(String s) { - setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); - try { - if(out==null) { - out = new PrintWriter(new BufferedWriter( - new OutputStreamWriter(socket.getOutputStream())), true); - } - append("S: "+s); - out.print(s+NEW_LINE); - out.flush(); - sendField.setText(""); - setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - } catch (Exception e) { - setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - JOptionPane.showMessageDialog(SocketTestClient.this, - e.getMessage(),"Error Sending Message", - JOptionPane.ERROR_MESSAGE); - disconnect(); - } + + public void stopWaitInfo() { + setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } - - private void changeBorder(String ip) { - if(ip==null || ip.equals("")) - connectedBorder = BorderFactory.createTitledBorder( - new EtchedBorder(), "Connected To < NONE >"); - else - connectedBorder = BorderFactory.createTitledBorder( - new EtchedBorder(), "Connected To < "+ip+" >"); + + public void showConectionInfo(String ip) { + connectedBorder = BorderFactory.createTitledBorder( + new EtchedBorder(), "Connected To < "+ip+" >"); CompoundBorder cb=new CompoundBorder( BorderFactory.createEmptyBorder(5,10,10,10), connectedBorder); @@ -464,5 +363,27 @@ private void changeBorder(String ip) { invalidate(); repaint(); } - -} + + public void disconnected() { + showConectionInfo("NONE"); + ipField.setEditable(true); + portField.setEditable(true); + connectButton.setText("Connect"); + connectButton.setMnemonic('C'); + connectButton.setToolTipText("Start Connection"); + sendButton.setEnabled(false); + sendField.setEditable(false); + } + + public boolean isHexOutput() { + return hexOutputCheckBox.isSelected(); + } + + public void appendMessage(String message) { + append(message); + } + + public void resetSend() { + sendField.setText(""); + } +} \ No newline at end of file diff --git a/src/main/java/net/sf/sockettest/swing/SocketTestClientView.java b/src/main/java/net/sf/sockettest/swing/SocketTestClientView.java new file mode 100644 index 0000000..15f22db --- /dev/null +++ b/src/main/java/net/sf/sockettest/swing/SocketTestClientView.java @@ -0,0 +1,33 @@ +package net.sf.sockettest.swing; + +public interface SocketTestClientView extends AskView { + void focusOnIp(); + + void focusOnPort(); + + void startWaitInfo(); + + void stopWaitInfo(); + + void connected(); + + void showConectionInfo(String messagge); + + void clearMessages(); + + void focusSendField(); + + void disconnected(); + + boolean isHexOutput(); + + void appendMessage(String message); + + void resetSend(); + + String getMessages(); + + String chooseFile(); + + void saveText(String text, String fileName); +} diff --git a/src/net/sf/sockettest/swing/SocketTestServer.java b/src/main/java/net/sf/sockettest/swing/SocketTestServer.java similarity index 50% rename from src/net/sf/sockettest/swing/SocketTestServer.java rename to src/main/java/net/sf/sockettest/swing/SocketTestServer.java index f87919e..3d1d3d1 100644 --- a/src/net/sf/sockettest/swing/SocketTestServer.java +++ b/src/main/java/net/sf/sockettest/swing/SocketTestServer.java @@ -3,10 +3,8 @@ import java.awt.*; import java.awt.event.*; import javax.swing.*; -import javax.swing.event.*; import javax.swing.border.*; -import java.net.*; import java.io.*; import net.sf.sockettest.*; @@ -15,11 +13,10 @@ * * @author Akshathkumar Shetty */ -public class SocketTestServer extends JPanel /*JFrame*/ { +public class SocketTestServer extends JPanel implements SocketTestServerView { private final String NEW_LINE = "\r\n"; private ClassLoader cl = getClass().getClassLoader(); - public ImageIcon logo = new ImageIcon( - cl.getResource("icons/logo.gif")); + public ImageIcon logo = new ImageIcon(cl.getResource("icons/logo.gif")); private JPanel topPanel; private JPanel toPanel; @@ -31,15 +28,14 @@ public class SocketTestServer extends JPanel /*JFrame*/ { private JLabel ipLabel = new JLabel("IP Address"); private JLabel portLabel = new JLabel("Port"); - private JLabel logoLabel = new JLabel("SocketTest v 3.0", logo, - JLabel.CENTER); + private JLabel logoLabel = new JLabel("SocketTest v 3.0", logo, JLabel.CENTER); private JTextField ipField = new JTextField("0.0.0.0",20); private JTextField portField = new JTextField("21",10); private JButton portButton = new JButton("Port"); private JButton connectButton = new JButton("Start Listening"); - + private JLabel convLabel = new JLabel("Conversation with Client"); - private Border connectedBorder = BorderFactory.createTitledBorder(new EtchedBorder(),"Connected Client : < NONE >"); + private Border connectedBorder; private JTextArea messagesField = new JTextArea(); private JLabel sendLabel = new JLabel("Message"); @@ -49,21 +45,23 @@ public class SocketTestServer extends JPanel /*JFrame*/ { private JButton disconnectButton = new JButton("Disconnect"); private JButton saveButton = new JButton("Save"); private JButton clearButton = new JButton("Clear"); - + + private JCheckBox hexInputCheckBox = new JCheckBox("Hex Input"); + private JCheckBox hexOutputCheckBox = new JCheckBox("Hex Output"); + private GridBagConstraints gbc = new GridBagConstraints(); - - private Socket socket; - private ServerSocket server; - private SocketServer socketServer; - private PrintWriter out; - - protected final JFrame parent; - - public SocketTestServer(final JFrame parent) { - //Container cp = getContentPane(); - this.parent = parent; + private JFrame parent; + + private SocketTextServerController controller; + + public SocketTestServer() { + buildGUI(); + showConnectionInfo("NONE"); + } + + private void buildGUI() { Container cp = this; - + topPanel = new JPanel(); toPanel = new JPanel(); toPanel.setLayout(new GridBagLayout()); @@ -77,26 +75,26 @@ public SocketTestServer(final JFrame parent) { gbc.anchor = GridBagConstraints.EAST; gbc.fill = GridBagConstraints.NONE; toPanel.add(ipLabel, gbc); - + gbc.weightx = 1.0; //streach gbc.gridx = 1; gbc.gridwidth = 3; gbc.fill = GridBagConstraints.HORIZONTAL; ActionListener ipListener = new ActionListener() { public void actionPerformed(ActionEvent e) { - portField.requestFocus(); + focusOnPort(); } }; ipField.addActionListener(ipListener); toPanel.add(ipField, gbc); - + gbc.weightx = 0.0; gbc.gridy = 1; gbc.gridx = 0; gbc.gridwidth = 1; gbc.fill = GridBagConstraints.NONE; toPanel.add(portLabel, gbc); - + gbc.weightx = 1.0; gbc.gridy = 1; gbc.gridx = 1; @@ -104,12 +102,12 @@ public void actionPerformed(ActionEvent e) { gbc.fill = GridBagConstraints.HORIZONTAL; ActionListener connectListener = new ActionListener() { public void actionPerformed(ActionEvent e) { - connect(); + controller.connect(SocketTestServer.this.ipField.getText(), SocketTestServer.this.portField.getText()); } }; portField.addActionListener(connectListener); toPanel.add(portField, gbc); - + gbc.weightx = 0.0; gbc.gridy = 1; gbc.gridx = 2; @@ -119,13 +117,13 @@ public void actionPerformed(ActionEvent e) { portButton.setToolTipText("View Standard Ports"); ActionListener portButtonListener = new ActionListener() { public void actionPerformed(ActionEvent e) { - PortDialog dia = new PortDialog(parent, PortDialog.TCP); - dia.show(); + PortDialog dialog = new PortDialog(parent, PortDialog.TCP); + dialog.setVisible(true); } }; portButton.addActionListener(portButtonListener); toPanel.add(portButton, gbc); - + gbc.weightx = 0.0; gbc.gridy = 1; gbc.gridx = 3; @@ -135,24 +133,25 @@ public void actionPerformed(ActionEvent e) { connectButton.setToolTipText("Start Listening"); connectButton.addActionListener(connectListener); toPanel.add(connectButton, gbc); - - toPanel.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Listen On")); - topPanel.setLayout(new BorderLayout(10,0)); + + toPanel.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(), "Listen On")); + topPanel.setLayout(new BorderLayout(10, 0)); topPanel.add(toPanel); logoLabel.setVerticalTextPosition(JLabel.BOTTOM); logoLabel.setHorizontalTextPosition(JLabel.CENTER); - topPanel.add(logoLabel,BorderLayout.EAST); + topPanel.add(logoLabel, BorderLayout.EAST); topPanel.setBorder(BorderFactory.createEmptyBorder(10,10,5,10)); - - + + textPanel = new JPanel(); - textPanel.setLayout(new BorderLayout(0,5)); - textPanel.add(convLabel,BorderLayout.NORTH); + textPanel.setLayout(new BorderLayout(0, 5)); + textPanel.add(convLabel, BorderLayout.NORTH); messagesField.setEditable(false); JScrollPane jsp = new JScrollPane(messagesField); textPanel.add(jsp); - textPanel.setBorder(BorderFactory.createEmptyBorder(3,3,0,3)); - + textPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 0, 3)); + textPanel.add(hexOutputCheckBox, BorderLayout.SOUTH); + sendPanel = new JPanel(); sendPanel.setLayout(new GridBagLayout()); gbc.weighty = 0.0; @@ -176,17 +175,7 @@ public void actionPerformed(ActionEvent e) { sendButton.setToolTipText("Send text to client"); ActionListener sendListener = new ActionListener() { public void actionPerformed(ActionEvent e) { - String msg = sendField.getText(); - if(!msg.equals("")) - sendMessage(msg); - else { - int value = JOptionPane.showConfirmDialog( - SocketTestServer.this, "Send Blank Line ?", - "Send Data To Client", - JOptionPane.YES_NO_OPTION); - if (value == JOptionPane.YES_OPTION) - sendMessage(msg); - } + controller.buildMessage(sendField.getText()); } }; sendButton.addActionListener(sendListener); @@ -194,19 +183,22 @@ public void actionPerformed(ActionEvent e) { sendPanel.add(sendButton, gbc); ActionListener disconnectListener = new ActionListener() { public void actionPerformed(ActionEvent e) { - disconnect(); + controller.disconnect(); } }; gbc.gridx = 3; disconnectButton.addActionListener(disconnectListener); disconnectButton.setEnabled(false); sendPanel.add(disconnectButton, gbc); - + gbc.gridx = 1; + gbc.gridy = 1; + sendPanel.add(hexInputCheckBox, gbc); + sendPanel.setBorder( new CompoundBorder( BorderFactory.createEmptyBorder(0,0,0,3), BorderFactory.createTitledBorder("Send"))); - + buttonPanel = new JPanel(); buttonPanel.setLayout(new GridBagLayout()); gbc.weighty = 0.0; @@ -227,26 +219,7 @@ public void actionPerformed(ActionEvent e) { saveButton.setMnemonic('S'); ActionListener saveListener = new ActionListener() { public void actionPerformed(ActionEvent e) { - String text = messagesField.getText(); - if(text.equals("")) { - error("Nothing to save","Save to file"); - return; - } - String fileName=""; - JFileChooser chooser = new JFileChooser(); - chooser.setCurrentDirectory(new File(".")); - int returnVal = chooser.showSaveDialog(SocketTestServer.this); - if(returnVal == JFileChooser.APPROVE_OPTION) { - fileName=chooser.getSelectedFile().getAbsolutePath(); - try { - Util.writeFile(fileName,text); - } catch (Exception ioe) { - JOptionPane.showMessageDialog(SocketTestServer.this, - ""+ioe.getMessage(), - "Error saving to file..", - JOptionPane.ERROR_MESSAGE); - } - } + controller.saveFile(messagesField.getText()); } }; saveButton.addActionListener(saveListener); @@ -262,152 +235,100 @@ public void actionPerformed(ActionEvent e) { clearButton.addActionListener(clearListener); buttonPanel.add(clearButton, gbc); buttonPanel.setBorder(BorderFactory.createEmptyBorder(3,0,0,3)); - + centerPanel = new JPanel(); centerPanel.setLayout(new BorderLayout(0,10)); centerPanel.add(buttonPanel,BorderLayout.SOUTH); centerPanel.add(textPanel,BorderLayout.CENTER); - + CompoundBorder cb=new CompoundBorder( BorderFactory.createEmptyBorder(5,10,10,10), connectedBorder); centerPanel.setBorder(cb); - + cp.setLayout(new BorderLayout(10,0)); cp.add(topPanel,BorderLayout.NORTH); cp.add(centerPanel,BorderLayout.CENTER); } - - /* - public static void main(String args[]) { - SocketTestServer stServer=new SocketTestServer(); - stServer.setTitle("SocketTest Server"); - stServer.setSize(500,400); - Util.centerWindow(stServer); - stServer.setDefaultCloseOperation(EXIT_ON_CLOSE); - stServer.setIconImage(stServer.logo.getImage()); - stServer.setVisible(true); - } - */ - - ///////////////////// - //action & helper methods - ///////////////////// - private void connect() { - if(server!=null) { - stop(); - return; - } - String ip=ipField.getText(); - String port=portField.getText(); - if(ip==null || ip.equals("")) { - JOptionPane.showMessageDialog(SocketTestServer.this, - "No IP Address. Please enter IP Address", - "Error connecting", JOptionPane.ERROR_MESSAGE); - ipField.requestFocus(); - ipField.selectAll(); - return; - } - if(port==null || port.equals("")) { - JOptionPane.showMessageDialog(SocketTestServer.this, - "No Port number. Please enter Port number", - "Error connecting", JOptionPane.ERROR_MESSAGE); - portField.requestFocus(); - portField.selectAll(); - return; - } - setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); - if(!Util.checkHost(ip)) { - JOptionPane.showMessageDialog(SocketTestServer.this, - "Bad IP Address", - "Error connecting", JOptionPane.ERROR_MESSAGE); - ipField.requestFocus(); - ipField.selectAll(); - setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - return; - } - int portNo = 0; - try { - portNo=Integer.parseInt(port); - } catch (Exception e) { - JOptionPane.showMessageDialog(SocketTestServer.this, - "Bad Port number. Please enter Port number", - "Error connecting", JOptionPane.ERROR_MESSAGE); - portField.requestFocus(); - portField.selectAll(); - setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - return; - } - try { - InetAddress bindAddr=null; - if(!ip.equals("0.0.0.0")) - bindAddr = InetAddress.getByName(ip); - else - bindAddr = null; - server = new ServerSocket(portNo,1,bindAddr); - - ipField.setEditable(false); - portField.setEditable(false); - - connectButton.setText("Stop Listening"); - connectButton.setMnemonic('S'); - connectButton.setToolTipText("Stop Listening"); - //sendButton.setEnabled(true); - //sendField.setEditable(true); - } catch (Exception e) { - error(e.getMessage(), "Starting Server at "+portNo); - setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - return; + + @Override + public String chooseFile() { + String fileName = null; + JFileChooser chooser = new JFileChooser(); + chooser.setCurrentDirectory(new File(".")); + int returnVal = chooser.showSaveDialog(SocketTestServer.this); + if (returnVal == JFileChooser.APPROVE_OPTION) { + fileName = chooser.getSelectedFile().getAbsolutePath(); } - //changeBorder(" "+socket.getInetAddress().getHostName()+ - // " ["+socket.getInetAddress().getHostAddress()+"] "); + return fileName; + } + + @Override + public boolean confirm(String title, String message, int option) { + int value = JOptionPane.showConfirmDialog( + SocketTestServer.this, message, + title, + JOptionPane.YES_NO_OPTION); + return value == option; + } + + @Override + public boolean isHexInput() { + return hexInputCheckBox.isSelected(); + } + + public SocketTestServer setParent(JFrame parent) { + this.parent = parent; + return this; + } + + @Override + public void focusOnPort() { + portField.requestFocus(); + portField.selectAll(); + } + + @Override + public void focusOnIp() { + ipField.requestFocus(); + ipField.selectAll(); + } + + @Override + public void connected() { + ipField.setEditable(false); + portField.setEditable(false); + connectButton.setText("Stop Listening"); + connectButton.setMnemonic('S'); + connectButton.setToolTipText("Stop Listening"); + //sendButton.setEnabled(true); + //sendField.setEditable(true); + } + + @Override + public void stopWaitInfo() { setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - messagesField.setText("> Server Started on Port: "+portNo+NEW_LINE); - append("> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); - socketServer=SocketServer.handle(this,server); - //sendField.requestFocus(); } - //disconnect a client - public synchronized void disconnect() { - try { - socketServer.setDesonnected(true); - } catch (Exception e) {} + + @Override + public void startWaitInfo() { + setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); } - - public synchronized void stop() { - try { - disconnect(); //close any client - socketServer.setStop(true); - } catch (Exception e) {} - server=null; + + public void stopped() { ipField.setEditable(true); portField.setEditable(true); connectButton.setText("Start Listening"); connectButton.setMnemonic('S'); connectButton.setToolTipText("Start Listening"); - append("> Server stopped"); - append("> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } - - public synchronized void setClientSocket(Socket s) { - - if(s==null) { - out=null; - socket = null; - changeBorder(null); - sendButton.setEnabled(false); - sendField.setEditable(false); - disconnectButton.setEnabled(false); - } else { - socket = s; - changeBorder(" "+socket.getInetAddress().getHostName()+ - " ["+socket.getInetAddress().getHostAddress()+"] "); - sendButton.setEnabled(true); - sendField.setEditable(true); - disconnectButton.setEnabled(true); - } + + public void socketSet(boolean b) { + sendButton.setEnabled(b); + sendField.setEditable(b); + disconnectButton.setEnabled(b); } - + public void error(String error) { if(error==null || error.equals("")) return; @@ -422,43 +343,22 @@ public void error(String error, String heading) { error, heading, JOptionPane.ERROR_MESSAGE); } - public void append(String msg) { + @Override + public void appendMessage(String msg) { messagesField.append(msg+NEW_LINE); messagesField.setCaretPosition(messagesField.getText().length()); } - - public void appendnoNewLine(String msg) { - messagesField.append(msg); - messagesField.setCaretPosition(messagesField.getText().length()); + + public void resetSendField() { + sendField.setText(""); } - - public void sendMessage(String s) { - setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); - try { - if(out==null) { - out = new PrintWriter(new BufferedWriter( - new OutputStreamWriter(socket.getOutputStream())), true); - } - append("S: "+s); - out.print(s+NEW_LINE); - out.flush(); - sendField.setText(""); - setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - } catch (Exception e) { - setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - JOptionPane.showMessageDialog(SocketTestServer.this, - e.getMessage(),"Error Sending Message", - JOptionPane.ERROR_MESSAGE); - disconnect(); - } + + public boolean isHexOutput() { + return hexOutputCheckBox.isSelected(); } - - private void changeBorder(String ip) { - if(ip==null || ip.equals("")) - connectedBorder = BorderFactory.createTitledBorder( - new EtchedBorder(), "Connected Client : < NONE >"); - else - connectedBorder = BorderFactory.createTitledBorder( + + public void showConnectionInfo(String ip) { + connectedBorder = BorderFactory.createTitledBorder( new EtchedBorder(), "Connected Client : < "+ip+" >"); CompoundBorder cb=new CompoundBorder( BorderFactory.createEmptyBorder(5,10,10,10), @@ -466,5 +366,10 @@ private void changeBorder(String ip) { centerPanel.setBorder(cb); invalidate(); repaint(); - } + } + + public void setController(SocketTextServerController controller) { + this.controller = controller; + controller.setView(this); + } } diff --git a/src/main/java/net/sf/sockettest/swing/SocketTestServerView.java b/src/main/java/net/sf/sockettest/swing/SocketTestServerView.java new file mode 100644 index 0000000..90eb453 --- /dev/null +++ b/src/main/java/net/sf/sockettest/swing/SocketTestServerView.java @@ -0,0 +1,29 @@ +package net.sf.sockettest.swing; + +public interface SocketTestServerView extends AskView { + String chooseFile(); + + boolean isHexInput(); + + void focusOnPort(); + + void focusOnIp(); + + void connected(); + + void stopWaitInfo(); + + void startWaitInfo(); + + void appendMessage(String msg); + + void stopped(); + + void showConnectionInfo(String ip); + + void socketSet(boolean set); + + void resetSendField(); + + boolean isHexOutput(); +} diff --git a/src/net/sf/sockettest/swing/SocketTestUdp.java b/src/main/java/net/sf/sockettest/swing/SocketTestUdp.java similarity index 98% rename from src/net/sf/sockettest/swing/SocketTestUdp.java rename to src/main/java/net/sf/sockettest/swing/SocketTestUdp.java index fe25473..1437645 100644 --- a/src/net/sf/sockettest/swing/SocketTestUdp.java +++ b/src/main/java/net/sf/sockettest/swing/SocketTestUdp.java @@ -3,7 +3,6 @@ import java.awt.*; import java.awt.event.*; import javax.swing.*; -import javax.swing.event.*; import javax.swing.border.*; import java.net.*; @@ -15,7 +14,7 @@ * * @author Akshathkumar Shetty */ -public class SocketTestUdp extends JPanel /*JFrame*/ { +public class SocketTestUdp extends JPanel { private final String NEW_LINE = "\r\n"; private ClassLoader cl = getClass().getClassLoader(); public ImageIcon logo = new ImageIcon( @@ -62,11 +61,9 @@ public class SocketTestUdp extends JPanel /*JFrame*/ { private DatagramPacket pack; private byte buffer[]; - protected final JFrame parent; + private JFrame parent; - public SocketTestUdp(final JFrame parent) { - //Container cp = getContentPane(); - this.parent = parent; + public SocketTestUdp() { Container cp = this; northPanel = new JPanel(); @@ -316,7 +313,12 @@ public void actionPerformed(ActionEvent e) { cp.add(buttonPanel,BorderLayout.SOUTH); } - + + public SocketTestUdp setParent(JFrame parent) { + this.parent = parent; + return this; + } + ///////////////////// //action & helper methods ///////////////////// diff --git a/src/net/sf/sockettest/swing/SplashScreen.java b/src/main/java/net/sf/sockettest/swing/SplashScreen.java similarity index 95% rename from src/net/sf/sockettest/swing/SplashScreen.java rename to src/main/java/net/sf/sockettest/swing/SplashScreen.java index 1eb0e74..97d7cb0 100644 --- a/src/net/sf/sockettest/swing/SplashScreen.java +++ b/src/main/java/net/sf/sockettest/swing/SplashScreen.java @@ -2,8 +2,7 @@ import java.awt.*; import javax.swing.*; -import javax.swing.border.*; -import java.awt.event.*; + import net.sf.sockettest.*; /** diff --git a/src/icons/ball.gif b/src/main/resources/icons/ball.gif similarity index 100% rename from src/icons/ball.gif rename to src/main/resources/icons/ball.gif diff --git a/src/icons/logo.gif b/src/main/resources/icons/logo.gif similarity index 100% rename from src/icons/logo.gif rename to src/main/resources/icons/logo.gif diff --git a/src/readme.txt b/src/main/resources/readme.txt similarity index 100% rename from src/readme.txt rename to src/main/resources/readme.txt diff --git a/src/tcpports.txt b/src/main/resources/tcpports.txt similarity index 100% rename from src/tcpports.txt rename to src/main/resources/tcpports.txt diff --git a/src/udpports.txt b/src/main/resources/udpports.txt similarity index 100% rename from src/udpports.txt rename to src/main/resources/udpports.txt diff --git a/src/test/java/net/sf/sockettest/AppTest.java b/src/test/java/net/sf/sockettest/AppTest.java new file mode 100644 index 0000000..1da15d5 --- /dev/null +++ b/src/test/java/net/sf/sockettest/AppTest.java @@ -0,0 +1,38 @@ +package test.java.net.sf.sockettest; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +}