|
From: <jo...@us...> - 2008-04-01 06:04:03
|
Revision: 217
http://mspsim.svn.sourceforge.net/mspsim/?rev=217&view=rev
Author: joxe
Date: 2008-03-31 23:03:58 -0700 (Mon, 31 Mar 2008)
Log Message:
-----------
small fix to the network connection.
Modified Paths:
--------------
mspsim/se/sics/mspsim/util/NetworkConnection.java
Modified: mspsim/se/sics/mspsim/util/NetworkConnection.java
===================================================================
--- mspsim/se/sics/mspsim/util/NetworkConnection.java 2008-03-31 18:12:04 UTC (rev 216)
+++ mspsim/se/sics/mspsim/util/NetworkConnection.java 2008-04-01 06:03:58 UTC (rev 217)
@@ -56,7 +56,9 @@
*/
public class NetworkConnection implements Runnable {
+ private final static boolean DEBUG = true;
private final static int DEFAULT_PORT = 4711;
+
ServerSocket serverSocket = null;
ArrayList<ConnectionThread> connections = new ArrayList<ConnectionThread>();
@@ -65,10 +67,10 @@
public NetworkConnection() {
if (connect(DEFAULT_PORT)) {
- System.out.println("Connected to network...");
+ System.out.println("NetworkConnection: Connected to network...");
} else {
setupServer(DEFAULT_PORT);
- System.out.println("Setup network server...");
+ System.out.println("NetworkConnection: Setup network server...");
}
}
@@ -80,7 +82,7 @@
private void setupServer(int port) {
try {
serverSocket = new ServerSocket(port);
- System.out.println("setup of server socket finished... ");
+ if (DEBUG) System.out.println("NetworkConnection: setup of server socket finished... ");
new Thread(this).start();
} catch (IOException e) {
e.printStackTrace();
@@ -88,11 +90,11 @@
}
public void run() {
- System.out.println("Accepting new connections...");
+ System.out.println("NetworkConnection: Accepting new connections...");
while (true) {
try {
Socket s = serverSocket.accept();
- System.out.println("New connection accepted...");
+ if (DEBUG) System.out.println("NetworkConnection: New connection accepted...");
connections.add(new ConnectionThread(s));
} catch (IOException e) {
// TODO Auto-generated catch block
@@ -124,19 +126,23 @@
// Data was sent from the radio in the node (or other node) and should
// be sent out to other nodes!!!
public void dataSent(int[] data) {
- for (int i = 0; i < data.length; i++) {
- buf[i] = (byte) data[i];
- }
- ConnectionThread[] cthr = connections.toArray(new ConnectionThread[connections.size()]);
- for (int i = 0; i < cthr.length; i++) {
- if (cthr[i].isClosed()) {
- connections.remove(cthr);
- } else {
- try {
- cthr[i].output.write(buf);
- } catch (IOException e) {
- e.printStackTrace();
- cthr[i].close();
+ if (connections.size() > 0) {
+ for (int i = 0; i < data.length; i++) {
+ buf[i] = (byte) data[i];
+ }
+ ConnectionThread[] cthr = connections.toArray(new ConnectionThread[connections.size()]);
+ for (int i = 0; i < cthr.length; i++) {
+ if (cthr[i].isClosed()) {
+ connections.remove(cthr);
+ } else {
+ try {
+ cthr[i].output.write((byte) data.length);
+ cthr[i].output.write(buf);
+ if (DEBUG) System.out.println("NetworkConnection: wrote " + data.length + " bytes");
+ } catch (IOException e) {
+ e.printStackTrace();
+ cthr[i].close();
+ }
}
}
}
@@ -188,15 +194,15 @@
@Override
public void run() {
- System.out.println("Started connection thread...");
+ if (DEBUG) System.out.println("NetworkConnection: Started connection thread...");
while (socket != null) {
int len;
try {
len = input.read();
if (len > 0) {
input.readFully(buffer, 0, len);
- System.out.println("Read packet with " + len + " bytes");
- dataReceived(buffer, len);
+ if (DEBUG) System.out.println("NetworkConnection: Read packet with " + len + " bytes");
+ dataReceived(buffer, len);
}
} catch (IOException e) {
e.printStackTrace();
@@ -205,6 +211,4 @@
}
}
}
-
-
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <jo...@us...> - 2008-04-01 22:26:19
|
Revision: 221
http://mspsim.svn.sourceforge.net/mspsim/?rev=221&view=rev
Author: joxe
Date: 2008-04-01 15:25:56 -0700 (Tue, 01 Apr 2008)
Log Message:
-----------
fixed bug and debug-output for NC.
Modified Paths:
--------------
mspsim/se/sics/mspsim/util/NetworkConnection.java
Modified: mspsim/se/sics/mspsim/util/NetworkConnection.java
===================================================================
--- mspsim/se/sics/mspsim/util/NetworkConnection.java 2008-04-01 08:19:47 UTC (rev 220)
+++ mspsim/se/sics/mspsim/util/NetworkConnection.java 2008-04-01 22:25:56 UTC (rev 221)
@@ -137,8 +137,11 @@
} else {
try {
cthr[i].output.write((byte) data.length);
- cthr[i].output.write(buf);
- if (DEBUG) System.out.println("NetworkConnection: wrote " + data.length + " bytes");
+ cthr[i].output.write(buf, 0, data.length);
+ if (DEBUG) {
+ System.out.println("NetworkConnection: wrote " + data.length + " bytes");
+ printPacket(buf, data.length);
+ }
} catch (IOException e) {
e.printStackTrace();
cthr[i].close();
@@ -148,6 +151,13 @@
}
}
+ private void printPacket(byte[] data, int len) {
+ for (int i = 0; i < len; i++) {
+ System.out.print("" + Utils.hex8(data[i]) + " ");
+ }
+ System.out.println();
+ }
+
private boolean connect(int port) {
try {
Socket socket = new Socket("127.0.0.1", port);
@@ -201,7 +211,11 @@
len = input.read();
if (len > 0) {
input.readFully(buffer, 0, len);
- if (DEBUG) System.out.println("NetworkConnection: Read packet with " + len + " bytes");
+ if (DEBUG) {
+ System.out.println("NetworkConnection: Read packet with " + len + " bytes");
+ printPacket(buffer, len);
+ }
+
dataReceived(buffer, len);
}
} catch (IOException e) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <jo...@us...> - 2008-04-04 07:46:06
|
Revision: 229
http://mspsim.svn.sourceforge.net/mspsim/?rev=229&view=rev
Author: joxe
Date: 2008-04-04 00:45:50 -0700 (Fri, 04 Apr 2008)
Log Message:
-----------
fixed network connection (server) to avoid sending back to the source radio.
Modified Paths:
--------------
mspsim/se/sics/mspsim/util/NetworkConnection.java
Modified: mspsim/se/sics/mspsim/util/NetworkConnection.java
===================================================================
--- mspsim/se/sics/mspsim/util/NetworkConnection.java 2008-04-04 06:53:36 UTC (rev 228)
+++ mspsim/se/sics/mspsim/util/NetworkConnection.java 2008-04-04 07:45:50 UTC (rev 229)
@@ -104,19 +104,20 @@
}
// Data incoming from the network!!!
- private void dataReceived(byte[] data, int len) {
+ private void dataReceived(byte[] data, int len, ConnectionThread source) {
int[] buf = new int[len];
if (listener != null) {
for (int i = 0; i < buf.length; i++) {
buf[i] = data[i];
}
- // Send this data to the transmitter
+ // Send this data to the transmitter in this node!
listener.transmissionStarted();
listener.transmissionEnded(buf);
}
+ // And if this is the server, propagate to the others
if (serverSocket != null) {
- dataSent(buf);
+ dataSent(buf, source);
}
}
@@ -126,6 +127,12 @@
// Data was sent from the radio in the node (or other node) and should
// be sent out to other nodes!!!
public void dataSent(int[] data) {
+ dataSent(data, null);
+ }
+
+ // Data was sent either from radio, or came from another "radio" -
+ // and if so it should be propagated to all others.
+ public void dataSent(int[] data, ConnectionThread source) {
if (connections.size() > 0) {
for (int i = 0; i < data.length; i++) {
buf[i] = (byte) data[i];
@@ -134,7 +141,8 @@
for (int i = 0; i < cthr.length; i++) {
if (cthr[i].isClosed()) {
connections.remove(cthr);
- } else {
+ // Do not write back to the source
+ } else if (cthr[i] != source){
try {
cthr[i].output.write((byte) data.length);
cthr[i].output.write(buf, 0, data.length);
@@ -215,8 +223,7 @@
System.out.println("NetworkConnection: Read packet with " + len + " bytes");
printPacket(buffer, len);
}
-
- dataReceived(buffer, len);
+ dataReceived(buffer, len, this);
}
} catch (IOException e) {
e.printStackTrace();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ni...@us...> - 2008-04-24 15:31:09
|
Revision: 264
http://mspsim.svn.sourceforge.net/mspsim/?rev=264&view=rev
Author: nifi
Date: 2008-04-24 08:29:13 -0700 (Thu, 24 Apr 2008)
Log Message:
-----------
cleanup
Modified Paths:
--------------
mspsim/se/sics/mspsim/util/NetworkConnection.java
Modified: mspsim/se/sics/mspsim/util/NetworkConnection.java
===================================================================
--- mspsim/se/sics/mspsim/util/NetworkConnection.java 2008-04-24 15:18:03 UTC (rev 263)
+++ mspsim/se/sics/mspsim/util/NetworkConnection.java 2008-04-24 15:29:13 UTC (rev 264)
@@ -78,7 +78,7 @@
public void addPacketListener(PacketListener pl) {
listener = pl;
}
-
+
private void setupServer(int port) {
try {
serverSocket = new ServerSocket(port);
@@ -107,15 +107,15 @@
// all other nodes
private void dataReceived(byte[] data, int len, ConnectionThread source) {
int[] buf = new int[len];
+ for (int i = 0; i < buf.length; i++) {
+ buf[i] = data[i];
+ }
if (listener != null) {
- for (int i = 0; i < buf.length; i++) {
- buf[i] = data[i];
- }
// Send this data to the transmitter in this node!
listener.transmissionStarted();
listener.transmissionEnded(buf);
}
-
+
// And if this is the server, propagate to the others
if (serverSocket != null) {
dataSent(buf, source);
@@ -123,7 +123,7 @@
}
- byte[] buf = new byte[256];
+ private byte[] buf = new byte[256];
// Data was sent from the radio in the node (or other node) and should
// be sent out to other nodes!!!
@@ -137,7 +137,7 @@
if (connections.size() > 0) {
for (int i = 0; i < data.length; i++) {
buf[i] = (byte) data[i];
- }
+ }
ConnectionThread[] cthr = connections.toArray(new ConnectionThread[connections.size()]);
for (int i = 0; i < cthr.length; i++) {
if (cthr[i].isClosed()) {
@@ -159,10 +159,10 @@
}
}
}
-
+
private void printPacket(byte[] data, int len) {
for (int i = 0; i < len; i++) {
- System.out.print("" + Utils.hex8(data[i]) + " ");
+ System.out.print(Utils.hex8(data[i]) + " ");
}
System.out.println();
}
@@ -214,9 +214,9 @@
@Override
public void run() {
if (DEBUG) System.out.println("NetworkConnection: Started connection thread...");
- while (socket != null) {
- int len;
- try {
+ try {
+ while (socket != null) {
+ int len;
len = input.read();
if (len > 0) {
input.readFully(buffer, 0, len);
@@ -226,11 +226,11 @@
}
dataReceived(buffer, len, this);
}
- } catch (IOException e) {
- e.printStackTrace();
- socket = null;
}
+ } catch (IOException e) {
+ e.printStackTrace();
+ close();
}
- }
+ }
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ni...@us...> - 2008-04-25 15:02:34
|
Revision: 267
http://mspsim.svn.sourceforge.net/mspsim/?rev=267&view=rev
Author: nifi
Date: 2008-04-25 08:02:17 -0700 (Fri, 25 Apr 2008)
Log Message:
-----------
removed debug output
Modified Paths:
--------------
mspsim/se/sics/mspsim/util/NetworkConnection.java
Modified: mspsim/se/sics/mspsim/util/NetworkConnection.java
===================================================================
--- mspsim/se/sics/mspsim/util/NetworkConnection.java 2008-04-25 14:54:01 UTC (rev 266)
+++ mspsim/se/sics/mspsim/util/NetworkConnection.java 2008-04-25 15:02:17 UTC (rev 267)
@@ -56,15 +56,13 @@
*/
public class NetworkConnection implements Runnable {
- private final static boolean DEBUG = true;
+ private final static boolean DEBUG = false;
private final static int DEFAULT_PORT = 4711;
-
- ServerSocket serverSocket = null;
-
- ArrayList<ConnectionThread> connections = new ArrayList<ConnectionThread>();
-
+
+ private ServerSocket serverSocket = null;
+ private ArrayList<ConnectionThread> connections = new ArrayList<ConnectionThread>();
private PacketListener listener;
-
+
public NetworkConnection() {
if (connect(DEFAULT_PORT)) {
System.out.println("NetworkConnection: Connected to network...");
@@ -137,8 +135,9 @@
} else if (cthr[i] != source){
try {
cthr[i].output.write(receivedData, 0, receivedData.length);
+ cthr[i].output.flush();
if (DEBUG) {
- System.out.println("NetworkConnection: wrote " + receivedData.length + " bytes");
+// System.out.println("NetworkConnection: wrote " + receivedData.length + " bytes");
printPacket(receivedData);
}
} catch (IOException e) {
@@ -151,6 +150,7 @@
}
private void printPacket(byte[] data) {
+ System.out.print("NetworkConnection: ");
for (int i = 0, len = data.length; i < len; i++) {
System.out.print(Utils.hex8(data[i]) + " ");
}
@@ -210,7 +210,7 @@
buffer[0] = (byte) (len & 0xff);
input.readFully(buffer, 1, len);
if (DEBUG) {
- System.out.println("NetworkConnection: Read packet with " + len + " bytes");
+// System.out.println("NetworkConnection: Read packet with " + len + " bytes");
printPacket(buffer);
}
dataReceived(buffer, this);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ni...@us...> - 2008-05-13 15:22:00
|
Revision: 282
http://mspsim.svn.sourceforge.net/mspsim/?rev=282&view=rev
Author: nifi
Date: 2008-05-13 08:21:57 -0700 (Tue, 13 May 2008)
Log Message:
-----------
changed to use separate thread for I/O
Modified Paths:
--------------
mspsim/se/sics/mspsim/util/NetworkConnection.java
Modified: mspsim/se/sics/mspsim/util/NetworkConnection.java
===================================================================
--- mspsim/se/sics/mspsim/util/NetworkConnection.java 2008-05-13 15:18:14 UTC (rev 281)
+++ mspsim/se/sics/mspsim/util/NetworkConnection.java 2008-05-13 15:21:57 UTC (rev 282)
@@ -60,7 +60,8 @@
private final static int DEFAULT_PORT = 4711;
private ServerSocket serverSocket = null;
- private ArrayList<ConnectionThread> connections = new ArrayList<ConnectionThread>();
+ private SendThread sendThread = null;
+ private ConnectionThread[] connections = null;
private PacketListener listener;
public NetworkConnection() {
@@ -70,6 +71,7 @@
setupServer(DEFAULT_PORT);
System.out.println("NetworkConnection: Setup network server...");
}
+ sendThread = new SendThread();
}
// TODO: this should handle several listeners!!!
@@ -86,16 +88,15 @@
e.printStackTrace();
}
}
-
+
public void run() {
System.out.println("NetworkConnection: Accepting new connections...");
while (true) {
try {
Socket s = serverSocket.accept();
- if (DEBUG) System.out.println("NetworkConnection: New connection accepted...");
- connections.add(new ConnectionThread(s));
+ if (DEBUG) System.out.println("NetworkConnection: New connection from " + s.getRemoteSocketAddress());
+ connections = (ConnectionThread[]) Utils.add(ConnectionThread.class, connections, new ConnectionThread(s));
} catch (IOException e) {
- // TODO Auto-generated catch block
e.printStackTrace();
}
}
@@ -115,44 +116,25 @@
dataSent(data, source);
}
}
-
// Data was sent from the radio in the node (or other node) and should
// be sent out to other nodes!!!
public void dataSent(byte[] receivedData) {
dataSent(receivedData, null);
}
-
+
// Data was sent either from radio, or came from another "radio" -
// and if so it should be propagated to all others.
public void dataSent(byte[] receivedData, ConnectionThread source) {
- if (connections.size() > 0) {
- ConnectionThread[] cthr = connections.toArray(new ConnectionThread[connections.size()]);
- for (int i = 0; i < cthr.length; i++) {
- if (cthr[i].isClosed()) {
- connections.remove(cthr);
- // Do not write back to the source
- } else if (cthr[i] != source){
- try {
- cthr[i].output.write(receivedData, 0, receivedData.length);
- cthr[i].output.flush();
- if (DEBUG) {
-// System.out.println("NetworkConnection: wrote " + receivedData.length + " bytes");
- printPacket(receivedData);
- }
- } catch (IOException e) {
- e.printStackTrace();
- cthr[i].close();
- }
- }
- }
+ if (connections != null && sendThread != null) {
+ sendThread.send(receivedData, source);
}
}
- private void printPacket(byte[] data) {
- System.out.print("NetworkConnection: ");
+ private void printPacket(String prefix, byte[] data) {
+ System.out.print("NetworkConnection: " + prefix);
for (int i = 0, len = data.length; i < len; i++) {
- System.out.print(Utils.hex8(data[i]) + " ");
+ System.out.print(' ' + Utils.hex8(data[i]));
}
System.out.println();
}
@@ -160,7 +142,7 @@
private boolean connect(int port) {
try {
Socket socket = new Socket("127.0.0.1", port);
- connections.add(new ConnectionThread(socket));
+ connections = (ConnectionThread[]) Utils.add(ConnectionThread.class, connections, new ConnectionThread(socket));
} catch (UnknownHostException e) {
return false;
} catch (IOException e) {
@@ -168,7 +150,75 @@
}
return true;
}
-
+
+ private static class SendEvent {
+ public final byte[] data;
+ public final ConnectionThread source;
+ public SendEvent(byte[] data, ConnectionThread source) {
+ this.data = data;
+ this.source = source;
+ }
+ }
+
+ class SendThread implements Runnable {
+
+ private ArrayList<SendEvent> queue = new ArrayList<SendEvent>();
+
+ public SendThread() {
+ new Thread(this).start();
+ }
+
+ public synchronized void send(byte[] receivedData, ConnectionThread source) {
+ queue.add(new SendEvent(receivedData, source));
+ notifyAll();
+ }
+
+ public synchronized SendEvent getNext() throws InterruptedException {
+ while (queue.isEmpty()) {
+ wait();
+ }
+ return queue.remove(0);
+ }
+
+ private void sendPacket(SendEvent event) {
+ ConnectionThread[] cthr = connections;
+ if (cthr != null) {
+ for (int i = 0; i < cthr.length; i++) {
+ if (cthr[i].isClosed()) {
+ connections = (ConnectionThread[]) Utils.remove(connections, cthr[i]);
+ // Do not write back to the source
+ } else if (cthr[i] != event.source){
+ try {
+ cthr[i].output.write(event.data, 0, event.data.length);
+ cthr[i].output.flush();
+ } catch (IOException e) {
+ e.printStackTrace();
+ cthr[i].close();
+ }
+ }
+ }
+ if (DEBUG) {
+// System.out.println("NetworkConnection: wrote " + receivedData.length + " bytes");
+ printPacket("sent", event.data);
+ }
+ }
+ }
+
+ public void run() {
+ try {
+ SendEvent event;
+ do {
+ event = getNext();
+ if (event != null) {
+ sendPacket(event);
+ }
+ } while (event != null);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
class ConnectionThread implements Runnable {
Socket socket;
DataInputStream input;
@@ -207,7 +257,7 @@
input.readFully(buffer, 1, len);
if (DEBUG) {
// System.out.println("NetworkConnection: Read packet with " + len + " bytes");
- printPacket(buffer);
+ printPacket("read", buffer);
}
dataReceived(buffer, this);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|