summaryrefslogtreecommitdiffstats
path: root/libraries/oscP5/src/oscP5/OscNetManager.java
blob: dd4f614aaa7b019b4d59e160e5224c6b9b1a6d72 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
 * 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.DatagramPacket;
import java.util.ArrayList;

import netP5.AbstractMulticast;
import netP5.AbstractTcpClient;
import netP5.Logger;
import netP5.Multicast;
import netP5.NetAddress;
import netP5.NetAddressList;
import netP5.TcpClient;
import netP5.TcpPacket;
import netP5.TcpPacketListener;
import netP5.TcpServer;
import netP5.UdpClient;
import netP5.UdpPacketListener;
import netP5.UdpServer;


/**
 * @invisible
 */

public class OscNetManager
    implements UdpPacketListener, TcpPacketListener {

  protected OscProperties _myOscProperties;

  protected UdpClient _myUdpClient = null;

  protected UdpServer _myUdpServer = null;

  protected TcpServer _myTcpServer = null;

  protected TcpClient _myTcpClient = null;

  protected boolean isTcpClient = false;

  protected boolean isTcpServer = false;

  protected AbstractMulticast _myMulticast = null;

  protected ArrayList<UdpPacketListener> _myUdpListener = new ArrayList<UdpPacketListener>();

  protected ArrayList<TcpPacketListener> _myTcpListener = new ArrayList<TcpPacketListener>();

  public final static int NONE = 0;

  public void start(final OscProperties theOscProperties) {
    stop();
    _myOscProperties = theOscProperties;
    int networkProtocol = _myOscProperties.networkProtocol();
	switch (networkProtocol) {
      case (OscProperties.UDP):
        newUdp();
        break;
      case (OscProperties.MULTICAST):
        newMulticast();
        break;
      case (OscProperties.TCP):
        newTcp();
        break;
    }
    _myOscProperties.isLocked = true;
  }


  protected void stop() {
    _myUdpClient = null;
    if (_myMulticast != null) {
    	Logger.printDebug("OscP5.stop", "multicast.");
      _myMulticast.dispose();
    }
    if (_myUdpServer != null) {
    	Logger.printDebug("OscP5.stop", "stopping udpserver.");
      _myUdpServer.dispose();
    }
    _myMulticast = null;
    _myUdpServer = null;
    Logger.printProcess("OscP5", "stopped.");
  }


  private void newUdp() {
    if (_myOscProperties.remoteAddress() != null && _myOscProperties.remoteAddress().isvalid()) {
      _myUdpClient = new UdpClient(_myOscProperties.remoteAddress().address(), _myOscProperties.remoteAddress().port());
    }
    else {
      _myUdpClient = new UdpClient();
    }

    if (_myOscProperties.listeningPort() > 0) {
      _myUdpServer = new UdpServer(this, _myOscProperties.listeningPort(), _myOscProperties.datagramSize());
    }
  }


  private void newTcp() {
    if (_myOscProperties.listeningPort() > 0) {
      _myTcpServer = new TcpServer(this, _myOscProperties.listeningPort(), TcpServer.MODE_STREAM);
      isTcpServer = true;
    }
    else if (_myOscProperties.remoteAddress().isvalid()) {
      _myTcpClient = new TcpClient(
          this,
          _myOscProperties.remoteAddress().address(),
          _myOscProperties.remoteAddress().port(),
          TcpClient.MODE_STREAM);
      isTcpClient = true;
    }
  }


  private void newMulticast() {
    if (_myOscProperties.remoteAddress() != null && _myOscProperties.remoteAddress().isvalid()) {
      _myMulticast = new Multicast(
          this,
          _myOscProperties.remoteAddress().address(),
          _myOscProperties.remoteAddress().port(),
          _myOscProperties.datagramSize());

    }
    else {
      // ESCA-JAVA0266:
    System.out.println("ERROR @ Multicast");
    }

  }


  public void setTimeToLive(final int theTTL) {
    if (_myMulticast != null) {
      _myMulticast.setTimeToLive(theTTL);
    }
    else {
      Logger.printWarning("OscNetManager.setTimeToLive", "only supported for multicast session.");
    }
  }


  public TcpServer tcpServer() {
    return _myTcpServer;
  }


  public TcpClient tcpClient() {
    return _myTcpClient;
  }


  /**
   * @param theListener DatagramPacketListener
   */
  public void addUdpListener(final UdpPacketListener theListener) {
    _myUdpListener.add(theListener);
  }


  /**
   * @param theListener DatagramPacketListener
   */
  public void removeUdpListener(final UdpPacketListener theListener) {
    _myUdpListener.remove(theListener);
  }


  /**
   * @param theListener TcpPacketListener
   */
  public void addTcpListener(final TcpPacketListener theListener) {
    _myTcpListener.add(theListener);
  }


  /**
   * @param theListener TcpPacketListener
   */
  public void removeTcpListener(final TcpPacketListener theListener) {
    _myTcpListener.remove(theListener);
  }


  /**
   * @param thePacket OscPacket
   */
  public void send(final OscPacket thePacket) {
    if (_myOscProperties.sendStatus() == false && _myOscProperties.networkProtocol() != OscProperties.TCP) {
      Logger.printWarning("OscNetManager.send", "please specify a remote address. send(OscPacket theOscPacket) "
                          + "is only supported when there is a host specified in OscProperties.");
    }
    else {
      try {
        switch (_myOscProperties.networkProtocol()) {
          case (OscProperties.UDP):
            if (_myOscProperties.srsp()) {
              _myUdpServer.send(
                  thePacket.getBytes(),
                  _myOscProperties.remoteAddress().inetaddress(),
                  _myOscProperties.remoteAddress().port());

            }
            else {
              _myUdpClient.send(
                  thePacket.getBytes(),
                  _myOscProperties.remoteAddress().inetaddress(),
                  _myOscProperties.remoteAddress().port());
            }
            break;
          case (OscProperties.TCP):
            if (isTcpServer) {
              _myTcpServer.send(thePacket.getBytes());
            }
            else if (isTcpClient) {
              _myTcpClient.send(thePacket.getBytes());
            }
            break;
          case (OscProperties.MULTICAST):
            _myMulticast.send(thePacket.getBytes());
            break;
        }
      }
      catch (final NullPointerException e) {
        Logger.printError("OscManager.send", "NullPointerException " + e);
      }
    }
  }


  public void send(final DatagramPacket thePacket) {
    if (_myOscProperties.srsp()) {
      _myUdpServer.send(thePacket);
    }
    else {
      _myUdpClient.send(thePacket);
    }
  }


  /**
   * @param thePacket OscPacket
   * @param theAddress String
   * @param thePort int
   */
  public void send(final OscPacket thePacket, final String theAddress, final int thePort) {
    try {
      switch (_myOscProperties.networkProtocol()) {
        case (OscProperties.UDP):
          if (_myOscProperties.srsp()) {
            _myUdpServer.send(thePacket.getBytes(), theAddress, thePort);
          }
          else {
            _myUdpClient.send(thePacket.getBytes(), theAddress, thePort);
          }
          break;
        case (OscProperties.MULTICAST):
          _myMulticast.send(thePacket.getBytes());
          break;
        case (OscProperties.TCP):
          Logger.printWarning(
              "OscP5.send",
              "send(OscPacket thePacket,String theAddress,int thePort) is not supported in TCP mode.");
          break;
      }
    }
    catch (final NullPointerException e) {
      Logger.printError("OscP5.send", "NullPointerException " + e);
    }
  }


  /**
   * @param thePacket OscPacket
   * @param theList OscHostList
   */
  public void send(final OscPacket thePacket, final NetAddressList theList) {
    switch (_myOscProperties.networkProtocol()) {
      case (OscProperties.UDP):
        final byte[] myBytes = thePacket.getBytes();
        final DatagramPacket myPacket = new DatagramPacket(myBytes, myBytes.length);
        for (int i = 0; i < theList.list().size(); i++) {
          myPacket.setAddress(theList.get(i).inetaddress());
          myPacket.setPort(theList.get(i).port());
          send(myPacket);
        }
        break;
      case (OscProperties.TCP):
        Logger.printWarning(
            "OscP5.send",
            "send(OscPacket thePacket,NetAddressList theList) is not supported in TCP mode.");
        break;
    }
  }


  /**
   * @param thePacket OscPacket
   * @param theHost NetAddress
   */
  public void send(final OscPacket thePacket, final NetAddress theHost) {
    switch (_myOscProperties.networkProtocol()) {

      case (OscProperties.UDP):
        if (theHost.isvalid()) {
          final byte[] myBytes = thePacket.getBytes();
          final DatagramPacket myPacket = new DatagramPacket(myBytes, myBytes.length);
          myPacket.setAddress(theHost.inetaddress());
          myPacket.setPort(theHost.port());
          send(myPacket);
        }
        break;
      case (OscProperties.TCP):
        Logger.printWarning("OscP5.send", "send(OscPacket thePacket,NetAddress theHost) is not supported in TCP mode.");
        break;
    }
  }


  /**
   * @param theAddrPattern String
   * @param theArguments Object[]
   */
  public void send(final String theAddrPattern, final Object[] theArguments) {
    send(new OscMessage(theAddrPattern, theArguments));
  }


  public void send(final String theAddrPattern, final Object[] theArguments, final String theAddress, final int thePort) {
    send(new OscMessage(theAddrPattern, theArguments), theAddress, thePort);
  }


  public void send(final String theAddrPattern, final Object[] theArguments, final NetAddressList theList) {
    send(new OscMessage(theAddrPattern, theArguments), theList);
  }


  public void send(final String theAddrPattern, final Object[] theArguments, final NetAddress theHost) {
    send(new OscMessage(theAddrPattern, theArguments), theHost);
  }


  public void process(final DatagramPacket thePacket, final int thePort) {
    for (int i = 0; i < _myUdpListener.size(); i++) {
      _myUdpListener.get(i).process(thePacket, thePort);
    }
  }


  public void process(final TcpPacket thePacket, final int thePort) {
    for (int i = 0; i < _myTcpListener.size(); i++) {
      _myTcpListener.get(i).process(thePacket, thePort);
    }
  }


  public void remove(final AbstractTcpClient theClient) {}


  public void status(final int theIndex) {}
}