root/trunk/libffado/src/libosc/OscServer.cpp

Revision 560, 5.0 kB (checked in by ppalmers, 17 years ago)

- Sort the FFADODevice vector on GUID before assigning device id's

This results in the same device id for identical device setups,
independent of the way they are connected or the node numbers they
have been assigned.

- Sanitized debug message reporting a bit
- Cosmetic changes

Line 
1 /*
2  * Copyright (C) 2005-2007 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License version 2.1, as published by the Free Software Foundation;
12  *
13  * This library 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 GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21  * MA 02110-1301 USA
22  */
23
24 #include <lo/lo.h>
25
26 #include "OscServer.h"
27 #include "OscMessage.h"
28 #include "OscResponse.h"
29 #include "OscNode.h"
30
31 #include <assert.h>
32
33 namespace OSC {
34
35 IMPL_DEBUG_MODULE( OscServer, OscServer, DEBUG_LEVEL_NORMAL );
36
37 OscServer::OscServer(string port)
38     : m_port(port)
39     , m_server(NULL)
40     , m_rootNode(new OscNode("/root"))
41 {
42
43 }
44
45 OscServer::~OscServer() {
46     lo_server_thread_free(m_server);
47     if (m_rootNode) delete m_rootNode;
48 }
49
50 void
51 OscServer::setVerboseLevel(int l) {
52     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting verbose level to %d...\n", l );
53     setDebugLevel(l);
54     if (m_rootNode) m_rootNode->setVerboseLevel(l);
55 }
56
57 bool
58 OscServer::start()
59 {
60     if (!m_server) {
61         debugWarning("no server thread\n");
62         return false;
63     }
64     lo_server_thread_start(m_server);
65     return true;
66 }
67
68 bool
69 OscServer::stop()
70 {
71     if (!m_server) {
72         debugWarning("no server thread\n");
73         return false;
74     }
75     lo_server_thread_stop(m_server);
76     return true;
77 }
78
79 bool
80 OscServer::init()
81 {
82     if (m_rootNode == NULL) { // should be done at creation
83         debugError("Could not initialize root node\n");
84         return false;
85     }
86
87     m_server = lo_server_thread_new(m_port.c_str(), error_cb);
88
89     if (m_server == NULL) {
90         debugWarning("Could not start OSC server on port %s, trying other port...\n", m_port.c_str());
91         m_server = lo_server_thread_new(NULL, error_cb);
92         if (!m_server) {
93             debugError("Could not start OSC server.\n");
94             return false;
95         }
96         debugWarning("Started OSC server at %s\n",
97                     lo_server_get_url(lo_server_thread_get_server(m_server)));
98     } else {
99         debugOutput(DEBUG_LEVEL_VERBOSE,
100                     "Started OSC server at %s\n",
101                     lo_server_get_url(lo_server_thread_get_server(m_server)));
102     }
103
104     // For debugging, print all incoming OSC messages
105     lo_server_thread_add_method(m_server, NULL, NULL, generic_cb, this);
106
107     return true;
108 }
109
110 bool
111 OscServer::registerAtRootNode(OscNode *n) {
112     return m_rootNode->addChildOscNode(n);
113 }
114
115 bool
116 OscServer::unregisterAtRootNode(OscNode * n) {
117     return m_rootNode->removeChildOscNode(n);
118 }
119
120 // callbacks
121
122 void
123 OscServer::error_cb(int num, const char* msg, const char* path)
124 {
125     debugError("liblo server error %d in path %s, message: %s\n", num, path, msg);
126 }
127
128 // Display incoming OSC messages (for debugging purposes)
129 int
130 OscServer::generic_cb(const char* path, const char* types, lo_arg** argv, int argc, lo_message msg, void* user_data)
131 {
132     OscServer *server=reinterpret_cast<OscServer *>(user_data);
133     assert(server);
134
135     debugOutput(DEBUG_LEVEL_VERBOSE, "Message on: %s\n", path);
136
137     if(!server->m_rootNode) {
138         debugError("No root node registered");
139         return 1;
140     }
141
142     // construct the message
143     OscMessage m = OscMessage(path, types, argv, argc);
144
145 #ifdef DEBUG
146     if (getDebugLevel()>=DEBUG_LEVEL_VERY_VERBOSE) {
147         for (int i=0; i < argc; ++i) {
148             printf("arg %d '%c' ", i, types[i]);
149             lo_arg_pp(lo_type(types[i]), argv[i]);
150             printf("\n");
151         }
152         m.print();
153     }
154 #endif
155
156     // process it
157     OscResponse r=server->m_rootNode->processOscMessage(string("/root")+string(path), &m);
158     if(r.isHandled()) {
159         if(r.hasMessage()) {
160             // send response
161             lo_address addr = lo_message_get_source(msg);
162
163             lo_message lo_msg;
164             lo_msg=r.getMessage().makeLoMessage();
165
166             debugOutput(DEBUG_LEVEL_VERBOSE, " Sending response to %s\n",lo_address_get_url(addr));
167
168             #ifdef DEBUG
169                 if(getDebugLevel()>=DEBUG_LEVEL_VERY_VERBOSE) r.getMessage().print();
170             #endif
171
172             if (lo_send_message(addr, "/response", lo_msg) < 0) {
173                 debugError("Failed to send response\n");
174             }
175             lo_message_free(lo_msg);
176         } else {
177             debugOutput(DEBUG_LEVEL_VERBOSE, " No response...\n");
178         }
179         return 0;
180     } else {
181         if (r.isError()) {
182             debugOutput(DEBUG_LEVEL_VERBOSE, " Error in message...\n");
183         } else {
184             debugOutput(DEBUG_LEVEL_VERBOSE, " Not handled...\n");
185         }
186         m.print();
187         return 1;  // not handled
188     }
189 }
190
191
192 } // end of namespace OSC
Note: See TracBrowser for help on using the browser.