1 /** |
1 /** |
2 * @author cramakrishnan |
2 * @author cramakrishnan |
3 * |
3 * |
4 * Copyright (C) 2004, C. Ramakrishnan / Illposed Software |
4 * Copyright (C) 2004, C. Ramakrishnan / Illposed Software |
5 * All rights reserved. |
5 * All rights reserved. |
6 * |
6 * |
7 * See license.txt (or license.rtf) for license information. |
7 * See license.txt (or license.rtf) for license information. |
8 * |
8 * |
9 * |
9 * |
10 * OSCPortIn is the class that listens for OSC messages. |
10 * OSCPortIn is the class that listens for OSC messages. |
11 * |
11 * |
12 * To receive OSC, you need to construct the OSCPort with a |
12 * To receive OSC, you need to construct the OSCPort with a |
13 * |
13 * |
14 * An example based on com.illposed.osc.test.OSCPortTest::testReceiving() : |
14 * An example based on com.illposed.osc.test.OSCPortTest::testReceiving() : |
15 |
15 |
16 receiver = new OSCPort(OSCPort.defaultSCOSCPort()); |
16 receiver = new OSCPort(OSCPort.defaultSCOSCPort()); |
17 OSCListener listener = new OSCListener() { |
17 OSCListener listener = new OSCListener() { |
18 public void acceptMessage(java.util.Date time, OSCMessage message) { |
18 public void acceptMessage(java.util.Date time, OSCMessage message) { |
19 System.out.println("Message received!"); |
19 System.out.println("Message received!"); |
20 } |
20 } |
21 }; |
21 }; |
22 receiver.addListener("/message/receiving", listener); |
22 receiver.addListener("/message/receiving", listener); |
23 receiver.startListening(); |
23 receiver.startListening(); |
24 |
24 |
25 * Then, using a program such as SuperCollider or sendOSC, send a message |
25 * Then, using a program such as SuperCollider or sendOSC, send a message |
26 * to this computer, port 57110 (defaultSCOSCPort), with the address /message/receiving |
26 * to this computer, port 57110 (defaultSCOSCPort), with the address /message/receiving |
27 */ |
27 */ |
28 |
28 |
29 package com.illposed.osc; |
29 package com.illposed.osc; |
33 import com.illposed.osc.utility.OSCByteArrayToJavaConverter; |
33 import com.illposed.osc.utility.OSCByteArrayToJavaConverter; |
34 import com.illposed.osc.utility.OSCPacketDispatcher; |
34 import com.illposed.osc.utility.OSCPacketDispatcher; |
35 |
35 |
36 public class OSCPortIn extends OSCPort implements Runnable { |
36 public class OSCPortIn extends OSCPort implements Runnable { |
37 |
37 |
38 // state for listening |
38 // state for listening |
39 protected boolean isListening; |
39 protected boolean isListening; |
40 protected OSCByteArrayToJavaConverter converter = new OSCByteArrayToJavaConverter(); |
40 protected OSCByteArrayToJavaConverter converter = new OSCByteArrayToJavaConverter(); |
41 protected OSCPacketDispatcher dispatcher = new OSCPacketDispatcher(); |
41 protected OSCPacketDispatcher dispatcher = new OSCPacketDispatcher(); |
42 |
42 |
43 /** |
43 /** |
44 * Create an OSCPort that listens on port |
44 * Create an OSCPort that listens on port |
45 * @param port |
45 * @param port |
46 * @throws SocketException |
46 * @throws SocketException |
47 */ |
47 */ |
48 public OSCPortIn(int port) throws SocketException { |
48 public OSCPortIn(int port) throws SocketException { |
49 socket = new DatagramSocket(port); |
49 socket = new DatagramSocket(port); |
50 this.port = port; |
50 this.port = port; |
51 } |
51 } |
52 |
52 |
53 /** |
53 /** |
54 * @see java.lang.Runnable#run() |
54 * @see java.lang.Runnable#run() |
55 */ |
55 */ |
56 public void run() { |
56 public void run() { |
57 //maximum UDP packet size |
57 //maximum UDP packet size |
58 byte[] buffer = new byte[65536]; |
58 byte[] buffer = new byte[65536]; |
59 DatagramPacket packet = new DatagramPacket(buffer, 65536); |
59 DatagramPacket packet = new DatagramPacket(buffer, 65536); |
60 while (isListening) { |
60 while (isListening) { |
61 try { |
61 try { |
62 packet.setLength(65536); |
62 packet.setLength(65536); |
63 socket.receive(packet); |
63 socket.receive(packet); |
64 OSCPacket oscPacket = converter.convert(buffer, packet.getLength()); |
64 OSCPacket oscPacket = converter.convert(buffer, packet.getLength()); |
65 dispatcher.dispatchPacket(oscPacket); |
65 dispatcher.dispatchPacket(oscPacket); |
66 } catch (java.net.SocketException e) { |
66 } catch (java.net.SocketException e) { |
67 if (isListening) e.printStackTrace(); |
67 if (isListening) e.printStackTrace(); |
68 } catch (IOException e) { |
68 } catch (IOException e) { |
69 if (isListening) e.printStackTrace(); |
69 if (isListening) e.printStackTrace(); |
70 } |
70 } |
71 } |
71 } |
72 } |
72 } |
73 |
73 |
74 /** |
74 /** |
75 * Start listening for incoming OSCPackets |
75 * Start listening for incoming OSCPackets |
76 */ |
76 */ |
77 public void startListening() { |
77 public void startListening() { |
78 isListening = true; |
78 isListening = true; |
79 Thread thread = new Thread(this); |
79 Thread thread = new Thread(this); |
80 thread.start(); |
80 thread.start(); |
81 } |
81 } |
82 |
82 |
83 /** |
83 /** |
84 * Stop listening for incoming OSCPackets |
84 * Stop listening for incoming OSCPackets |
85 */ |
85 */ |
86 public void stopListening() { |
86 public void stopListening() { |
87 isListening = false; |
87 isListening = false; |
88 } |
88 } |
89 |
89 |
90 /** |
90 /** |
91 * Am I listening for packets? |
91 * Am I listening for packets? |
92 */ |
92 */ |
93 public boolean isListening() { |
93 public boolean isListening() { |
94 return isListening; |
94 return isListening; |
95 } |
95 } |
96 |
96 |
97 /** |
97 /** |
98 * Register the listener for incoming OSCPackets addressed to an Address |
98 * Register the listener for incoming OSCPackets addressed to an Address |
99 * @param anAddress the address to listen for |
99 * @param anAddress the address to listen for |
100 * @param listener the object to invoke when a message comes in |
100 * @param listener the object to invoke when a message comes in |
101 */ |
101 */ |
102 public void addListener(String anAddress, OSCListener listener) { |
102 public void addListener(String anAddress, OSCListener listener) { |
103 dispatcher.addListener(anAddress, listener); |
103 dispatcher.addListener(anAddress, listener); |
104 } |
104 } |
105 |
105 |
106 /** |
106 /** |
107 * Close the socket and free-up resources. It's recommended that clients call |
107 * Close the socket and free-up resources. It's recommended that clients call |
108 * this when they are done with the port. |
108 * this when they are done with the port. |
109 */ |
109 */ |
110 public void close() { |
110 public void close() { |
111 socket.close(); |
111 socket.close(); |
112 } |
112 } |
113 |
113 |
114 } |
114 } |