forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOSCPortIn.java
More file actions
128 lines (114 loc) · 3.3 KB
/
Copy pathOSCPortIn.java
File metadata and controls
128 lines (114 loc) · 3.3 KB
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
/*
* Copyright (C) 2004-2006, C. Ramakrishnan / Illposed Software.
* All rights reserved.
*
* This code is licensed under the BSD 3-Clause license.
* See file LICENSE (or LICENSE.html) for more information.
*/
package com.illposed.osc;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.io.IOException;
import java.net.SocketException;
import com.illposed.osc.utility.OSCByteArrayToJavaConverter;
import com.illposed.osc.utility.OSCPacketDispatcher;
/**
* OSCPortIn is the class that listens for OSC messages.
*
* An example based on
* {@link com.illposed.osc.OSCPortTest#testReceiving()}:
* <pre>
receiver = new OSCPortIn(OSCPort.DEFAULT_SC_OSC_PORT());
OSCListener listener = new OSCListener() {
public void acceptMessage(java.util.Date time, OSCMessage message) {
System.out.println("Message received!");
}
};
receiver.addListener("/message/receiving", listener);
receiver.startListening();
* </pre>
*
* Then, using a program such as SuperCollider or sendOSC, send a message
* to this computer, port {@link #DEFAULT_SC_OSC_PORT},
* with the address "/message/receiving".
*
* @author Chandrasekhar Ramakrishnan
*/
public class OSCPortIn extends OSCPort implements Runnable {
// state for listening
private boolean listening;
private OSCByteArrayToJavaConverter converter
= new OSCByteArrayToJavaConverter();
private OSCPacketDispatcher dispatcher = new OSCPacketDispatcher();
/**
* Create an OSCPort that listens on the specified port.
* @param port UDP port to listen on.
* @throws SocketException
*/
public OSCPortIn(int port) throws SocketException {
super(new DatagramSocket(port), port);
}
/**
* Buffers were 1500 bytes in size, but were
* increased to 1536, as this is a common MTU.
*/
private static final int BUFFER_SIZE = 1536;
/**
* Run the loop that listens for OSC on a socket until
* {@link #isListening()} becomes false.
* @see java.lang.Runnable#run()
*/
public void run() {
byte[] buffer = new byte[BUFFER_SIZE];
DatagramPacket packet = new DatagramPacket(buffer, BUFFER_SIZE);
DatagramSocket socket = getSocket();
while (listening) {
try {
try {
socket.receive(packet);
} catch (SocketException ex) {
if (listening) {
throw ex;
} else {
// if we closed the socket while receiving data,
// the exception is expected/normal, so we hide it
continue;
}
}
OSCPacket oscPacket = converter.convert(buffer,
packet.getLength());
dispatcher.dispatchPacket(oscPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Start listening for incoming OSCPackets
*/
public void startListening() {
listening = true;
Thread thread = new Thread(this);
thread.start();
}
/**
* Stop listening for incoming OSCPackets
*/
public void stopListening() {
listening = false;
}
/**
* Am I listening for packets?
*/
public boolean isListening() {
return listening;
}
/**
* Register the listener for incoming OSCPackets addressed to an Address
* @param anAddress the address to listen for. The address can be specified as a regex, e.g., "/m.*e/receiving"
* @param listener the object to invoke when a message comes in
*/
public void addListener(String anAddress, OSCListener listener) {
dispatcher.addListener(anAddress, listener);
}
}