1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.ximtec.igesture.io.tuio;
22
23 import com.illposed.osc.*;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.net.SocketException;
28 import java.util.*;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31
32 import javax.xml.parsers.DocumentBuilder;
33 import javax.xml.parsers.DocumentBuilderFactory;
34 import javax.xml.parsers.ParserConfigurationException;
35
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.w3c.dom.Node;
39 import org.w3c.dom.NodeList;
40 import org.ximtec.igesture.io.tuio.handler.AbstractTuioHandler;
41 import org.xml.sax.SAXException;
42
43
44
45
46
47
48
49 public class TuioConnection implements OSCListener {
50
51 private static final Logger LOGGER = Logger.getLogger(TuioConnection.class.getName());
52
53
54 private int port = TuioConstants.DEFAULT_PORT;
55
56 private OSCPortIn oscPort;
57
58 private boolean connected = false;
59
60 private TuioTime currentTime;
61
62
63 private Map<String, AbstractTuioHandler> profileToHandler = new HashMap<String, AbstractTuioHandler>();
64
65
66
67
68 public TuioConnection()
69 {
70 this(TuioConstants.DEFAULT_PORT);
71 }
72
73
74
75
76
77
78 public TuioConnection(int port) {
79 this.port = port;
80 }
81
82
83
84
85
86 public void connect() {
87
88
89 TuioTime.initSession();
90 currentTime = new TuioTime();
91 currentTime.reset();
92
93 try {
94
95 oscPort = new OSCPortIn(port);
96
97
98 initProfiles();
99
100
101 Set<String> keys = profileToHandler.keySet();
102 for(String key : keys)
103 {
104 oscPort.addListener(key, this);
105 }
106
107
108 oscPort.startListening();
109 connected = true;
110 LOGGER.log(Level.INFO,"Connected to port "+port);
111 } catch (SocketException e) {
112 LOGGER.log(Level.SEVERE,"Failed to connect to port "+port,e);
113 connected = false;
114 } catch (Exception e) {
115 LOGGER.log(Level.SEVERE,"Could not parse "+TuioConstants.XML_TUIO_PROFILES,e);
116 disconnect();
117 }
118 }
119
120
121
122
123
124
125
126 private void initProfiles() throws ParserConfigurationException, SAXException, IOException
127 {
128
129 File file = new File(System.getProperty("user.dir")+System.getProperty("file.separator")+TuioConstants.XML_TUIO_PROFILES);
130
131
132 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
133 DocumentBuilder db;
134 db = dbf.newDocumentBuilder();
135
136 Document doc = db.parse(file);
137
138 doc.getDocumentElement().normalize();
139
140 NodeList nodeList = doc.getElementsByTagName("association");
141
142
143 for (int i = 0; i < nodeList.getLength(); i++)
144 {
145 Node node = nodeList.item(i);
146
147 if (node.getNodeType() == Node.ELEMENT_NODE)
148 {
149 NodeList profiles = ((Element)node).getElementsByTagName("profile");
150 NodeList p = ((Element) profiles.item(0)).getChildNodes();
151
152
153 NodeList handlers = ((Element)node).getElementsByTagName("handler");
154 NodeList h = ((Element) handlers.item(0)).getChildNodes();
155
156
157 try {
158
159 Class c = Class.forName(((Node) h.item(0)).getNodeValue());
160 profileToHandler.put(((Node) p.item(0)).getNodeValue(), (AbstractTuioHandler) c.newInstance());
161 }
162 catch (Exception e) {
163 LOGGER.log(Level.SEVERE,"Could not find class: "+((Node) h.item(0)).getNodeValue(),e);
164 }
165 }
166 }
167 }
168
169
170
171
172 public void disconnect() {
173 LOGGER.log(Level.INFO,"Disconnecting from port "+port);
174 oscPort.stopListening();
175 try { Thread.sleep(100); }
176 catch (Exception e) {};
177 oscPort.close();
178 connected = false;
179 }
180
181
182
183
184
185 public boolean isConnected() { return connected; }
186
187
188
189
190
191
192
193 public void addTuioListener(TuioListener listener, Set<String> modifiers)
194 {
195 for(String s : modifiers)
196 {
197 if(profileToHandler.containsKey(s))
198 {
199 profileToHandler.get(s).addTuioListener(listener);
200 }
201 else
202 {
203 LOGGER.log(Level.WARNING,this.getClass().getName()+": Incorrect modifiers");
204 }
205 }
206 }
207
208
209
210
211
212
213
214 public void removeTuioListener(TuioListener listener, Set<String> modifiers)
215 {
216 for(String s : modifiers)
217 {
218 if(profileToHandler.containsKey(s))
219 {
220 profileToHandler.get(s).removeTuioListener(listener);
221 }
222 else
223 {
224 LOGGER.log(Level.WARNING,this.getClass().getName()+": Incorrect modifiers");
225 }
226 }
227 }
228
229
230
231
232
233 public void removeAllTuioListener(Set<String> modifiers)
234 {
235 for(String s : modifiers)
236 {
237 if(profileToHandler.containsKey(s))
238 {
239 profileToHandler.get(s).removeAllTuioListeners();
240 }
241 else
242 {
243 LOGGER.log(Level.WARNING,this.getClass().getName()+": Incorrect modifiers");
244 }
245 }
246 }
247
248
249
250
251
252
253
254
255 public void acceptMessage(Date date, OSCMessage message) {
256
257 String address = message.getAddress();
258
259
260 if(profileToHandler.containsKey(address))
261 profileToHandler.get(address).acceptMessage(message, currentTime);
262 }
263 }
264