From e7be7dcd18a3e56953cd757766e82aba05f05e86 Mon Sep 17 00:00:00 2001 From: joxe Date: Mon, 31 Mar 2008 18:12:04 +0000 Subject: [PATCH] added simple connection between mspsim nodes (not yet working) git-svn-id: https://mspsim.svn.sourceforge.net/svnroot/mspsim/mspsim@216 23d1a52b-0c3c-0410-b72d-8f29ab48fe35 --- CHANGE_LOG.txt | 1 + se/sics/mspsim/ui/SerialMon.java | 4 +- se/sics/mspsim/util/NetworkConnection.java | 210 +++++++++++++++++++++ 3 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 se/sics/mspsim/util/NetworkConnection.java diff --git a/CHANGE_LOG.txt b/CHANGE_LOG.txt index 9731af9..19d7c6c 100644 --- a/CHANGE_LOG.txt +++ b/CHANGE_LOG.txt @@ -14,6 +14,7 @@ Changes: - added event system for cycle based events - fixed timer (A/B) to be event driven instead of tick driven - fixed bugs in interrupt handling of timer system +- added argument management and -nogui parameter 0.84 Changes: diff --git a/se/sics/mspsim/ui/SerialMon.java b/se/sics/mspsim/ui/SerialMon.java index 533152c..5ec686d 100644 --- a/se/sics/mspsim/ui/SerialMon.java +++ b/se/sics/mspsim/ui/SerialMon.java @@ -55,7 +55,7 @@ import se.sics.mspsim.core.*; public class SerialMon implements KeyListener, USARTListener { private static final String PREFIX = " > "; - + private static final int MAX_LINES = 200; private String name; private JFrame window; private USART usart; @@ -97,7 +97,7 @@ public class SerialMon implements KeyListener, USARTListener { public void dataReceived(USART source, int data) { if (data == '\n') { - if (lines >= 60) { + if (lines >= MAX_LINES) { int index = text.indexOf('\n'); text = text.substring(index + 1); } else { diff --git a/se/sics/mspsim/util/NetworkConnection.java b/se/sics/mspsim/util/NetworkConnection.java new file mode 100644 index 0000000..c2e2719 --- /dev/null +++ b/se/sics/mspsim/util/NetworkConnection.java @@ -0,0 +1,210 @@ +/** + * Copyright (c) 2007, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of MSPSim. + * + * $Id: $ + * + * ----------------------------------------------------------------- + * + * NetworkConnection + * + * Author : Joakim Eriksson + * Created : 31 mar 2008 + * Updated : $Date:$ + * $Revision:$ + */ +package se.sics.mspsim.util; + +import java.io.DataInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.UnknownHostException; +import java.util.ArrayList; + +import se.sics.mspsim.chip.PacketListener; + +/** + * @author joakim + * + */ +public class NetworkConnection implements Runnable { + + private final static int DEFAULT_PORT = 4711; + ServerSocket serverSocket = null; + + ArrayList connections = new ArrayList(); + + private PacketListener listener; + + public NetworkConnection() { + if (connect(DEFAULT_PORT)) { + System.out.println("Connected to network..."); + } else { + setupServer(DEFAULT_PORT); + System.out.println("Setup network server..."); + } + } + + // TODO: this should handle several listeners!!! + public void addPacketListener(PacketListener pl) { + listener = pl; + } + + private void setupServer(int port) { + try { + serverSocket = new ServerSocket(port); + System.out.println("setup of server socket finished... "); + new Thread(this).start(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void run() { + System.out.println("Accepting new connections..."); + while (true) { + try { + Socket s = serverSocket.accept(); + System.out.println("New connection accepted..."); + connections.add(new ConnectionThread(s)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + // Data incoming from the network!!! + private void dataReceived(byte[] data, int len) { + 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 + listener.transmissionStarted(); + listener.transmissionEnded(buf); + } + + if (serverSocket != null) { + dataSent(buf); + } + } + + + 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!!! + 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(); + } + } + } + } + + private boolean connect(int port) { + try { + Socket socket = new Socket("127.0.0.1", port); + connections.add(new ConnectionThread(socket)); + } catch (UnknownHostException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } + return true; + } + + class ConnectionThread implements Runnable { + byte[] buffer = new byte[256]; + + Socket socket; + DataInputStream input; + OutputStream output; + + public ConnectionThread(Socket socket) throws IOException { + this.socket = socket; + input = new DataInputStream(socket.getInputStream()); + output = socket.getOutputStream(); + new Thread(this).start(); + } + + public void close() { + try { + input.close(); + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + socket = null; + } + + public boolean isClosed() { + return socket == null; + } + + @Override + public void run() { + System.out.println("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); + } + } catch (IOException e) { + e.printStackTrace(); + socket = null; + } + } + } + } + + +}