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