From 62cc435a5f80ab20dfa41c562ff557e179006aaa Mon Sep 17 00:00:00 2001 From: rylon Date: Tue, 6 Nov 2012 14:43:10 +0100 Subject: [PATCH 1/4] exception fix netbeans project --- ws2012/P2P/uebungen/2/p2p_ex2/build.xml | 74 ++ ws2012/P2P/uebungen/2/p2p_ex2/manifest.mf | 3 + .../2/p2p_ex2/nbproject/build-impl.xml | 883 ++++++++++++++++++ .../2/p2p_ex2/nbproject/genfiles.properties | 8 + .../nbproject/private/private.properties | 2 + .../2/p2p_ex2/nbproject/project.properties | 72 ++ .../uebungen/2/p2p_ex2/nbproject/project.xml | 13 + .../src/buffered/BufferedNetworkStack.java | 2 +- 8 files changed, 1056 insertions(+), 1 deletion(-) create mode 100644 ws2012/P2P/uebungen/2/p2p_ex2/build.xml create mode 100644 ws2012/P2P/uebungen/2/p2p_ex2/manifest.mf create mode 100644 ws2012/P2P/uebungen/2/p2p_ex2/nbproject/build-impl.xml create mode 100644 ws2012/P2P/uebungen/2/p2p_ex2/nbproject/genfiles.properties create mode 100644 ws2012/P2P/uebungen/2/p2p_ex2/nbproject/private/private.properties create mode 100644 ws2012/P2P/uebungen/2/p2p_ex2/nbproject/project.properties create mode 100644 ws2012/P2P/uebungen/2/p2p_ex2/nbproject/project.xml diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/build.xml b/ws2012/P2P/uebungen/2/p2p_ex2/build.xml new file mode 100644 index 00000000..ecd442ef --- /dev/null +++ b/ws2012/P2P/uebungen/2/p2p_ex2/build.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + Builds, tests, and runs the project P2P. + + + diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/manifest.mf b/ws2012/P2P/uebungen/2/p2p_ex2/manifest.mf new file mode 100644 index 00000000..328e8e5b --- /dev/null +++ b/ws2012/P2P/uebungen/2/p2p_ex2/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/build-impl.xml b/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/build-impl.xml new file mode 100644 index 00000000..0a42dc22 --- /dev/null +++ b/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/build-impl.xml @@ -0,0 +1,883 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set 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 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}" + + + + + + + + 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 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/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/genfiles.properties b/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/genfiles.properties new file mode 100644 index 00000000..ce24c0ab --- /dev/null +++ b/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=42736814 +build.xml.script.CRC32=b6e6d2dd +build.xml.stylesheet.CRC32=28e38971@1.38.3.45 +# 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=42736814 +nbproject/build-impl.xml.script.CRC32=082c0e5f +nbproject/build-impl.xml.stylesheet.CRC32=229523de@1.38.3.45 diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/private/private.properties b/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/private/private.properties new file mode 100644 index 00000000..86f01fb1 --- /dev/null +++ b/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/private/private.properties @@ -0,0 +1,2 @@ +compile.on.save=true +user.properties.file=C:\\Users\\rylon\\.netbeans\\6.9\\build.properties diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/project.properties b/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/project.properties new file mode 100644 index 00000000..d87d7aaf --- /dev/null +++ b/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/project.properties @@ -0,0 +1,72 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +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 +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +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}/P2P.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +file.reference.p2p_ex2-src=src +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.5 +javac.target=1.5 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${libs.junit.classpath}:\ + ${libs.junit_4.classpath} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=buffered.BufferedNetworkStack +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +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} +source.encoding=UTF-8 +src.dir=${file.reference.p2p_ex2-src} diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/project.xml b/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/project.xml new file mode 100644 index 00000000..639afa1e --- /dev/null +++ b/ws2012/P2P/uebungen/2/p2p_ex2/nbproject/project.xml @@ -0,0 +1,13 @@ + + + org.netbeans.modules.java.j2seproject + + + P2P + + + + + + + diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStack.java b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStack.java index 396c75f5..5b92e4eb 100644 --- a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStack.java +++ b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStack.java @@ -34,7 +34,7 @@ class BufferedNetworkStack { boolean flag = false; StringBuilder result = new StringBuilder(16); for (String s : cunks) { - if (!s.isEmpty()) { + if (s != null && !s.isEmpty()) { result.append(s); flag = true; } From 1e0cee10615713ff17e58440284ebb1bee158de7 Mon Sep 17 00:00:00 2001 From: rylon Date: Tue, 6 Nov 2012 15:04:24 +0100 Subject: [PATCH 2/4] timeout now working, no more exceptions --- .../buffered/BufferedNetworkStackClient.java | 22 ++++++++++++++----- .../2/p2p_ex2/src/buffered/ClientGUI.java | 8 +++++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java index b46611ca..f1062069 100644 --- a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java +++ b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java @@ -18,13 +18,13 @@ public class BufferedNetworkStackClient { buf = ByteBuffer.allocate(1024); } - public void push(String s, String host, int port) throws IOException { + public void push(String s, String host, int port) throws IOException, Exception{ if (channel == null) { channel = DatagramChannel.open(); } InetSocketAddress dest = new InetSocketAddress(host, port); - + buf.clear(); buf.put(CMD_PUSH); @@ -32,11 +32,15 @@ public class BufferedNetworkStackClient { buf.put(length); buf.flip(); - channel.send(buf, dest); + channel.send(buf, dest); buf.flip(); buf.clear(); - channel.receive(buf); + channel.configureBlocking(false); + + if(channel.receive(buf) == null){ + throw new Exception("ERROR: Buffer empty - not data recieved"); + } byte[] receivedData = buf.array(); String infoPacket = new String(receivedData).trim(); @@ -46,6 +50,7 @@ public class BufferedNetworkStackClient { int remoteBufSize = Integer.valueOf(info[0]); int sessionId = Integer.valueOf(info[1]); + System.out.println('4'); sendChunks(s, dest, remoteBufSize, sessionId); } @@ -80,7 +85,7 @@ public class BufferedNetworkStackClient { } } - public String pop(String host, int port) throws IOException { + public String pop(String host, int port) throws IOException, Exception{ if (channel == null) { channel = DatagramChannel.open(); } @@ -88,7 +93,12 @@ public class BufferedNetworkStackClient { InetSocketAddress dest = new InetSocketAddress(host, port); channel.send(ByteBuffer.wrap(new byte[] { CMD_POP }), dest); - channel.receive(buf); + channel.configureBlocking(false); + + if(channel.receive(buf) == null){ + throw new Exception("ERROR: Buffer empty - not data recieved"); + } + byte[] receivedData = buf.array(); String strReceived = new String(receivedData).toString().trim(); diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/ClientGUI.java b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/ClientGUI.java index 4baca129..62dea1b6 100644 --- a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/ClientGUI.java +++ b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/ClientGUI.java @@ -115,7 +115,9 @@ public class ClientGUI extends JFrame { e1.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); - } + } catch (Exception e3){ + textPane.setText(textPane.getText() + "\n" + e3.getMessage()); + } textPane.setText(textPane.getText() + "\n" + received); } @@ -129,7 +131,9 @@ public class ClientGUI extends JFrame { } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); - } + } catch (Exception e3){ + textPane.setText(textPane.getText() + "\n" + e3.getMessage()); + } } }); From c4aac841fcad722694001c4129abca4ff8bb463f Mon Sep 17 00:00:00 2001 From: rylon Date: Tue, 6 Nov 2012 15:08:28 +0100 Subject: [PATCH 3/4] merge fix --- .../uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStack.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStack.java b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStack.java index e519f3bc..ff864dc9 100644 --- a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStack.java +++ b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStack.java @@ -38,7 +38,6 @@ class BufferedNetworkStack { for (String s : cunks) { if (s != null && !s.isEmpty()) { result.append(s); - flag = true; } } From 1c680009c5efe0fa194674be2b24780825bf7997 Mon Sep 17 00:00:00 2001 From: rylon Date: Tue, 6 Nov 2012 15:40:32 +0100 Subject: [PATCH 4/4] timeout implemented --- .../buffered/BufferedNetworkStackClient.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java index f43a907e..1bc2a986 100644 --- a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java +++ b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java @@ -11,6 +11,8 @@ public class BufferedNetworkStackClient { protected static final byte CMD_POP = '2'; protected static final byte CMD_PRINT = '3'; + protected static final long TIMEOUT = 5000; + protected static final int BUF_SIZE = 128; private DatagramChannel channel = null; @@ -38,11 +40,15 @@ public class BufferedNetworkStackClient { buf.flip(); buf = ByteBuffer.allocate(BUF_SIZE); - channel.configureBlocking(false); - - if(channel.receive(buf) == null){ + + channel.configureBlocking(false); + long time = System.currentTimeMillis() + TIMEOUT; + boolean rec = false; + while(rec = channel.receive(buf) == null && System.currentTimeMillis() < time){} + if(!rec){ throw new Exception("ERROR: Buffer empty - not data recieved"); } + channel.configureBlocking(true); byte[] receivedData = buf.array(); String infoPacket = new String(receivedData).trim(); @@ -97,10 +103,13 @@ public class BufferedNetworkStackClient { channel.send(ByteBuffer.wrap(new byte[] { CMD_POP }), dest); channel.configureBlocking(false); - - if(channel.receive(buf) == null){ + long time = System.currentTimeMillis() + TIMEOUT; + boolean rec = false; + while(rec = channel.receive(buf) == null && System.currentTimeMillis() < time){} + if(!rec){ throw new Exception("ERROR: Buffer empty - not data recieved"); } + channel.configureBlocking(true); byte[] receivedData = buf.array(); String strReceived = new String(receivedData).toString().trim();