summaryrefslogtreecommitdiffstats
path: root/libraries/oscP5/src/oscP5/OscPatcher.java
blob: aade541594a6ff237f962fc26e66de97a966fd5b (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
 * An OSC (Open Sound Control) library for processing.
 *
 * (c) 2004-2012
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 * 
 * @author              Andreas Schlegel http://www.sojamo.de
 * @modified    12/23/2012
 * @version             0.9.9
 */

package oscP5;

import java.net.InetAddress;
import java.util.ArrayList;
import netP5.Bytes;
import netP5.TcpClient;

/**
 *
 * @invisible
 */
public abstract class OscPatcher {

  
protected static final byte ZEROBYTE = 0x00;

  protected static final byte KOMMA = 0x2c;

  protected static final long TIMETAG_OFFSET = 2208988800L;

  protected static final long TIEMTAG_NOW = 1;

  protected ArrayList<OscMessage> messages;

  protected byte[] _myAddrPattern;

  protected int _myAddrInt = -1;

  protected byte[] _myTypetag = new byte[0];

  protected byte[] _myData = new byte[0];

  protected Object[] _myArguments;
  
  protected boolean isValid = false;

  protected long timetag = 1;

  protected boolean isArray = false;

  protected byte _myArrayType = 0X00;

  protected OscPatcher() {
  }


  protected int parseBundle(final byte[] theBytes,
                            final InetAddress theAddress, final int thePort,
                            final TcpClient theClient) {
    if (theBytes.length > OscBundle.BUNDLE_HEADER_SIZE) {
      timetag = (new Long(Bytes.toLong(Bytes.copy(theBytes, 8, 8)))).longValue();
      int myPosition = OscBundle.BUNDLE_HEADER_SIZE;
      messages = new ArrayList<OscMessage>();
      int myMessageLength = Bytes.toInt(Bytes.copy(theBytes, myPosition,4));
      while (myMessageLength != 0 && (myMessageLength % 4 == 0)
             && myPosition < theBytes.length) {
        myPosition += 4;
        messages.add(new OscMessage(Bytes.copy(theBytes, myPosition,myMessageLength),
                                    theAddress,
                                    thePort,
                                    timetag,
                                    theClient));
        myPosition += myMessageLength;
        if(myPosition >= theBytes.length)
            break;
        myMessageLength = Bytes.toInt(Bytes.copy(theBytes, myPosition,4));
      }
    }
    for (int i = 0; i < messages.size(); i++) {
      if (!messages.get(i).isValid) {
        messages.remove(messages.get(i));
      }
    }

    if (messages.size() > 0) {
      isValid = true;
    }
    return messages.size();
  }


  protected void parseMessage(final byte[] theBytes) {
    int myLength = theBytes.length;
    int myIndex = 0;
    myIndex = parseAddrPattern(theBytes, myLength, myIndex);
    if (myIndex != -1) {
      myIndex = parseTypetag(theBytes, myLength, myIndex);
    }
    if (myIndex != -1) {
      _myData = Bytes.copy(theBytes, myIndex);
      _myArguments = parseArguments(_myData);
      isValid = true;
    }
  }


  protected int parseAddrPattern(final byte[] theBytes, final int theLength,
                                 final int theIndex) {
    if (theLength > 4 && theBytes[4] == KOMMA) {
      _myAddrInt = Bytes.toInt(Bytes.copy(theBytes, 0, 4));
    }
    for (int i = theIndex; i < theLength; i++) {
      if (theBytes[i] == ZEROBYTE) {
        _myAddrPattern = Bytes.copy(theBytes, theIndex, i);
        return i + align(i);
      }
    }
    return -1;
  }


  protected int parseTypetag(final byte[] theBytes, final int theLength,
                             int theIndex) {
    if (theBytes[theIndex] == KOMMA) {
      theIndex++;
      for (int i = theIndex; i < theLength; i++) {
        if (theBytes[i] == ZEROBYTE) {
          _myTypetag = Bytes.copy(theBytes, theIndex, i - theIndex);
          return i + align(i);
        }
      }
    }
    return -1;
  }


  /**
   * cast the arguments passed with the incoming osc message and store them in
   * an object array.
   *
   * @param theBytes
   * @return
   */
  protected Object[] parseArguments(final byte[] theBytes) {
    Object[] myArguments = new Object[0];
    int myTagIndex = 0;
    int myIndex = 0;
    myArguments = new Object[_myTypetag.length];
    isArray = (_myTypetag.length > 0) ? true : false;
    while (myTagIndex < _myTypetag.length) {
      /* check if we still save the arguments as an array */
      if (myTagIndex == 0) {
        _myArrayType = _myTypetag[myTagIndex];
      } else {
        if (_myTypetag[myTagIndex] != _myArrayType) {
          isArray = false;
        }
      }
      switch (_myTypetag[myTagIndex]) {
      case (0x63): // char c
        myArguments[myTagIndex] = (new Character((char) (Bytes
                .toInt(Bytes.copy(theBytes, myIndex, 4)))));
        myIndex += 4;
        break;
      case (0x69): // int i
        myArguments[myTagIndex] = (new Integer(Bytes.toInt(Bytes.copy(
                theBytes, myIndex, 4))));
        myIndex += 4;
        break;
      case (0x66): // float f
        myArguments[myTagIndex] = (new Float(Bytes.toFloat(Bytes.copy(
                theBytes, myIndex, 4))));
        myIndex += 4;

        break;
      case (0x6c): // long l
      case (0x68): // long h
        myArguments[myTagIndex] = (new Long(Bytes.toLong(Bytes.copy(
                theBytes, myIndex, 8))));
        myIndex += 8;
        break;
      case (0x64): // double d
        myArguments[myTagIndex] = (new Double(Bytes.toDouble(Bytes
                .copy(theBytes, myIndex, 8))));
        myIndex += 8;
        break;
      case (0x53): // Symbol S
      case (0x73): // String s
        int newIndex = myIndex;
        StringBuffer stringBuffer = new StringBuffer();

        stringLoop:
                do {
          if (theBytes[newIndex] == 0x00) {
            break stringLoop;
          } else {
            stringBuffer.append((char) theBytes[newIndex]);
          }
          newIndex++;
        } while (newIndex < theBytes.length);

        myArguments[myTagIndex] = (stringBuffer.toString());
        myIndex = newIndex + align(newIndex);
        break;
      case 0x62: // byte[] b - blob
        int myLen = Bytes.toInt(Bytes.copy(theBytes, myIndex, 4));
        myIndex += 4;
        myArguments[myTagIndex] = Bytes.copy(theBytes, myIndex, myLen);
        myIndex += myLen + (align(myLen) % 4);
        break;
      case 0x6d: // midi m
        myArguments[myTagIndex] = Bytes.copy(theBytes, myIndex, 4);
        myIndex += 4;
        break;
        /*
         * no arguments for typetags T,F,N T = true F = false N = false
         */
      }
      myTagIndex++;
    }
    _myData = Bytes.copy(_myData, 0, myIndex);
    return myArguments;
  }


  protected static int align(int theInt) {
    return (4 - (theInt % 4));
  }

}