libAlgAudio  v1.99-440-g08538e5-dirty
The development library for AlgAudio framework.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OSC.hpp
Go to the documentation of this file.
1 #ifndef OSC_HPP
2 #define OSC_HPP
3 /*
4 This file is part of AlgAudio.
5 
6 AlgAudio, Copyright (C) 2015 CeTA - Audiovisual Technology Center
7 
8 AlgAudio is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
12 
13 AlgAudio is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with AlgAudio. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <string>
22 #include <map>
23 #include <list>
24 #include <thread>
25 #include <mutex>
26 #include <functional>
27 #ifndef __unix__
28  #include <winsock2.h>
29  // Othrewise lo is confused when building under MSYS
30  #define WIN32
31 #endif
32 #include <lo/lo_cpp.h>
33 #include "Utilities.hpp"
34 
35 namespace AlgAudio{
36 
37 namespace Exceptions{
38 struct OSCException : public Exception{
39  OSCException(std::string t) : Exception(t) {}
40 };
41 } // namespace Exceptions
42 
43 template<typename T>
44 inline T UnpackLOMessage(const lo::Message& msg, unsigned int n);
45 template<>
46 inline int UnpackLOMessage<int>(const lo::Message& msg, unsigned int n){ return msg.argv()[n]->i32; }
47 template<>
48 inline float UnpackLOMessage<float>(const lo::Message& msg, unsigned int n){ return msg.argv()[n]->f; }
49 template<>
50 inline double UnpackLOMessage<double>(const lo::Message& msg, unsigned int n){ return msg.argv()[n]->d; }
51 template<>
52 inline std::string UnpackLOMessage<std::string>(const lo::Message& msg, unsigned int n){ return std::string(&msg.argv()[n]->s); }
53 
60 class OSC{
61 public:
62  OSC(std::string address, std::string port);
63  ~OSC();
64  void Send(std::string path);
65  void Send(std::string path, lo::Message);
66  void Send(std::string path, std::function<void(lo::Message)> reply_action, lo::Message);
67 
72  void TriggerReplies();
73 
78  void AddMethodHandler(std::string path, std::function<void(lo::Message)>);
79 
80 private:
82  std::unique_ptr<lo::ServerThread> server;
83  lo::Address addr;
84 
86  static int msg_id;
87 
88  std::recursive_mutex osc_mutex;
90  std::map< int, std::function< void(lo::Message) > > waiting_for_reply;
93  std::list< std::function<void()> > replies_to_call;
94 };
95 
96 } // namespace AlgAudio
97 
98 #endif // OSC_HPP
void TriggerReplies()
float UnpackLOMessage< float >(const lo::Message &msg, unsigned int n)
Definition: OSC.hpp:48
OSC(std::string address, std::string port)
Definition: OSC.hpp:60
int UnpackLOMessage< int >(const lo::Message &msg, unsigned int n)
Definition: OSC.hpp:46
void AddMethodHandler(std::string path, std::function< void(lo::Message)>)
Definition: Alertable.hpp:26
T UnpackLOMessage(const lo::Message &msg, unsigned int n)
void Send(std::string path)
OSCException(std::string t)
Definition: OSC.hpp:39
double UnpackLOMessage< double >(const lo::Message &msg, unsigned int n)
Definition: OSC.hpp:50
Definition: Exception.hpp:29