0165c3d42c5ec322e5540e35a146a084d0eeeca3
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / architecture / osclib / faust / src / nodes / RootNode.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 <string>
25 #include <sstream>
26
27 #include "RootNode.h"
28 #include "Message.h"
29 #include "OSCStream.h"
30 #include "OSCControler.h"
31 #include "OSCIO.h"
32
33 #ifdef WIN32
34 # include "winsock2.h"
35 #else
36 # include "ip/NetworkingUtils.h"
37 #endif
38
39 using namespace std;
40
41 namespace oscfaust
42 {
43
44 static const char * kHelloMsg = "hello";
45
46
47 //--------------------------------------------------------------------------
48 // ip address utility
49 //--------------------------------------------------------------------------
50 static string getHostName()
51 {
52 char name[512];
53 int ret = gethostname(name, 512);
54 if (ret == -1) return "";
55 return name;
56 }
57
58 string getIP()
59 {
60 string name = getHostName();
61 stringstream ipStr;
62 if (name.size()) {
63 unsigned long ip = GetHostByName(name.c_str());
64 ipStr << ((ip >> 24) & 0xff) << '.'
65 << ((ip >> 16) & 0xff) << '.'
66 << ((ip >> 8) & 0xff) << '.'
67 << (ip & 0xff);
68 }
69 return ipStr.str();
70 }
71
72 //--------------------------------------------------------------------------
73 // signal data handler
74 //--------------------------------------------------------------------------
75 bool RootNode::acceptSignal( const Message* msg )
76 {
77 bool ret = true;
78 int n = msg->size();
79 if (n) {
80 float val, * buff = new float[n];
81 for (int i = 0; i < n ; i++) {
82 if (msg->param(i, val)) // assumes that it receives float values only
83 buff[i] = val;
84 else { // in case not
85 ret = false; // set return code to false
86 break; // and stops reading data
87 }
88 }
89 if (ret) fIO->receive (n, buff); // call the IO controler receive method with the float data
90 delete buff;
91 }
92 else ret = false;
93 return ret;
94 }
95
96 //--------------------------------------------------------------------------
97 bool RootNode::accept( const Message* msg )
98 {
99 string val;
100 // checks for the 'hello' message first
101 if ((msg->size() == 1) && (msg->param(0, val)) && (val == kHelloMsg) ) {
102 hello (msg->src());
103 return true;
104 }
105 else if (MessageDriven::accept (msg)) // next checks for standard handlers ('get' for example)
106 return true;
107 else if (fIO) // when still not handled and if a IO controler is set
108 return acceptSignal (msg); // try to read signal data
109 return false;
110 }
111
112 //--------------------------------------------------------------------------
113 void RootNode::setPorts (int* in, int* out, int* err)
114 {
115 fUPDIn = in;
116 fUDPOut = out;
117 fUDPErr = err;
118 }
119
120 //--------------------------------------------------------------------------
121 void RootNode::hello (unsigned long ipdest ) const
122 {
123 if (fUPDIn && fUDPOut && fUDPErr) { // on 'hello' request
124 unsigned long savedip = oscout.getAddress(); // saves the current dest IP
125 oscout.setAddress(ipdest); // set the destination IP
126 // and sends its address + the udp port numbers (in, out and err)
127 oscout << OSCStart(getOSCAddress().c_str()) << getIP() << *fUPDIn << *fUDPOut << *fUDPErr << OSCEnd();
128 oscout.setAddress(savedip); // and restores the dest IP
129 }
130 }
131
132 } // end namespoace