f29e764888b38830a9cd7220c5a206031fd6c24a
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / architecture / osclib / faust / src / osc / OSCListener.cpp
1 /*
2
3 Faust Project
4
5 Copyright (C) 2011 Grame
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
22 research@grame.fr
23
24 */
25
26 #include <iostream>
27
28 #include "OSCListener.h"
29 #include "Message.h"
30
31 #include "osc/OscReceivedElements.h"
32 #include "ip/IpEndpointName.h"
33
34 using namespace std;
35 using namespace osc;
36
37 namespace oscfaust
38 {
39
40 //--------------------------------------------------------------------------
41 OSCListener::OSCListener(MessageProcessor *mp, int port)
42 : fSocket(0), fMsgHandler(mp),
43 fRunning(false), fPort(port)
44 {
45 fSocket = new UdpListeningReceiveSocket(IpEndpointName( IpEndpointName::ANY_ADDRESS, fPort ), this);
46 fPort = 0;
47 }
48
49 OSCListener::~OSCListener() { stop(); delete fSocket; }
50
51 //--------------------------------------------------------------------------
52 void OSCListener::run()
53 {
54 fRunning = true;
55 while (fRunning) {
56 try {
57 if (fPort) {
58 delete fSocket;
59 fSocket = new UdpListeningReceiveSocket(IpEndpointName( IpEndpointName::ANY_ADDRESS, fPort ), this);
60 fPort = 0;
61 }
62 fSocket->Run();
63 }
64 catch (osc::Exception e) {
65 cerr << "osc error: " << e.what() << endl;
66 }
67 }
68 }
69
70 //--------------------------------------------------------------------------
71 void OSCListener::ProcessMessage( const osc::ReceivedMessage& m, const IpEndpointName& src )
72 {
73 Message* msg = new Message(m.AddressPattern());
74 msg->setSrcIP (src.address);
75 ReceivedMessageArgumentIterator i = m.ArgumentsBegin();
76 while (i != m.ArgumentsEnd()) {
77 if (i->IsString()) {
78 msg->add<string>(i->AsStringUnchecked());
79 }
80 else if (i->IsInt32()) {
81 msg->add<int>(i->AsInt32Unchecked());
82 }
83 else if (i->IsFloat()) {
84 msg->add<float>(i->AsFloatUnchecked());
85 }
86 i++;
87 }
88 fMsgHandler->processMessage (msg);
89 delete msg;
90 }
91
92 } // end namespoace