forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOSCBundle.java
More file actions
190 lines (167 loc) · 4.92 KB
/
Copy pathOSCBundle.java
File metadata and controls
190 lines (167 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright (C) 2003-2006, C. Ramakrishnan / Illposed Software.
* All rights reserved.
*
* This code is licensed under the BSD 3-Clause license.
* See file LICENSE (or LICENSE.html) for more information.
*/
package com.illposed.osc;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import com.illposed.osc.utility.OSCJavaToByteArrayConverter;
/**
* A bundle represents a collection of OSC packets
* (either messages or other bundles)
* and has a time-tag which can be used by a scheduler to execute
* a bundle in the future,
* instead of immediately.
* {@link OSCMessage}s are executed immediately.
*
* Bundles should be used if you want to send multiple messages to be executed
* atomically together, or you want to schedule one or more messages to be
* executed in the future.
*
* @author Chandrasekhar Ramakrishnan
*/
public class OSCBundle extends OSCPacket {
/**
* 2208988800 seconds -- includes 17 leap years
*/
public static final BigInteger SECONDS_FROM_1900_TO_1970 =
new BigInteger("2208988800");
/**
* The Java representation of an OSC timestamp with the semantics of
* "immediately".
*/
public static final Date TIMESTAMP_IMMEDIATE = new Date(0);
private Date timestamp;
private List<OSCPacket> packets;
/**
* Create a new empty OSCBundle with a timestamp of immediately.
* You can add packets to the bundle with addPacket()
*/
public OSCBundle() {
this(TIMESTAMP_IMMEDIATE);
}
/**
* Create an OSCBundle with the specified timestamp.
* @param timestamp the time to execute the bundle
*/
public OSCBundle(Date timestamp) {
this((Collection<OSCPacket>) null, timestamp);
}
// deprecated since version 1.0, March 2012
/**
* Creates an OSCBundle made up of the given packets
* with a timestamp of now.
* @param packets array of OSCPackets to initialize this object with
* @deprecated
*/
public OSCBundle(OSCPacket[] packets) {
this(packets, TIMESTAMP_IMMEDIATE);
}
/**
* Creates an OSCBundle made up of the given packets
* with a timestamp of now.
* @param packets array of OSCPackets to initialize this object with
*/
public OSCBundle(Collection<OSCPacket> packets) {
this(packets, TIMESTAMP_IMMEDIATE);
}
// deprecated since version 1.0, March 2012
/**
* Creates an OSCBundle, specifying the packets and timestamp.
* @param packets the packets that make up the bundle
* @param timestamp the time to execute the bundle
* @deprecated
*/
public OSCBundle(OSCPacket[] packets, Date timestamp) {
this((packets == null)
? new LinkedList<OSCPacket>()
: Arrays.asList(packets),
timestamp);
}
/**
* Create an OSCBundle, specifying the packets and timestamp.
* @param packets the packets that make up the bundle
* @param timestamp the time to execute the bundle
*/
public OSCBundle(Collection<OSCPacket> packets, Date timestamp) {
if (null == packets) {
this.packets = new LinkedList<OSCPacket>();
} else {
this.packets = new ArrayList<OSCPacket>(packets);
}
this.timestamp = timestamp;
init();
}
/**
* Return the time the bundle will execute.
* @return a Date
*/
public Date getTimestamp() {
return timestamp;
}
/**
* Set the time the bundle will execute.
* @param timestamp Date
*/
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
/**
* Add a packet to the list of packets in this bundle.
* @param packet OSCMessage or OSCBundle
*/
public void addPacket(OSCPacket packet) {
packets.add(packet);
}
/**
* Get the packets contained in this bundle.
* @return the packets contained in this bundle.
*/
public OSCPacket[] getPackets() {
OSCPacket[] packetArray = new OSCPacket[packets.size()];
packets.toArray(packetArray);
return packetArray;
}
/**
* Convert the time-tag (a Java Date) into the OSC byte stream.
* Used Internally.
*/
protected void computeTimeTagByteArray(OSCJavaToByteArrayConverter stream) {
if ((null == timestamp) || (timestamp == TIMESTAMP_IMMEDIATE)) {
stream.write(0);
stream.write(1);
return;
}
long millisecs = timestamp.getTime();
long secsSince1970 = millisecs / 1000;
long secs = secsSince1970 + SECONDS_FROM_1900_TO_1970.longValue();
// this line was cribbed from jakarta commons-net's NTP TimeStamp code
long fraction = ((millisecs % 1000) * 0x100000000L) / 1000;
stream.write((int) secs);
stream.write((int) fraction);
}
/**
* Compute the OSC byte stream representation of the bundle.
* Used Internally.
* @param stream OscPacketByteArrayConverter
*/
protected byte[] computeByteArray(OSCJavaToByteArrayConverter stream) {
stream.write("#bundle");
computeTimeTagByteArray(stream);
byte[] packetBytes;
for (OSCPacket pkg : packets) {
packetBytes = pkg.getByteArray();
stream.write(packetBytes.length);
stream.write(packetBytes);
}
return stream.toByteArray();
}
}