|
1 using System; |
|
2 using System.Collections.Generic; |
|
3 using System.Drawing; |
|
4 using System.Linq; |
|
5 using System.Net; |
|
6 using System.Text; |
|
7 |
|
8 using Bespoke.Common.Osc; |
|
9 |
|
10 namespace Tuio |
|
11 { |
|
12 /// <summary> |
|
13 /// Simple, still uncomplete implementation of a TUIO server in C#. |
|
14 /// |
|
15 /// Current shortcomings: |
|
16 /// Object support missing. |
|
17 /// Does not implement frame times. |
|
18 /// Only supports external TUIO cursors. |
|
19 /// Allways commits all cursors. |
|
20 /// |
|
21 /// (c) 2010 by Dominik Schmidt (schmidtd@comp.lancs.ac.uk) |
|
22 /// </summary> |
|
23 public class TuioServer |
|
24 { |
|
25 #region constants |
|
26 |
|
27 private const string _cursorAddressPattern = "/tuio/2Dcur"; |
|
28 |
|
29 private const string _objectAddressPattern = "/tuio/2Dobj"; |
|
30 |
|
31 #endregion |
|
32 |
|
33 #region fields |
|
34 |
|
35 private IPEndPoint _ipEndPoint; |
|
36 |
|
37 private Dictionary<int, TuioCursor> _cursors; |
|
38 |
|
39 private int _currentFrame; |
|
40 |
|
41 #endregion |
|
42 |
|
43 #region constructors |
|
44 |
|
45 /// <summary> |
|
46 /// Creates a new server with and endpoint at localhost, port 3333. |
|
47 /// </summary> |
|
48 public TuioServer() : this("127.0.0.1", 3333) { } |
|
49 |
|
50 /// <summary> |
|
51 /// Creates a new server. |
|
52 /// </summary> |
|
53 /// <param name="host">Endpoint host</param> |
|
54 /// <param name="port">Endpoint port</param> |
|
55 public TuioServer(string host, int port) |
|
56 { |
|
57 _cursors = new Dictionary<int, TuioCursor>(); |
|
58 _ipEndPoint = new IPEndPoint(IPAddress.Parse(host), port); |
|
59 _currentFrame = 0; |
|
60 } |
|
61 |
|
62 #endregion |
|
63 |
|
64 #region frame related methods |
|
65 |
|
66 /// <summary> |
|
67 /// Initialized a new frame and increases the frame counter. |
|
68 /// </summary> |
|
69 public void InitFrame() |
|
70 { |
|
71 _currentFrame++; |
|
72 } |
|
73 |
|
74 /// <summary> |
|
75 /// Commits the current frame. |
|
76 /// </summary> |
|
77 public void CommitFrame() |
|
78 { |
|
79 GetFrameBundle().Send(_ipEndPoint); |
|
80 } |
|
81 |
|
82 #endregion |
|
83 |
|
84 #region cursor related methods |
|
85 |
|
86 /// <summary> |
|
87 /// Adds a TUIO cursor. A new id, not used before, must be provided. |
|
88 /// </summary> |
|
89 /// <param name="id">New id</param> |
|
90 /// <param name="location">Location</param> |
|
91 public void AddTuioCursor(int id, PointF location) |
|
92 { |
|
93 lock(_cursors) |
|
94 if(!_cursors.ContainsKey(id)) |
|
95 _cursors.Add(id, new TuioCursor(id, location)); |
|
96 } |
|
97 |
|
98 /// <summary> |
|
99 /// Updates a TUIO cursor. An id of an existing cursor must be provided. |
|
100 /// </summary> |
|
101 /// <param name="id">Id</param> |
|
102 /// <param name="location">Location</param> |
|
103 public void UpdateTuioCursor(int id, PointF location) |
|
104 { |
|
105 TuioCursor cursor; |
|
106 if(_cursors.TryGetValue(id, out cursor)) |
|
107 cursor.Location = location; |
|
108 } |
|
109 |
|
110 /// <summary> |
|
111 /// Deletes a TUIO cursor. An id of an existing cursor must be provided. |
|
112 /// </summary> |
|
113 /// <param name="id">Id</param> |
|
114 public void DeleteTuioCursor(int id) |
|
115 { |
|
116 lock (_cursors) |
|
117 _cursors.Remove(id); |
|
118 } |
|
119 |
|
120 #endregion |
|
121 |
|
122 #region osc message assembly |
|
123 |
|
124 private OscBundle GetFrameBundle() |
|
125 { |
|
126 OscBundle bundle = new OscBundle(_ipEndPoint); |
|
127 |
|
128 bundle.Append(GetAliveMessage()); |
|
129 foreach (OscMessage msg in GetCursorMessages()) |
|
130 bundle.Append(msg); |
|
131 bundle.Append(GetSequenceMessage()); |
|
132 |
|
133 return bundle; |
|
134 } |
|
135 |
|
136 private OscMessage GetAliveMessage() |
|
137 { |
|
138 OscMessage msg = new OscMessage(_ipEndPoint, _cursorAddressPattern); |
|
139 |
|
140 msg.Append("alive"); |
|
141 lock (_cursors) |
|
142 foreach (TuioCursor cursor in _cursors.Values) |
|
143 msg.Append((Int32)cursor.Id); |
|
144 |
|
145 return msg; |
|
146 } |
|
147 |
|
148 private OscMessage GetSequenceMessage() |
|
149 { |
|
150 OscMessage msg = new OscMessage(_ipEndPoint, _cursorAddressPattern); |
|
151 |
|
152 msg.Append("fseq"); |
|
153 msg.Append((Int32)_currentFrame); |
|
154 |
|
155 return msg; |
|
156 } |
|
157 |
|
158 private OscMessage GetCursorMessage(TuioCursor cursor) |
|
159 { |
|
160 OscMessage msg = new OscMessage(_ipEndPoint, _cursorAddressPattern); |
|
161 |
|
162 msg.Append("set"); |
|
163 msg.Append((Int32)cursor.Id); |
|
164 msg.Append(cursor.Location.X); |
|
165 msg.Append(cursor.Location.Y); |
|
166 msg.Append(cursor.Speed.X); |
|
167 msg.Append(cursor.Speed.Y); |
|
168 msg.Append(cursor.MotionAcceleration); |
|
169 |
|
170 return msg; |
|
171 } |
|
172 |
|
173 private IEnumerable<OscMessage> GetCursorMessages() |
|
174 { |
|
175 List<OscMessage> msgs = new List<OscMessage>(); |
|
176 |
|
177 lock (_cursors) |
|
178 foreach (TuioCursor cursor in _cursors.Values) |
|
179 msgs.Add(GetCursorMessage(cursor)); |
|
180 |
|
181 return msgs.AsEnumerable(); |
|
182 } |
|
183 |
|
184 #endregion |
|
185 } |
|
186 } |