diff -r 000000000000 -r 6fefd4afe506 middleware/src/Communication/TuioServer.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/middleware/src/Communication/TuioServer.cs Fri Mar 09 14:52:11 2012 +0100 @@ -0,0 +1,186 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Net; +using System.Text; + +using Bespoke.Common.Osc; + +namespace Tuio +{ + /// + /// Simple, still uncomplete implementation of a TUIO server in C#. + /// + /// Current shortcomings: + /// Object support missing. + /// Does not implement frame times. + /// Only supports external TUIO cursors. + /// Allways commits all cursors. + /// + /// (c) 2010 by Dominik Schmidt (schmidtd@comp.lancs.ac.uk) + /// + public class TuioServer + { + #region constants + + private const string _cursorAddressPattern = "/tuio/2Dcur"; + + private const string _objectAddressPattern = "/tuio/2Dobj"; + + #endregion + + #region fields + + private IPEndPoint _ipEndPoint; + + private Dictionary _cursors; + + private int _currentFrame; + + #endregion + + #region constructors + + /// + /// Creates a new server with and endpoint at localhost, port 3333. + /// + public TuioServer() : this("127.0.0.1", 3333) { } + + /// + /// Creates a new server. + /// + /// Endpoint host + /// Endpoint port + public TuioServer(string host, int port) + { + _cursors = new Dictionary(); + _ipEndPoint = new IPEndPoint(IPAddress.Parse(host), port); + _currentFrame = 0; + } + + #endregion + + #region frame related methods + + /// + /// Initialized a new frame and increases the frame counter. + /// + public void InitFrame() + { + _currentFrame++; + } + + /// + /// Commits the current frame. + /// + public void CommitFrame() + { + GetFrameBundle().Send(_ipEndPoint); + } + + #endregion + + #region cursor related methods + + /// + /// Adds a TUIO cursor. A new id, not used before, must be provided. + /// + /// New id + /// Location + public void AddTuioCursor(int id, PointF location) + { + lock(_cursors) + if(!_cursors.ContainsKey(id)) + _cursors.Add(id, new TuioCursor(id, location)); + } + + /// + /// Updates a TUIO cursor. An id of an existing cursor must be provided. + /// + /// Id + /// Location + public void UpdateTuioCursor(int id, PointF location) + { + TuioCursor cursor; + if(_cursors.TryGetValue(id, out cursor)) + cursor.Location = location; + } + + /// + /// Deletes a TUIO cursor. An id of an existing cursor must be provided. + /// + /// Id + public void DeleteTuioCursor(int id) + { + lock (_cursors) + _cursors.Remove(id); + } + + #endregion + + #region osc message assembly + + private OscBundle GetFrameBundle() + { + OscBundle bundle = new OscBundle(_ipEndPoint); + + bundle.Append(GetAliveMessage()); + foreach (OscMessage msg in GetCursorMessages()) + bundle.Append(msg); + bundle.Append(GetSequenceMessage()); + + return bundle; + } + + private OscMessage GetAliveMessage() + { + OscMessage msg = new OscMessage(_ipEndPoint, _cursorAddressPattern); + + msg.Append("alive"); + lock (_cursors) + foreach (TuioCursor cursor in _cursors.Values) + msg.Append((Int32)cursor.Id); + + return msg; + } + + private OscMessage GetSequenceMessage() + { + OscMessage msg = new OscMessage(_ipEndPoint, _cursorAddressPattern); + + msg.Append("fseq"); + msg.Append((Int32)_currentFrame); + + return msg; + } + + private OscMessage GetCursorMessage(TuioCursor cursor) + { + OscMessage msg = new OscMessage(_ipEndPoint, _cursorAddressPattern); + + msg.Append("set"); + msg.Append((Int32)cursor.Id); + msg.Append(cursor.Location.X); + msg.Append(cursor.Location.Y); + msg.Append(cursor.Speed.X); + msg.Append(cursor.Speed.Y); + msg.Append(cursor.MotionAcceleration); + + return msg; + } + + private IEnumerable GetCursorMessages() + { + List msgs = new List(); + + lock (_cursors) + foreach (TuioCursor cursor in _cursors.Values) + msgs.Add(GetCursorMessage(cursor)); + + return msgs.AsEnumerable(); + } + + #endregion + } +}