1 // Expose FGProtocol module to Nasal to enable I/O protocols to be implemented in Nasal space, e.g.
2 // to support REST/AJAX, JSON or HTTP WebSockets
4 // Copyright (C) 2013 The FlightGear community
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "NasalFGProtocol.hxx"
25 #include <Main/globals.hxx>
26 #include <Main/util.hxx>
28 #include <simgear/nasal/cppbind/from_nasal.hxx>
29 #include <simgear/nasal/cppbind/to_nasal.hxx>
30 #include <simgear/nasal/cppbind/NasalHash.hxx>
31 #include <simgear/nasal/cppbind/Ghost.hxx>
33 class NasalProtocolWrapper;
35 typedef boost::shared_ptr<NasalProtocolWrapper> NasalProtocolWrapper_ptr;
36 typedef nasal::Ghost< NasalProtocolWrapper_ptr > NasalProtocol;
38 class NasalProtocolWrapper : public FGProtocol {
52 NasalProtocolWrapper::open() {
53 SG_LOG(SG_GENERAL, SG_ALERT, "::open" << std::endl);
58 NasalProtocolWrapper::process() {
59 SG_LOG(SG_GENERAL, SG_ALERT, "::process" << std::endl);
64 NasalProtocolWrapper::close() {
65 SG_LOG(SG_GENERAL, SG_ALERT, "::close" << std::endl);
70 NasalProtocolWrapper::gen_message() {
71 SG_LOG(SG_GENERAL, SG_ALERT, "::gen_message" << std::endl);
76 NasalProtocolWrapper::parse_message() {
77 SG_LOG(SG_GENERAL, SG_ALERT, "::parse_message" << std::endl);
82 NasalProtocolWrapper::set_direction() {
83 SG_LOG(SG_GENERAL, SG_ALERT, "::set_direction" << std::endl);
88 naRef to_nasal_helper(naContext c, NasalProtocolWrapper *obj)
90 NasalProtocolWrapper_ptr ptr(obj);
91 return NasalProtocol::create(c, ptr );
96 from_nasal_helper(naContext c, naRef ref, const NasalProtocolWrapper*)
98 return (NasalProtocolWrapper*) naGhost_ptr(ref);
101 static naRef f_new_protocol(const nasal::CallContext& ctx)
104 return ctx.to_nasal( new NasalProtocolWrapper );
107 //------------------------------------------------------------------------------
108 naRef initNasalFGProtocol(naRef globals, naContext c)
110 NasalProtocol::init("io.protocol")
111 // This is where you want to expose various methods from FGProtocol/NetChat/NetChannel
112 .method("open", &NasalProtocolWrapper::open)
113 .method("close", &NasalProtocolWrapper::close)
114 .method("gen_message", &NasalProtocolWrapper::gen_message)
115 .method("parse_message", &NasalProtocolWrapper::parse_message)
116 .method("set_direction", &NasalProtocolWrapper::set_direction);
118 nasal::Hash globals_module(globals, c);
119 nasal::Hash protocol = globals_module.createHash("protocol");
120 protocol.set("new", &f_new_protocol);