//jleblanc 3.18.06 //simple input/output GUI //THIS DOESN'T work but is an extension of the previous //messenger program to think about how to get a message //form the PIC //IMPORT JAVA .awt FOR GUI// import java.awt.*; import java.io.*; // this is the input/output library needed for data streams import java.net.*; // this is the network library needed for sockets public class Messenger extends PApplet implements ActionListener { //NETWORK STUFF String host; int port; Socket mySocket; // declare Socket DataInputStream myInputStream; // declare data input stream. This will run within a socket, bringing data into Java DataOutputStream myOutputStream; // declare data output stream. This will run within a socket, sending data out from Java byte myDataIn, myDataOut; // declare some variables to store the data we're sending and receiving //NOTE that in processing a byte is from 127 to -128 where: //for a byte from 0-255 0->0, 127->127, 128->-128, 255->-1 int DATA=0; //String message; int holdsize; byte[] hold = new byte[30]; //STRING STUFF String message; TextField input = new TextField("msg", 25); Button send = new Button(); PFont font; //GRAB message STUFF String InMSG; int numberIN; void setup() { size(500,250); background(0); //SET NETWORK STUFF host = "128.122.151.199"; // define a host to communicate with. This can be a name or IP address port = 10001; // define a port to contact on that host. Must be a number, typically 10001 for an XPort //SET INTERFACE STUFF font = loadFont("ArialMT-24.vlw"); //BUTTON send.setLocation(width-150,100); send.setSize(100,20); send.addActionListener(this); send.setLabel("SEND");//this is the event message add(send); //TEXT INPUT input.addActionListener(this); input.setLocation(100, 100); input.setSize(200,20); add(input); //Storage Variables message = "test"; //GRABBING VARIABLES InMSG=""; numberIN=0; }//END setup void draw () { background(50); smooth (); fill(255); textFont(font); textSize(24); //CHECK NETWORK checkConnection(host, port); // subroutine to create a connection, via a socket, to the XPort //if (dataIsWaiting() == true) // check to see if there's new data waiting to come in //{ // myDataIn = getSomeData(); // ... and if there's new data, get it //} //Here when data is waiting we want to grab it all //We put it into this string InMSG //THE question is will it get the whole message within //THIS draw loop? should we make it pause....? if (dataIsWaiting() == true) { //Clear the variables numberIN=0; myDataIn=""; //Grab stuff till it runs out while(dataIsWaiting() == true) { myDataIn = getSomeData(); String extra[] = myDataIn; InMSG=append(InMSG,extra); numberIN=numberIN+1; } } //SPIT IT OUT text(InMSG, 100, 200,170, 170); //SETUP Screen //Title of messenger text("Messenger", 20, 20); //Output text(message, 100, 170,170, 170); input.addActionListener(this); }//END draw //INTERFACE FUNCTIONS void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command == "SEND") { message = getText(input);//message gets whatever is in the text box //NOW WE MUST SEND IT!!!!! //So this is the test send a message section //We want to take a string and break it up into an array of bytes and then send them //FIRST we load the hold array with the message //NOTE this should be given a max size myDataOut = 65; sendSomeData(myDataOut); holdsize=message.length(); myDataOut = (byte) holdsize; sendSomeData(myDataOut); for(int i=0; i0) // check to see if any bytes are available { bytesAvailable = true; // ...and if they are set the variable to true println(myInputStream.available() + " bytes available..."); } } catch(Exception e) { e.printStackTrace(); println("error while checking for bytes available"); } return bytesAvailable; } ////////GET SOME DATA \\\\\\\\\\ byte getSomeData() { byte inData = 0; // declare and initialize the data variable try { if (myInputStream.available()>0) // only read the byte if there's a byte to read [this is a redundant check] { inData = myInputStream.readByte(); // read a byte from the input stream println("data received: " + inData); } } catch(Exception e) { e.printStackTrace(); println("no data"); } return inData; } ////////SEND SOME DATA\\\\\\\\\\ void sendSomeData(byte outData) { if (myOutputStream == null) // if there's no active output stream { try { myOutputStream = new DataOutputStream(mySocket.getOutputStream()); // create an new output stream from a particular socket } catch (Exception e) { e.printStackTrace(); // println("no output stream"); } } try { myOutputStream.writeByte(outData); // write a byte to the output stream //println("data sent: " + outData); } catch(Exception e) { e.printStackTrace(); //println("event send failed"); } } public void stop() // when the program quits { try { myInputStream.close(); // close the input stream myOutputStream.close(); // close the output stream mySocket.close(); // close the socket } catch (Exception e) { e.printStackTrace(); println("couldn't close connection"); } } }//END Messenger