added reference to network library.

This commit is contained in:
sansumbrella
2010-01-05 08:47:47 +00:00
parent 2a975934c6
commit 38df2387d5
4 changed files with 225 additions and 77 deletions

View File

@@ -31,6 +31,10 @@ import processing.core.PApplet;
/**
* XMLElement is a representation of an XML object. The object is able to parse XML code. The methods described here are the most basic. More are documented in the Developer's Reference.
* <br><br>
* The encoding parameter inside XML files is ignored, only UTF-8 (or plain ASCII) are parsed properly.
* =advanced
* XMLElement is an XML element. This is the base class used for the
* Processing XML library, representing a single node of an XML tree.
*
@@ -38,6 +42,10 @@ import processing.core.PApplet;
*
* @author Marc De Scheemaecker
* @author processing.org
*
* @webref data:composite
* @usage Web &amp; Application
* @instanceName xml any variable of type XMLElement
*/
public class XMLElement implements Serializable {
@@ -103,6 +111,7 @@ public class XMLElement implements Serializable {
/**
* Creates an empty element to be used for #PCDATA content.
* @nowebref
*/
public XMLElement() {
this(null, null, null, NO_LINE);
@@ -173,6 +182,7 @@ public class XMLElement implements Serializable {
* @param namespace the namespace URI.
* @param systemID the system ID of the XML data where the element starts.
* @param lineNr the line in the XML data where the element starts.
* @nowebref
*/
public XMLElement(String fullName,
String namespace,
@@ -204,21 +214,25 @@ public class XMLElement implements Serializable {
* wraps exception handling, for more advanced exception handling,
* use the constructor that takes a Reader or InputStream.
* @author processing.org
* @param filename
* @param parent
* @param filename name of the XML file to load
* @param parent typically use "this"
*/
public XMLElement(PApplet parent, String filename) {
this();
parseFromReader(parent.createReader(filename));
}
/**
* @nowebref
*/
public XMLElement(Reader r) {
this();
parseFromReader(r);
}
/**
* @nowebref
*/
public XMLElement(String xml) {
this();
parseFromReader(new StringReader(xml));
@@ -348,6 +362,8 @@ public class XMLElement implements Serializable {
* Returns the full name (i.e. the name including an eventual namespace
* prefix) of the element.
*
* @webref
* @brief Returns the name of the element.
* @return the name, or null if the element only contains #PCDATA.
*/
public String getName() {
@@ -507,9 +523,12 @@ public class XMLElement implements Serializable {
/**
* Returns the number of children.
* Returns the number of children for the element.
*
* @return the count.
* @webref
* @see processing.xml.XMLElement#getChild(int)
* @see processing.xml.XMLElement#getChildren(String)
*/
public int getChildCount() {
return this.children.size();
@@ -554,27 +573,35 @@ public class XMLElement implements Serializable {
/**
* Quick accessor for an element at a particular index.
* @author processing.org
* @param index the element
*/
public XMLElement getChild(int which) {
return (XMLElement) children.elementAt(which);
public XMLElement getChild(int index) {
return (XMLElement) children.elementAt(index);
}
/**
* Get a child by its name or path.
* @param name element name or path/to/element
* Returns the child XMLElement as specified by the <b>index</b> parameter. The value of the <b>index</b> parameter must be less than the total number of children to avoid going out of the array storing the child elements.
* When the <b>path</b> parameter is specified, then it will return all children that match that path. The path is a series of elements and sub-elements, separated by slashes.
*
* @return the element
* @author processing.org
*
* @webref
* @see processing.xml.XMLElement#getChildCount()
* @see processing.xml.XMLElement#getChildren(String)
* @brief Get a child by its name or path.
* @param path path to a particular element
*/
public XMLElement getChild(String name) {
if (name.indexOf('/') != -1) {
return getChildRecursive(PApplet.split(name, '/'), 0);
public XMLElement getChild(String path) {
if (path.indexOf('/') != -1) {
return getChildRecursive(PApplet.split(path, '/'), 0);
}
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
XMLElement kid = getChild(i);
String kidName = kid.getName();
if (kidName != null && kidName.equals(name)) {
if (kidName != null && kidName.equals(path)) {
return kid;
}
}
@@ -681,20 +708,27 @@ public class XMLElement implements Serializable {
/**
* Get any children that match this name or path. Similar to getChild(),
* but will grab multiple matches rather than only the first.
* @param name element name or path/to/element
* Returns all of the children as an XMLElement array.
* When the <b>path</b> parameter is specified, then it will return all children that match that path.
* The path is a series of elements and sub-elements, separated by slashes.
*
* @param path element name or path/to/element
* @return array of child elements that match
* @author processing.org
*
* @webref
* @brief Returns all of the children as an XMLElement array.
* @see processing.xml.XMLElement#getChildCount()
* @see processing.xml.XMLElement#getChild(int)
*/
public XMLElement[] getChildren(String name) {
if (name.indexOf('/') != -1) {
return getChildrenRecursive(PApplet.split(name, '/'), 0);
public XMLElement[] getChildren(String path) {
if (path.indexOf('/') != -1) {
return getChildrenRecursive(PApplet.split(path, '/'), 0);
}
// if it's a number, do an index instead
// (returns a single element array, since this will be a single match
if (Character.isDigit(name.charAt(0))) {
return new XMLElement[] { getChild(Integer.parseInt(name)) };
if (Character.isDigit(path.charAt(0))) {
return new XMLElement[] { getChild(Integer.parseInt(path)) };
}
int childCount = getChildCount();
XMLElement[] matches = new XMLElement[childCount];
@@ -702,7 +736,7 @@ public class XMLElement implements Serializable {
for (int i = 0; i < childCount; i++) {
XMLElement kid = getChild(i);
String kidName = kid.getName();
if (kidName != null && kidName.equals(name)) {
if (kidName != null && kidName.equals(path)) {
matches[matchCount++] = kid;
}
}
@@ -882,12 +916,22 @@ public class XMLElement implements Serializable {
}
}
public String getStringAttribute(String name) {
return getAttribute(name);
}
/**
* Returns a String attribute of the element.
* If the <b>default</b> parameter is used and the attribute doesn't exist, the <b>default</b> value is returned.
* When using the version of the method without the <b>default</b> parameter, if the attribute doesn't exist, the value 0 is returned.
*
* @webref
* @param name the name of the attribute
* @param default Value value returned if the attribute is not found
*
* @brief Returns a String attribute of the element.
*/
public String getStringAttribute(String name, String defaultValue) {
return getAttribute(name, defaultValue);
}
@@ -899,18 +943,24 @@ public class XMLElement implements Serializable {
return getAttribute(name, namespace, defaultValue);
}
/**
* Returns an integer attribute of the element.
*/
public int getIntAttribute(String name) {
return getIntAttribute(name, 0);
}
/**
* Returns the value of an attribute.
* Returns an integer attribute of the element.
* If the <b>default</b> parameter is used and the attribute doesn't exist, the <b>default</b> value is returned.
* When using the version of the method without the <b>default</b> parameter, if the attribute doesn't exist, the value 0 is returned.
*
* @param name the non-null full name of the attribute.
* @param defaultValue the default value of the attribute.
* @param name the name of the attribute
* @param defaultValue value returned if the attribute is not found
*
* @webref
* @brief Returns an integer attribute of the element.
* @return the value, or defaultValue if the attribute does not exist.
*/
public int getIntAttribute(String name,
@@ -944,12 +994,17 @@ public class XMLElement implements Serializable {
/**
* Returns the value of an attribute.
* Returns a float attribute of the element.
* If the <b>default</b> parameter is used and the attribute doesn't exist, the <b>default</b> value is returned.
* When using the version of the method without the <b>default</b> parameter, if the attribute doesn't exist, the value 0 is returned.
*
* @param name the non-null full name of the attribute.
* @param defaultValue the default value of the attribute.
* @param name the name of the attribute
* @param defaultValue value returned if the attribute is not found
*
* @return the value, or defaultValue if the attribute does not exist.
*
* @webref
* @brief Returns a float attribute of the element.
*/
public float getFloatAttribute(String name,
float defaultValue) {
@@ -966,6 +1021,7 @@ public class XMLElement implements Serializable {
* @param defaultValue the default value of the attribute.
*
* @return the value, or defaultValue if the attribute does not exist.
* @nowebref
*/
public float getFloatAttribute(String name,
String namespace,
@@ -1253,11 +1309,15 @@ public class XMLElement implements Serializable {
/**
* Returns the content of an element. If there is no such content, <b>null</b> is returned.
* =advanced
* Return the #PCDATA content of the element. If the element has a
* combination of #PCDATA content and child elements, the #PCDATA
* sections can be retrieved as unnamed child objects. In this case,
* this method returns null.
*
* @webref
* @brief Returns the content of an element
* @return the content.
*/
public String getContent() {

View File

@@ -30,7 +30,17 @@ import java.io.*;
import java.lang.reflect.*;
import java.net.*;
/**
* A client connects to a server and sends data back and forth.
* If anything goes wrong with the connection,
* for example the host is not there or is listening on a different port,
* an exception is thrown.
*
* @webref
* @brief The client class is used to create client Objects which connect to a server to exchange data.
* @instanceName client any variable of type Client
* @usage Application
*/
public class Client implements Runnable {
PApplet parent;
@@ -50,7 +60,12 @@ public class Client implements Runnable {
int bufferIndex;
int bufferLast;
/**
*
* @param parent typically use "this"
* @param host address of the server
* @param port port to read/write from on the server
*/
public Client(PApplet parent, String host, int port) {
this.parent = parent;
this.host = host;
@@ -95,7 +110,10 @@ public class Client implements Runnable {
}
}
/**
* @param socket any object of type Socket
* @throws IOException
*/
public Client(PApplet parent, Socket socket) throws IOException {
this.socket = socket;
@@ -108,6 +126,8 @@ public class Client implements Runnable {
/**
* Disconnects from the server. Use to shut the connection when you're finished with the Client.
* =advanced
* Disconnect from the server and calls disconnectEvent(Client c)
* in the host PApplet.
* <P/>
@@ -115,6 +135,8 @@ public class Client implements Runnable {
* while your applet is still running. Otherwise, it will be
* automatically be shut down by the host PApplet
* (using dispose, which is identical)
* @webref
* @brief Disconnects from the server
*/
public void stop() {
dispose();
@@ -208,7 +230,9 @@ public class Client implements Runnable {
/**
* Returns the ip address of this feller as a String.
* Returns the IP address of the computer to which the Client is attached.
* @brief Returns the IP address of the machine as a String
* @webref
*/
public String ip() {
return socket.getInetAddress().getHostAddress();
@@ -216,8 +240,9 @@ public class Client implements Runnable {
/**
* Returns the number of bytes that have been read from serial
* and are waiting to be dealt with by the user.
* Returns the number of bytes available. When any client has bytes available from the server, it returns the number of bytes.
* @webref
* @brief Returns the number of bytes in the buffer waiting to be read
*/
public int available() {
return (bufferLast - bufferIndex);
@@ -225,7 +250,10 @@ public class Client implements Runnable {
/**
* Ignore all the bytes read so far and empty the buffer.
* Empty the buffer, removes all the data stored there.
*
* @webref
* @brief Clears the buffer
*/
public void clear() {
bufferLast = 0;
@@ -234,10 +262,11 @@ public class Client implements Runnable {
/**
* Returns a number between 0 and 255 for the next byte that's
* waiting in the buffer.
* Returns -1 if there was no byte (although the user should
* first check available() to see if things are ready to avoid this)
* Returns a number between 0 and 255 for the next byte that's waiting in the buffer.
* Returns -1 if there is no byte, although this should be avoided by first cheacking <b>available()</b> to see if any data is available.
*
* @webref
* @brief Returns a value from the buffer
*/
public int read() {
if (bufferIndex == bufferLast) return -1;
@@ -256,6 +285,9 @@ public class Client implements Runnable {
/**
* Returns the next byte in the buffer as a char.
* Returns -1, or 0xffff, if nothing is there.
*
* @webref
* @brief Returns the next byte in the buffer as a char
*/
public char readChar() {
if (bufferIndex == bufferLast) return (char)(-1);
@@ -264,10 +296,20 @@ public class Client implements Runnable {
/**
* Reads a group of bytes from the buffer.
* The version with no parameters returns a byte array of all data in the buffer.
* This is not efficient, but is easy to use.
* The version with the <b>byteBuffer</b> parameter is more memory and time efficient.
* It grabs the data in the buffer and puts it into the byte array passed in and returns an int value for the number of bytes read.
* If more bytes are available than can fit into the <b>byteBuffer</b>, only those that fit are read.
* =advanced
* Return a byte array of anything that's in the serial buffer.
* Not particularly memory/speed efficient, because it creates
* a byte array on each read, but it's easier to use than
* readBytes(byte b[]) (see below).
*
* @webref
* @brief Reads everything in the buffer
*/
public byte[] readBytes() {
if (bufferIndex == bufferLast) return null;
@@ -292,14 +334,16 @@ public class Client implements Runnable {
* Returns an int for how many bytes were read. If more bytes
* are available than can fit into the byte array, only those
* that will fit are read.
*
* @param bytebuffer passed in byte array to be altered
*/
public int readBytes(byte outgoing[]) {
public int readBytes(byte bytebuffer[]) {
if (bufferIndex == bufferLast) return 0;
synchronized (buffer) {
int length = bufferLast - bufferIndex;
if (length > outgoing.length) length = outgoing.length;
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
if (length > bytebuffer.length) length = bytebuffer.length;
System.arraycopy(buffer, bufferIndex, bytebuffer, 0, length);
bufferIndex += length;
if (bufferIndex == bufferLast) {
@@ -312,9 +356,16 @@ public class Client implements Runnable {
/**
* Reads from the serial port into a buffer of bytes up to and
* including a particular character. If the character isn't in
* the serial buffer, then 'null' is returned.
* Reads from the port into a buffer of bytes up to and including a particular character.
* If the character isn't in the buffer, 'null' is returned.
* The version with no <b>byteBuffer</b> parameter returns a byte array of all data up to and including the <b>interesting</b> byte.
* This is not efficient, but is easy to use. The version with the <b>byteBuffer</b> parameter is more memory and time efficient.
* It grabs the data in the buffer and puts it into the byte array passed in and returns an int value for the number of bytes read.
* If the byte buffer is not large enough, -1 is returned and an error is printed to the message area. If nothing is in the buffer, 0 is returned.
*
* @webref
* @brief Reads from the buffer of bytes up to and including a particular character
* @param interesting character designated to mark the end of the data
*/
public byte[] readBytesUntil(int interesting) {
if (bufferIndex == bufferLast) return null;
@@ -353,8 +404,10 @@ public class Client implements Runnable {
* and an error message is printed on the console.
* If nothing is in the buffer, zero is returned.
* If 'interesting' byte is not in the buffer, then 0 is returned.
*
* @param byteBuffer passed in byte array to be altered
*/
public int readBytesUntil(int interesting, byte outgoing[]) {
public int readBytesUntil(int interesting, byte byteBuffer[]) {
if (bufferIndex == bufferLast) return 0;
byte what = (byte)interesting;
@@ -369,14 +422,14 @@ public class Client implements Runnable {
if (found == -1) return 0;
int length = found - bufferIndex + 1;
if (length > outgoing.length) {
if (length > byteBuffer.length) {
System.err.println("readBytesUntil() byte buffer is" +
" too small for the " + length +
" bytes up to and including char " + interesting);
return -1;
}
//byte outgoing[] = new byte[length];
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
System.arraycopy(buffer, bufferIndex, byteBuffer, 0, length);
bufferIndex += length;
if (bufferIndex == bufferLast) {
@@ -389,12 +442,14 @@ public class Client implements Runnable {
/**
* Return whatever has been read from the serial port so far
* as a String. It assumes that the incoming characters are ASCII.
*
* If you want to move Unicode data, you can first convert the
* String to a byte stream in the representation of your choice
* Returns the all the data from the buffer as a String.
* This method assumes the incoming characters are ASCII.
* If you want to transfer Unicode data,
* first convert the String to a byte stream in the representation of your choice
* (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
*
* @webref
* @brief Returns the buffer as a String
*/
public String readString() {
if (bufferIndex == bufferLast) return null;
@@ -403,13 +458,16 @@ public class Client implements Runnable {
/**
* Combination of readBytesUntil and readString. See caveats in
* each function. Returns null if it still hasn't found what
* you're looking for.
* Combination of <b>readBytesUntil()</b> and <b>readString()</b>. Returns <b>null</b> if it doesn't find what you're looking for.
* =advanced
* <p/>
* If you want to move Unicode data, you can first convert the
* String to a byte stream in the representation of your choice
* (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
*
* @webref
* @brief Returns the buffer as a String up to and including a particular character
* @param interesting character designated to mark the end of the data
*/
public String readStringUntil(int interesting) {
byte b[] = readBytesUntil(interesting);
@@ -419,11 +477,15 @@ public class Client implements Runnable {
/**
* This will handle ints, bytes and chars transparently.
* Writes data to a server specified when constructing the client.
*
* @webref
* @brief Writes bytes, chars, ints, bytes[], Strings
* @param data data to write
*/
public void write(int what) { // will also cover char
public void write(int data) { // will also cover char
try {
output.write(what & 0xff); // for good measure do the &
output.write(data & 0xff); // for good measure do the &
output.flush(); // hmm, not sure if a good idea
} catch (Exception e) { // null pointer or serial port dead
@@ -437,9 +499,9 @@ public class Client implements Runnable {
}
public void write(byte bytes[]) {
public void write(byte data[]) {
try {
output.write(bytes);
output.write(data);
output.flush(); // hmm, not sure if a good idea
} catch (Exception e) { // null pointer or serial port dead
@@ -464,8 +526,8 @@ public class Client implements Runnable {
* String to a byte stream in the representation of your choice
* (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
*/
public void write(String what) {
write(what.getBytes());
public void write(String data) {
write(data.getBytes());
}

View File

@@ -30,7 +30,17 @@ import java.io.*;
import java.lang.reflect.*;
import java.net.*;
/**
* A server sends and receives data to and from its associated clients (other programs connected to it).
* When a server is started, it begins listening for connections on the port specified by the <b>port</b> parameter.
* Computers have many ports for transferring data and some are commonly used so be sure to not select one of these.
* For example, web servers usually use port 80 and POP mail uses port 110.
*
* @webref
* @brief The server class is used to create server objects which send and receives data to and from its associated clients (other programs connected to it).
* @instanceName server any variable of type Server
* @usage Application
*/
public class Server implements Runnable {
PApplet parent;
@@ -45,7 +55,11 @@ public class Server implements Runnable {
/** Array of client objects, useful length is determined by clientCount. */
public Client[] clients;
/**
*
* @param parent typically use "this"
* @param port port used to transfer data
*/
public Server(PApplet parent, int port) {
this.parent = parent;
this.port = port;
@@ -82,6 +96,8 @@ public class Server implements Runnable {
/**
* Disconnect a particular client.
* @webref
* @param client the client to disconnect
*/
public void disconnect(Client client) {
//client.stop();
@@ -128,7 +144,8 @@ public class Server implements Runnable {
int lastAvailable = -1;
/**
* Returns the next client in line that has something to say.
* Returns the next client in line with a new message
* @webref
*/
public Client available() {
synchronized (clients) {
@@ -149,11 +166,13 @@ public class Server implements Runnable {
/**
* Disconnect all clients and stop the server.
* Disconnects all clients and stops the server
* =advanced
* <p/>
* Use this to shut down the server if you finish using it while your applet
* is still running. Otherwise, it will be automatically be shut down by the
* host PApplet using dispose(), which is identical.
* @webref
*/
public void stop() {
dispose();
@@ -219,11 +238,15 @@ public class Server implements Runnable {
/**
* Write a value to all the connected clients.
* See Client.write() for operational details.
*
* @webref
* @brief Writes data to all connected clients
* @param data data to write
*/
public void write(int what) { // will also cover char
public void write(int data) { // will also cover char
int index = 0;
while (index < clientCount) {
clients[index].write(what);
clients[index].write(data);
if (clients[index].active()) {
index++;
} else {
@@ -237,10 +260,10 @@ public class Server implements Runnable {
* Write a byte array to all the connected clients.
* See Client.write() for operational details.
*/
public void write(byte what[]) {
public void write(byte data[]) {
int index = 0;
while (index < clientCount) {
clients[index].write(what);
clients[index].write(data);
if (clients[index].active()) {
index++;
} else {
@@ -254,10 +277,10 @@ public class Server implements Runnable {
* Write a String to all the connected clients.
* See Client.write() for operational details.
*/
public void write(String what) {
public void write(String data) {
int index = 0;
while (index < clientCount) {
clients[index].write(what);
clients[index].write(data);
if (clients[index].active()) {
index++;
} else {

View File

@@ -31,7 +31,10 @@ import java.io.*;
import java.util.*;
import java.lang.reflect.*;
/**
* Class for sending and receiving data using the serial communication protocol.
* @webref
*/
public class Serial implements SerialPortEventListener {
PApplet parent;