summaryrefslogtreecommitdiffstats
path: root/libraries/oscP5/src/oscP5/OscP5.java
blob: b2643dd2aa601d2fae4a268b9327317381a97bca (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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/**
 * 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.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import netP5.AbstractTcpClient;
import netP5.Logger;
import netP5.NetAddress;
import netP5.NetAddressList;
import netP5.NetInfo;
import netP5.TcpClient;
import netP5.TcpPacket;
import netP5.TcpPacketListener;
import netP5.TcpServer;
import netP5.UdpPacketListener;

/**
 * oscP5 is an osc implementation for the programming environment processing.
 * osc is the acronym for open sound control, a network protocol developed at
 * cnmat, uc berkeley. open sound control is a protocol for communication among
 * computers, sound synthesizers, and other multimedia devices that is optimized
 * for modern networking technology and has been used in many application areas.
 * for further specifications and application implementations please visit the
 * official osc site.
 * 
 * @usage Application
 * @example oscP5sendReceive
 * @related OscProperties
 * @related OscMessage
 * @related OscBundle
 */

/**
 * TODO add better error message handling for oscEvents, see this post
 * http://forum.processing.org/topic/oscp5-major-problems-with-error-handling# 25080000000811163
 */
public class OscP5 implements UdpPacketListener, TcpPacketListener {

	/*
	 * @TODO implement polling option to avoid threading and synchronization
	 * issues. check email from tom lieber. look into mutex objects.
	 * http://www.google.com/search?hl=en&q=mutex+java&btnG=Search
	 */

	// protected ArrayList _myOscPlugList = new ArrayList();

	protected HashMap<String, ArrayList<OscPlug>> _myOscPlugMap = new HashMap<String, ArrayList<OscPlug>>();

	protected NetInfo _myNetInfo;

	private OscNetManager _myOscNetManager;

	protected final static int NONE = OscNetManager.NONE;

	public final static boolean ON = OscProperties.ON;

	public final static boolean OFF = OscProperties.OFF;

	/**
	 * a static variable used when creating an oscP5 instance with a sepcified network protocol.
	 */
	public final static int UDP = OscProperties.UDP;

	/**
	 * a static variable used when creating an oscP5 instance with a sepcified network protocol.
	 */
	public final static int MULTICAST = OscProperties.MULTICAST;

	/**
	 * a static variable used when creating an oscP5 instance with a sepcified network protocol.
	 */
	public final static int TCP = OscProperties.TCP;

	protected final Object parent;

	private OscProperties _myOscProperties;

	private Class<?> _myParentClass;

	private Method _myEventMethod;

	private Class<?> _myEventClass = OscMessage.class;

	private boolean isEventMethod;

	private boolean isBroadcast = false;

	private NetAddress _myBroadcastAddress;

	private boolean isOscIn = false;

	/**
	 * @invisible
	 */
	public static final String VERSION = "0.9.9";


	/**
	 * @param theParent Object
	 * @param theProperties OscProperties
	 * @usage Application
	 */
	public OscP5(final Object theParent, final OscProperties theProperties) {
		welcome();
		parent = theParent;

		registerDispose(parent);

		_myOscProperties = theProperties;
		_myOscNetManager = new OscNetManager();
		_myOscNetManager.start(_myOscProperties);
		if (_myOscProperties.networkProtocol() == OscProperties.TCP) {
			_myOscNetManager.addTcpListener(this);
		}
		else {
			_myOscNetManager.addUdpListener(this);
		}
		isEventMethod = checkEventMethod();
		if (_myOscProperties.networkProtocol() == OscProperties.MULTICAST) {
			Logger.printInfo("OscP5", "is joining a multicast group @ " + _myOscProperties.remoteAddress().address() + ":" + _myOscProperties.remoteAddress().port());
		}
		else {
			Logger.printInfo("OscP5", "is running. you (" + ip() + ") are listening @ port " + _myOscProperties.remoteAddress().port());
		}
	}


	/**
	 * @param theParent Object
	 * @param theAddress String
	 * @param thePort int
	 * @param theMode int
	 * @usage Application
	 */
	public OscP5(final Object theParent, final String theAddress, final int thePort, final int theMode) {
		welcome();
		parent = theParent;
		_myOscProperties = new OscProperties();

		registerDispose(parent);

		switch (theMode) {
		case (MULTICAST):
			_myOscProperties.setNetworkProtocol(MULTICAST);
			_myOscProperties.setRemoteAddress(theAddress, thePort);
			_myOscProperties.setListeningPort(thePort);
			_myOscNetManager = new OscNetManager();
			_myOscNetManager.start(_myOscProperties);
			_myOscNetManager.addUdpListener(this);
			Logger.printInfo("OscP5", "is joining a multicast group @ " + _myOscProperties.remoteAddress().address() + ":" + _myOscProperties.remoteAddress().port());
			break;
		case (UDP):
			_myOscProperties.setRemoteAddress(theAddress, thePort);
			initUDP(thePort);
			break;
		case (TCP):
			_myOscProperties.setNetworkProtocol(TCP);
			_myOscProperties.setRemoteAddress(theAddress, thePort);
			_myOscNetManager = new OscNetManager();
			_myOscNetManager.start(_myOscProperties);
			_myOscNetManager.addTcpListener(this);
			break;
		}
		isEventMethod = checkEventMethod();
	}


	public OscP5(final Object theParent, final int theReceiveAtPort, final int theMode) {
		welcome();
		parent = theParent;

		registerDispose(parent);

		_myOscProperties = new OscProperties();
		switch (theMode) {
		case (UDP):
			initUDP(theReceiveAtPort);
			break;
		case (TCP):
			_myOscProperties.setNetworkProtocol(TCP);
			_myOscProperties.setListeningPort(theReceiveAtPort);
			_myOscNetManager = new OscNetManager();
			_myOscNetManager.start(_myOscProperties);
			_myOscNetManager.addTcpListener(this);
			break;
		case (MULTICAST):
			Logger.printWarning("OscP5", "please specify a multicast address. use " + "OscP5(Object theObject, String theMulticastAddress, int thePort, int theMode)");
			break;
		}
		isEventMethod = checkEventMethod();
	}


	/**
	 * @param theParent Object
	 * @param theReceiveAtPort int
	 * @usage Application
	 */
	public OscP5(final Object theParent, final int theReceiveAtPort) {
		welcome();
		parent = theParent;

		registerDispose(parent);

		initUDP(theReceiveAtPort);
		isEventMethod = checkEventMethod();
	}


	private void welcome() {
		System.out.println("OscP5 " + VERSION + " " + "infos, comments, questions at http://www.sojamo.de/oscP5\n\n");
	}


	private void registerDispose(Object theObject) {
		try {
			Object parent = null;
			String child = "processing.core.PApplet";
			try {
				Class<?> childClass = Class.forName(child);
				Class<?> parentClass = Object.class;

				if (parentClass.isAssignableFrom(childClass)) {
					parent = childClass.newInstance();
					parent = theObject;
				}
			} catch (Exception e) {
				// System.out.println(e);
			}
			try {
				Method method = parent.getClass().getMethod("registerDispose", Object.class);
				try {
					method.invoke(parent, new Object[] { this });
				} catch (IllegalArgumentException e) {
					// System.out.println(e);
				} catch (IllegalAccessException e) {
					// System.out.println(e);
				} catch (InvocationTargetException e) {
					// System.out.println(e);
				}
			} catch (SecurityException e) {
				// System.out.println("fail (1) " + e);
			} catch (NoSuchMethodException e) {
				// System.out.println("fail (2) " + e);
			}
		} catch (NullPointerException e) {
			System.err.println("Register Dispose\n" + e);
		}
	}


	private void initUDP(final int theReceiveAtPort) {
		_myOscProperties = new OscProperties();
		_myOscProperties.setListeningPort(theReceiveAtPort);
		_myOscNetManager = new OscNetManager();
		_myOscNetManager.start(_myOscProperties);
		_myOscNetManager.addUdpListener(this);
		Logger.printInfo("OscP5", "is running. you (" + ip() + ") are listening @ port " + theReceiveAtPort);
	}


	/**
	 * check which eventMethod exists in the Object oscP5 was started from. this is necessary for
	 * backwards compatibility for oscP5 because the previous parameterType for the eventMethod was
	 * OscIn and is now OscMessage.
	 * 
	 * @return boolean
	 * @invisible
	 */
	private boolean checkEventMethod() {
		_myParentClass = parent.getClass();
		try {
			Method[] myMethods = _myParentClass.getDeclaredMethods();
			for (int i = 0; i < myMethods.length; i++) {
				if (myMethods[i].getName().indexOf(_myOscProperties.eventMethod()) != -1) {
					Class<?>[] myClasses = myMethods[i].getParameterTypes();
					if (myClasses.length == 1) {
						_myEventClass = myClasses[0];
						isOscIn = ((_myEventClass.toString()).indexOf("OscIn") != -1) ? true : false;
						break;
					}
				}
			}

		} catch (Throwable e) {
			System.err.println(e);
		}

		String tMethod = _myOscProperties.eventMethod();
		if (tMethod != null) {
			try {
				Class<?>[] tClass = { _myEventClass };
				_myEventMethod = _myParentClass.getDeclaredMethod(tMethod, tClass);
				_myEventMethod.setAccessible(true);
				return true;
			} catch (SecurityException e1) {
				// e1.printStackTrace();
				Logger.printWarning("OscP5.plug", "### security issues in OscP5.checkEventMethod(). (this occures when running in applet mode)");
			} catch (NoSuchMethodException e1) {
			}
		}
		// online fix, since an applet throws a security exception when calling
		// setAccessible(true);
		if (_myEventMethod != null) {
			return true;
		}
		return false;
	}


	/**
	 * get the current version of oscP5.
	 * 
	 * @return String
	 */
	public String version() {
		return VERSION;
	}


	/**
	 * @invisible
	 */
	public void dispose() {
		stop();
	}


	public void addListener(OscEventListener theListener) {
		_myOscProperties.listeners().add(theListener);
	}


	public void removeListener(OscEventListener theListener) {
		_myOscProperties.listeners().remove(theListener);
	}


	public Vector<OscEventListener> listeners() {
		return _myOscProperties.listeners();
	}


	/**
	 * osc messages can be automatically forwarded to a specific method of an object. the plug
	 * method can be used to by-pass parsing raw osc messages - this job is done for you with the
	 * plug mechanism. you can also use the following array-types int[], float[], String[]. (but
	 * only as on single parameter e.g. somemethod(int[] theArray) {} ).
	 * 
	 * @param theObject Object, can be any Object
	 * @param theMethodName String, the method name an osc message should be forwarded to
	 * @param theAddrPattern String, the address pattern of the osc message
	 * @param theTypeTag String
	 * @example oscP5plug
	 * @usage Application
	 */
	public void plug(final Object theObject, final String theMethodName, final String theAddrPattern, final String theTypeTag) {
		final OscPlug myOscPlug = new OscPlug();
		myOscPlug.plug(theObject, theMethodName, theAddrPattern, theTypeTag);
		// _myOscPlugList.add(myOscPlug);
		if (_myOscPlugMap.containsKey(theAddrPattern)) {
			_myOscPlugMap.get(theAddrPattern).add(myOscPlug);
		}
		else {
			ArrayList<OscPlug> myOscPlugList = new ArrayList<OscPlug>();
			myOscPlugList.add(myOscPlug);
			_myOscPlugMap.put(theAddrPattern, myOscPlugList);
		}
	}


	/**
	 * @param theObject Object, can be any Object
	 * @param theMethodName String, the method name an osc message should be forwarded to
	 * @param theAddrPattern String, the address pattern of the osc message
	 * @example oscP5plug
	 * @usage Application
	 */
	public void plug(final Object theObject, final String theMethodName, final String theAddrPattern) {
		final Class<?> myClass = theObject.getClass();
		final Method[] myMethods = myClass.getDeclaredMethods();
		Class<?>[] myParams = null;
		for (int i = 0; i < myMethods.length; i++) {
			String myTypetag = "";
			try {
				myMethods[i].setAccessible(true);
			} catch (Exception e) {
			}
			if ((myMethods[i].getName()).equals(theMethodName)) {
				myParams = myMethods[i].getParameterTypes();
				OscPlug myOscPlug = new OscPlug();
				for (int j = 0; j < myParams.length; j++) {
					myTypetag += myOscPlug.checkType(myParams[j].getName());
				}

				myOscPlug.plug(theObject, theMethodName, theAddrPattern, myTypetag);
				// _myOscPlugList.add(myOscPlug);
				if (_myOscPlugMap.containsKey(theAddrPattern)) {
					_myOscPlugMap.get(theAddrPattern).add(myOscPlug);
				}
				else {
					ArrayList<OscPlug> myOscPlugList = new ArrayList<OscPlug>();
					myOscPlugList.add(myOscPlug);
					_myOscPlugMap.put(theAddrPattern, myOscPlugList);
				}

			}
		}
	}


	private void handleSystemMessage(final OscMessage theOscMessage) {
		if (theOscMessage.addrPattern().startsWith("/sys/ping")) {
			send("/sys/pong", new Object[0], _myBroadcastAddress);
		}
		else if (theOscMessage.addrPattern().startsWith("/sys/register")) {
			if (theOscMessage.tcpConnection() != null) {
				if (theOscMessage.checkTypetag("s")) {
					theOscMessage.tcpConnection().setName(theOscMessage.get(0).stringValue());
				}
			}
		}
	}


	private void callMethod(final OscMessage theOscMessage) {

		if (theOscMessage.addrPattern().startsWith("/sys/")) {
			handleSystemMessage(theOscMessage);
			// finish this for oscbroadcaster
			// return;
		}

		// forward the message to all OscEventListeners
		for (int i = listeners().size() - 1; i >= 0; i--) {
			((OscEventListener) listeners().get(i)).oscEvent(theOscMessage);
		}

		/* check if the arguments can be forwarded as array */

		if (theOscMessage.isArray) {
			// for (int i = 0; i < _myOscPlugList.size(); i++) {
			// OscPlug myPlug = ((OscPlug) _myOscPlugList.get(i));
			// if (myPlug.isArray && myPlug.checkMethod(theOscMessage, true)) {
			// invoke(myPlug.getObject(), myPlug.getMethod(),
			// theOscMessage.argsAsArray());
			// }
			// }

			if (_myOscPlugMap.containsKey(theOscMessage.addrPattern())) {
				ArrayList<OscPlug> myOscPlugList = _myOscPlugMap.get(theOscMessage.addrPattern());
				for (int i = 0; i < myOscPlugList.size(); i++) {
					OscPlug myPlug = (OscPlug) myOscPlugList.get(i);
					if (myPlug.isArray && myPlug.checkMethod(theOscMessage, true)) {
						// Should we set the following here? The old code did
						// not:
						// theOscMessage.isPlugged = true;
						invoke(myPlug.getObject(), myPlug.getMethod(), theOscMessage.argsAsArray());
					}
				}
			}

		}
		/* check if there is a plug method for the current message */
		// for (int i = 0; i < _myOscPlugList.size(); i++) {
		// OscPlug myPlug = ((OscPlug) _myOscPlugList.get(i));
		// if (!myPlug.isArray && myPlug.checkMethod(theOscMessage, false)) {
		// theOscMessage.isPlugged = true;
		// invoke(myPlug.getObject(), myPlug.getMethod(), theOscMessage
		// .arguments());
		// }
		// }

		if (_myOscPlugMap.containsKey(theOscMessage.addrPattern())) {
			ArrayList<OscPlug> myOscPlugList = _myOscPlugMap.get(theOscMessage.addrPattern());
			for (int i = 0; i < myOscPlugList.size(); i++) {
				OscPlug myPlug = (OscPlug) myOscPlugList.get(i);
				if (!myPlug.isArray && myPlug.checkMethod(theOscMessage, false)) {
					theOscMessage.isPlugged = true;
					invoke(myPlug.getObject(), myPlug.getMethod(), theOscMessage.arguments());
				}
			}
		}

		/* if no plug method was detected, then use the default oscEvent mehtod */
		Logger.printDebug("OscP5.callMethod ", "" + isEventMethod);
		if (isEventMethod) {
			try {
				if (isOscIn) {
					invoke(parent, _myEventMethod, new Object[] { new OscIn(theOscMessage) });
					Logger.printDebug("OscP5.callMethod ", "invoking OscIn " + isEventMethod);
				}
				else {
					invoke(parent, _myEventMethod, new Object[] { theOscMessage });
					Logger.printDebug("OscP5.callMethod ", "invoking OscMessage " + isEventMethod);
				}
			} catch (ClassCastException e) {
				Logger.printError("OscHandler.callMethod", " ClassCastException." + e);
			}
		}
	}


	private void invoke(final Object theObject, final Method theMethod, final Object[] theArgs) {
		try {
			theMethod.invoke(theObject, theArgs);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			Logger.printError("OscP5", "ERROR. an error occured while forwarding an OscMessage\n " + "to a method in your program. please check your code for any \n"
					+ "possible errors that might occur in the method where incoming\n " + "OscMessages are parsed e.g. check for casting errors, possible\n "
					+ "nullpointers, array overflows ... .\n" + "method in charge : " + theMethod.getName() + "  " + e);
		}
	}


	/**
	 * incoming osc messages from an udp socket are parsed, processed and forwarded to the parent.
	 * 
	 * @invisible
	 * @param thePacket DatagramPacket
	 * @param thePort int
	 */
	public void process(final DatagramPacket thePacket, final int thePort) {
		synchronized (this) {
			OscPacket p = OscPacket.parse(thePacket);
			if (p.isValid()) {
				if (p.type() == OscPacket.BUNDLE) {
					for (int i = 0; i < ((OscBundle) p).size(); i++) {
						callMethod(((OscBundle) p).getMessage(i));
					}
				}
				else {
					callMethod((OscMessage) p);
				}
			}
			notifyAll();
		}
	}


	/**
	 * @invisible
	 * @see netP5.TcpPacketListener#process(netP5.TcpPacket, int)
	 */
	public void process(final TcpPacket thePacket, final int thePort) {
		synchronized (this) {
			OscPacket p = OscPacket.parse(thePacket);
			if (p.isValid()) {
				if (p.type() == OscPacket.BUNDLE) {
					for (int i = 0; i < ((OscBundle) p).size(); i++) {
						callMethod(((OscBundle) p).getMessage(i));
					}
				}
				else {
					callMethod((OscMessage) p);
				}
			}
			notifyAll();
		}
	}


	/**
	 * @invisible
	 * @param theTcpClient AbstractTcpClient
	 */
	public void remove(AbstractTcpClient theTcpClient) {
	}


	/**
	 * @invisible
	 * @param theIndex int
	 */
	public void status(int theIndex) {
	}


	/**
	 * returns the current properties of oscP5.
	 * 
	 * @return OscProperties
	 * @related OscProperties
	 * @usage Application
	 */
	public OscProperties properties() {
		return _myOscProperties;
	}


	/**
	 * @invisible
	 * @return boolean
	 */
	public boolean isBroadcast() {
		return isBroadcast;
	}


	/**
	 * @return String
	 * @invisible
	 */
	public String ip() {
		return NetInfo.getHostAddress();
	}


	/**
	 * oscP5 has a logging mechanism which prints out processes, warnings and errors into the
	 * console window. e.g. turn off the error log with setLogStatus(Logger.ERROR, Logger.OFF);
	 * 
	 * @param theIndex int
	 * @param theValue int
	 * @usage Application
	 */
	public static void setLogStatus(final int theIndex, final int theValue) {
		Logger.set(theIndex, theValue);
	}


	/**
	 * @param theValue
	 */
	public static void setLogStatus(final int theValue) {
		for (int i = 0; i < Logger.ALL; i++) {
			Logger.set(i, theValue);
		}
	}


	/**
	 * set timeToLive of a multicast packet.
	 * 
	 * @param theTTL int
	 */
	public void setTimeToLive(int theTTL) {
		_myOscNetManager.setTimeToLive(theTTL);
	}


	/**
	 * @param theHost NetAddress
	 * @invisible
	 */
	public void disconnect(final NetAddress theHost) {
		if (theHost.isvalid() && theHost.name.length() > 1) {
			String myAddrPattern = "/sys/disconnect/" + theHost.name + "/" + theHost.port();
			send(myAddrPattern, new Object[0], theHost);
			isBroadcast = false;
			_myBroadcastAddress = null;
		}
	}


	/**
	 * @param theNetAddress NetAddress
	 * @param theName String
	 * @param theArguments String[]
	 * @invisible
	 */
	public void connect(final NetAddress theNetAddress, final String theName, final String[] theArguments) {
		if (theNetAddress.isvalid()) {
			_myBroadcastAddress = theNetAddress;
			_myBroadcastAddress.name = theName;
			String myAddrPattern = "/sys/connect/" + theName + "/" + _myOscProperties.listeningPort();
			send(myAddrPattern, theArguments, _myBroadcastAddress);
			isBroadcast = true;
		}
	}


	/**
	 * netinfo() returns an instance of a NetInfo Object from which you can get LAN and WAN
	 * information.
	 * 
	 * @return NetInfo
	 */
	public NetInfo netInfo() {
		return _myNetInfo;
	}


	/**
	 * return the instance of the running TCP server if in TCP mode.
	 * 
	 * @return TcpServer
	 */
	public TcpServer tcpServer() {
		return _myOscNetManager.tcpServer();
	}


	/**
	 * return the instance of the running TCP client if in TCP mode.
	 * 
	 * @return TcpClient
	 */
	public TcpClient tcpClient() {
		return _myOscNetManager.tcpClient();
	}


	/**
	 * you can send osc packets in many different ways. see below and use the send method that fits
	 * your needs.
	 * 
	 * 
	 * @param thePacket OscPacket
	 * @param theNetAddress NetAddress
	 * @usage Application
	 */
	public void send(final OscPacket thePacket, final NetAddress theNetAddress) {
		_myOscNetManager.send(thePacket, theNetAddress);
	}


	/**
	 * @param thePacket OscPacket
	 * @usage Application
	 * @example oscP5sendReceive
	 */
	public void send(final OscPacket thePacket) {
		_myOscNetManager.send(thePacket);
	}


	/**
	 * @param thePacket OscPacket
	 * @param theNetAddressList NetAddressList
	 * @usage Application
	 */
	public void send(final OscPacket thePacket, final NetAddressList theNetAddressList) {
		_myOscNetManager.send(thePacket, theNetAddressList);
	}


	/**
	 * @param theAddrPattern String
	 * @param theArguments Object[]
	 * @usage Application
	 */
	public void send(final String theAddrPattern, final Object[] theArguments) {
		_myOscNetManager.send(theAddrPattern, theArguments);
	}


	/**
	 * @param theAddrPattern String
	 * @param theArguments Object[]
	 * @param theNetAddressList NetAddressList
	 * @usage Application
	 */
	public void send(final String theAddrPattern, final Object[] theArguments, final NetAddressList theNetAddressList) {
		_myOscNetManager.send(theAddrPattern, theArguments, theNetAddressList);
	}


	/**
	 * @param theAddrPattern String
	 * @param theArguments Object[]
	 * @param theNetAddress NetAddress
	 * @usage Application
	 */
	public void send(final String theAddrPattern, final Object[] theArguments, final NetAddress theNetAddress) {
		_myOscNetManager.send(theAddrPattern, theArguments, theNetAddress);
	}


	/**
	 * @param theAddrPattern String
	 * @param theArguments Object[]
	 * @param theNetAddress NetAddress
	 * @usage Application
	 */
	public void send(final String theAddrPattern, final Object[] theArguments, final String theAddress, int thePort) {
		_myOscNetManager.send(theAddrPattern, theArguments, theAddress, thePort);
	}


	/**
	 * send to tcp client
	 * 
	 * @param thePacket OscPacket
	 * @param theClient TcpClient
	 */
	public void send(final OscPacket thePacket, final TcpClient theClient) {
		theClient.send(thePacket.getBytes());
	}


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


	/**
	 * the send method offers a lot of possibilities. have a look at the send documentation.
	 * 
	 * @param thePacket OscPacket
	 * @param theIpAddress String
	 * @param thePort int
	 * @usage Application
	 * @deprecated
	 */
	public void send(final OscPacket thePacket, final String theIpAddress, final int thePort) {
		_myOscNetManager.send(thePacket, theIpAddress, thePort);
	}


	/**
	 * stop oscP5 and close open Sockets.
	 */
	public void stop() {
		Logger.printDebug("OscP5.stop", "starting to stop oscP5.");
		_myOscNetManager.stop();
		Logger.printDebug("OscP5.stop", "stopping oscP5.");
	}


	/**
	 * a static method to send an OscMessage straight out of the box without having to instantiate
	 * oscP5.
	 * 
	 * @param theOscMessage OscMessage
	 * @param theNetAddress NetAddress
	 * @example oscP5flush
	 */
	public static void flush(final OscMessage theOscMessage, final NetAddress theNetAddress) {
		flush(theOscMessage.getBytes(), theNetAddress);
	}


	public static void flush(final OscPacket theOscPacket, final NetAddress theNetAddress) {
		flush(theOscPacket.getBytes(), theNetAddress);
	}


	public static void flush(final String theAddrPattern, final Object[] theArguments, final NetAddress theNetAddress) {
		flush((new OscMessage(theAddrPattern, theArguments)).getBytes(), theNetAddress);
	}


	public static void flush(final byte[] theBytes, final NetAddress theNetAddress) {
		DatagramSocket mySocket;
		try {
			mySocket = new DatagramSocket();

			DatagramPacket myPacket = new DatagramPacket(theBytes, theBytes.length, theNetAddress.inetaddress(), theNetAddress.port());
			mySocket.send(myPacket);
		} catch (SocketException e) {
			Logger.printError("OscP5.openSocket", "cant create socket " + e.getMessage());
		} catch (IOException e) {
			Logger.printError("OscP5.openSocket", "cant create multicastSocket " + e.getMessage());
		}
	}


	/*
	 * DEPRECATED methods and constructors.
	 */

	/**
	 * @param theBytes byte[]
	 * @param theAddress String
	 * @param thePort int
	 * @deprecated
	 */
	public static void flush(final byte[] theBytes, final String theAddress, final int thePort) {
		flush(theBytes, new NetAddress(theAddress, thePort));
	}


	/**
	 * @param theOscMessage OscMessage
	 * @param theAddress String
	 * @param thePort int
	 * @deprecated
	 */
	public static void flush(final OscMessage theOscMessage, final String theAddress, final int thePort) {
		flush(theOscMessage.getBytes(), new NetAddress(theAddress, thePort));
	}


	/**
	 * old version of constructor. still in here for backwards compatibility.
	 * 
	 * @deprecated
	 * @invisible
	 */
	public OscP5(final Object theParent, final String theHost, final int theSendToPort, final int theReceiveAtPort, final String theMethodName) {
		welcome();
		parent = theParent;

		registerDispose(parent);

		_myOscProperties = new OscProperties();
		_myOscProperties.setRemoteAddress(theHost, theSendToPort);
		_myOscProperties.setListeningPort(theReceiveAtPort);
		_myOscProperties.setEventMethod(theMethodName);
		_myOscNetManager = new OscNetManager();
		_myOscNetManager.start(_myOscProperties);
		_myOscNetManager.addUdpListener(this);
		isEventMethod = checkEventMethod();
	}


	/**
	 * @deprecated
	 * @param theAddrPattern String
	 * @return OscMessage
	 * @invisible
	 */
	public OscMessage newMsg(String theAddrPattern) {
		return new OscMessage(theAddrPattern);
	}


	/**
	 * @deprecated
	 * @param theAddrPattern String
	 * @return OscMessage
	 * @invisible
	 */

	public OscBundle newBundle() {
		return new OscBundle();
	}


	/**
	 * used by the monome library by jklabs
	 * 
	 * @deprecated
	 * @invisible
	 */
	public void disconnectFromTEMP() {
	}


	/**
	 * @deprecated
	 * @param theParent Object
	 * @param theAddress String
	 * @param thePort int
	 */
	public OscP5(final Object theParent, final String theAddress, final int thePort) {
		this(theParent, theAddress, thePort, OscProperties.MULTICAST);
	}

}