Refactoring of file beam.ml.
[Faustine.git] / interpretor / faust-0.9.47mr3 / architecture / osclib / faust / src / osc / OSCStream.cpp
1 /*
2 Copyright (c) Grame 2009
3
4 This library is free software; you can redistribute it and modify it under
5 the terms of the GNU Library General Public License as published by the
6 Free Software Foundation version 2 of the License, or any later version.
7
8 This library is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
11 for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16
17 Grame Research Laboratory, 9, rue du Garet 69001 Lyon - France
18 research@grame.fr
19
20 */
21
22 #include <iostream>
23 #include "OSCStream.h"
24
25 using namespace std;
26
27 namespace oscfaust
28 {
29
30 OSCStream* _oscout = 0; // OSC standard output stream
31 OSCStream* _oscerr = 0; // OSC standard input stream
32
33 static UdpSocket* _socket = 0; // a shared transmit socket
34
35
36 //--------------------------------------------------------------------------
37 OSCStream::OSCStream ()
38 : fState(kIdle), fPort(1024), fAddress(kLocalhost), fOutStream(fBuffer, kOutBufferSize), fSocket(_socket)
39 {
40 if (!fSocket) cerr << "warning: incorrect OSCStream, _socket not initialized" << endl;
41 }
42
43 //--------------------------------------------------------------------------
44 bool OSCStream::start ()
45 {
46 _socket = new UdpSocket;
47 _oscout = new OSCStream(_socket);
48 _oscerr = new OSCStream(_socket);
49 return (_socket && _oscout && _oscerr);
50 }
51
52 //--------------------------------------------------------------------------
53 void OSCStream::stop ()
54 {
55 delete _socket;
56 delete _oscout;
57 delete _oscerr;
58 _oscout = _oscerr = 0;
59 _socket = 0;
60 }
61
62 //--------------------------------------------------------------------------
63 void OSCStream::setAddress (const string& address)
64 {
65 IpEndpointName dst (address.c_str());
66 setAddress (dst.address);
67 }
68
69 //--------------------------------------------------------------------------
70 OSCStream& OSCStream::start(const char * address)
71 {
72 stream().Clear();
73 if (!stream().IsReady()) cerr << "OSCStream OutboundPacketStream not ready" << endl;
74 stream() << osc::BeginMessage( address ) ;
75 fState = kInProgress;
76 return *this;
77 }
78
79 //--------------------------------------------------------------------------
80 OSCStream& OSCStream::end()
81 {
82 if (state() == kInProgress) {
83 stream() << osc::EndMessage;
84 if (fSocket)
85 fSocket->SendTo (IpEndpointName (fAddress, fPort), stream().Data(), stream().Size() );
86 fState = kIdle;
87 }
88 return *this;
89 }
90
91 //--------------------------------------------------------------------------
92 OSCStream& operator <<(OSCStream& s, const string& val)
93 {
94 s.stream() << val.c_str();
95 return s;
96 }
97
98 //--------------------------------------------------------------------------
99 OSCStream& operator <<(OSCStream& s, const OSCErr& val) { return s.start(val.fAddress); }
100 OSCStream& operator <<(OSCStream& s, const OSCWarn& val) { return s.start(val.fAddress); }
101 OSCStream& operator <<(OSCStream& s, const OSCStart& val) { return s.start(val.fAddress); }
102 OSCStream& operator <<(OSCStream& s, const OSCEnd val) { return s.end(); }
103
104 } // end namespace
105