| 1 |
import java.util.*; |
|---|
| 2 |
import java.io.*; |
|---|
| 3 |
import java.net.InetAddress; |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
/** |
|---|
| 7 |
* |
|---|
| 8 |
* OscMessage |
|---|
| 9 |
* <BR><BR> |
|---|
| 10 |
* OpenSoundControl packet class. |
|---|
| 11 |
* |
|---|
| 12 |
* @author Ben Chun ben@benchun.net |
|---|
| 13 |
* @version 1.0 |
|---|
| 14 |
*/ |
|---|
| 15 |
|
|---|
| 16 |
public class OscPacket { |
|---|
| 17 |
private long time; |
|---|
| 18 |
private Vector messages; |
|---|
| 19 |
public InetAddress address; |
|---|
| 20 |
public int port; |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
/** |
|---|
| 24 |
* Constructor for all incoming packets and those outgoing packets |
|---|
| 25 |
* whose time, address, and port will be set later. |
|---|
| 26 |
* |
|---|
| 27 |
*/ |
|---|
| 28 |
public OscPacket() { |
|---|
| 29 |
time = 0; |
|---|
| 30 |
messages = new Vector(); |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
/** |
|---|
| 35 |
* Constructor for outgoing packets. |
|---|
| 36 |
* |
|---|
| 37 |
* @param time OSC time tag |
|---|
| 38 |
* @param address destination host |
|---|
| 39 |
* @param port destination port |
|---|
| 40 |
*/ |
|---|
| 41 |
public OscPacket(long time, InetAddress address, int port) { |
|---|
| 42 |
this.time = time; |
|---|
| 43 |
messages = new Vector(); |
|---|
| 44 |
this.address = address; |
|---|
| 45 |
this.port = port; |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
/** |
|---|
| 50 |
* Sets the time. |
|---|
| 51 |
* |
|---|
| 52 |
* @param time the new time |
|---|
| 53 |
*/ |
|---|
| 54 |
public void setTime(long time) { |
|---|
| 55 |
this.time = time; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
/** |
|---|
| 60 |
* Sets the destination address. |
|---|
| 61 |
* |
|---|
| 62 |
* @param address the new address |
|---|
| 63 |
*/ |
|---|
| 64 |
public void setAddress(InetAddress address) { |
|---|
| 65 |
this.address = address; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
/** |
|---|
| 69 |
* Sets the destination port. |
|---|
| 70 |
* |
|---|
| 71 |
* @param port the new port |
|---|
| 72 |
*/ |
|---|
| 73 |
public void setPort(int port) { |
|---|
| 74 |
this.port = port; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
/** |
|---|
| 79 |
* Adds a message to this packet. |
|---|
| 80 |
* |
|---|
| 81 |
* @param message the message to add |
|---|
| 82 |
*/ |
|---|
| 83 |
public void addMessage(OscMessage message) { |
|---|
| 84 |
messages.addElement(message); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
public InetAddress getAddress() { |
|---|
| 88 |
return address; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
public int getPort() { |
|---|
| 92 |
return port; |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
/** |
|---|
| 97 |
* Returns an XML representation of this packet, suitable for |
|---|
| 98 |
* sending to Flash. The return value should validate against |
|---|
| 99 |
* flosc.dtd |
|---|
| 100 |
* |
|---|
| 101 |
*/ |
|---|
| 102 |
public String getXml() { |
|---|
| 103 |
|
|---|
| 104 |
String xml = ""; |
|---|
| 105 |
xml += "<OSCPACKET ADDRESS=\"" + address.getHostAddress() + |
|---|
| 106 |
"\" PORT=\"" + port + |
|---|
| 107 |
"\" TIME=\""+ time + "\">"; |
|---|
| 108 |
|
|---|
| 109 |
Enumeration m = messages.elements(); |
|---|
| 110 |
while (m.hasMoreElements()) { |
|---|
| 111 |
OscMessage mess = (OscMessage)m.nextElement(); |
|---|
| 112 |
xml += mess.getXml(); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
xml += "</OSCPACKET>"; |
|---|
| 116 |
return xml; |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
/** |
|---|
| 120 |
* Returns a byte array representation of this packet, suitable for |
|---|
| 121 |
* sending to OSC client applications. |
|---|
| 122 |
* |
|---|
| 123 |
*/ |
|---|
| 124 |
public byte[] getByteArray() throws IOException { |
|---|
| 125 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|---|
| 126 |
DataOutputStream stream = new DataOutputStream(baos); |
|---|
| 127 |
|
|---|
| 128 |
// bundle |
|---|
| 129 |
if (messages.size() > 1) { |
|---|
| 130 |
baos.write( ("#bundle").getBytes() ); |
|---|
| 131 |
baos.write(0); |
|---|
| 132 |
// bundles have a time tag |
|---|
| 133 |
stream.writeLong(time); |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
// messages |
|---|
| 137 |
Enumeration m = messages.elements(); |
|---|
| 138 |
while (m.hasMoreElements()) { |
|---|
| 139 |
OscMessage mess = (OscMessage)m.nextElement(); |
|---|
| 140 |
byte[] byteArray = mess.getByteArray(); |
|---|
| 141 |
// bundles have message size tags |
|---|
| 142 |
if (messages.size() > 1) { |
|---|
| 143 |
stream.writeInt(byteArray.length); |
|---|
| 144 |
} |
|---|
| 145 |
baos.write(byteArray); |
|---|
| 146 |
} |
|---|
| 147 |
return baos.toByteArray(); |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
/** |
|---|
| 151 |
* Make the stream end on a 4-byte boundary by padding it with |
|---|
| 152 |
* null characters. |
|---|
| 153 |
* |
|---|
| 154 |
* @param stream The stream to align. |
|---|
| 155 |
*/ |
|---|
| 156 |
private void alignStream(ByteArrayOutputStream stream) throws IOException { |
|---|
| 157 |
int pad = 4 - ( stream.size() % 4 ); |
|---|
| 158 |
for (int i = 0; i < pad; i++) |
|---|
| 159 |
stream.write(0); |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
/** |
|---|
| 163 |
* Prints out a byte array in 4-byte lines, useful for debugging. |
|---|
| 164 |
* |
|---|
| 165 |
* @param byteArray The byte array |
|---|
| 166 |
*/ |
|---|
| 167 |
public static void printBytes(byte[] byteArray) { |
|---|
| 168 |
for (int i=0; i<byteArray.length; i++) { |
|---|
| 169 |
System.out.print(byteArray[i] + " (" + (char)byteArray[i] + ") "); |
|---|
| 170 |
if ((i+1)%4 == 0) |
|---|
| 171 |
System.out.print("\n"); |
|---|
| 172 |
} |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
} |
|---|
| 177 |
|
|---|