OOP initial commit with new tracked files.
[Faustine.git] / interpretor / faust-0.9.47mr3 / architecture / osclib / faust / src / osc / OSCSetup.cpp
1 /*
2
3 Copyright (C) 2011 Grame
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
20 research@grame.fr
21
22 */
23
24 #include <iostream>
25 #include <stdexcept>
26
27 #include "OSCSetup.h"
28 #include "TThreads.h"
29 #include "OSCListener.h"
30
31 namespace oscfaust
32 {
33
34 //--------------------------------------------------------------------------
35 /*!
36 \brief a specific thread to listen incoming osc packets
37 */
38 class OscThread : public TThreads
39 {
40 public:
41 SOSCListener fListener;
42
43 OscThread(MessageProcessor* mp, int udpport)
44 { fListener = OSCListener::create (mp, udpport); }
45 virtual ~OscThread() { stop(); }
46
47 /// \brief starts the osc listener
48 void run () { fListener->run(); }
49 void stop () { fListener->stop(); quit(); }
50 SOSCListener& listener() { return fListener; }
51 };
52
53 //--------------------------------------------------------------------------
54 OSCSetup::~OSCSetup() { stop(); }
55 bool OSCSetup::running() const { return fOSCThread ? fOSCThread->isRunning() : false; }
56
57 //--------------------------------------------------------------------------
58 bool OSCSetup::start(MessageProcessor* mp, int& inPort, int outPort, int errPort, const char* address )
59 {
60 int port = inPort;
61 bool done = false;
62 do {
63 try {
64 OSCStream::start();
65 oscout.setPort(outPort);
66 oscerr.setPort (errPort);
67 oscout.setAddress(address);
68 oscerr.setAddress(address);
69 fOSCThread = new OscThread (mp, port);
70 fOSCThread->start();
71 done = true;
72 }
73 catch (std::runtime_error e) {
74 if ( port - inPort > 1000) return false;
75 do {
76 port++;
77 } while ((port == outPort) || (port == errPort));
78 }
79 } while (!done);
80 inPort = port;
81 return true;
82 }
83
84 //--------------------------------------------------------------------------
85 void OSCSetup::stop()
86 {
87 if (fOSCThread) {
88 fOSCThread->stop();
89 OSCStream::stop();
90 delete fOSCThread;
91 fOSCThread = 0;
92 }
93 }
94
95 } // end namespoace