Debian Codegraph @

src/Network/native_ctrls.cxx

raw symbols SCIP
1 // native_ctrls.cxx -- FGFS "Native" controls I/O class
2 //
3 // Written by Curtis Olson, started July 2001.
4 //
5 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/io/iochannel.hxx>
30 #include <simgear/io/lowlevel.hxx> // endian tests
31
32 #if FG_HAVE_DDS
33 #include <simgear/io/SGDataDistributionService.hxx>
34 #endif
35
36 #include <Main/fg_props.hxx>
37 #include <Scenery/scenery.hxx> // ground elevation
38
39 #include "native_structs.hxx"
40 #include "native_ctrls.hxx"
41
42 // FreeBSD works better with this included last ... (?)
43 #if defined( _MSC_VER )
44 # include <windows.h>
45 #elif defined( __MINGW32__ )
46 # include <winsock2.h>
47 #else
48 # include <netinet/in.h> // htonl() ntohl()
49 #endif
50
51 // open hailing frequencies
52 bool FGNativeCtrls::open() {
53 if ( is_enabled() ) {
54 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
55 << "is already in use, ignoring" );
56 return false;
57 }
58
59 SGIOChannel *io = get_io_channel();
60
61 #if FG_HAVE_DDS
62 if ( io->get_type() == sgDDSType ) {
63 SG_DDS_Topic *dds = static_cast<SG_DDS_Topic*>(io);
64 dds->setup("FG_DDS_Ctrls", &FG_DDS_Ctrls_desc, sizeof(FG_DDS_Ctrls));
65 }
66 #endif
67
68 if ( ! io->open( get_direction() ) ) {
69 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
70 return false;
71 }
72
73 set_enabled( true );
74
75 return true;
76 }
77
78 // process work for this port
79 bool FGNativeCtrls::process() {
80 SGIOChannel *io = get_io_channel();
81 int length;
82 char *buf;
83
84 if ( io->get_type() == sgDDSType ) {
85 buf = reinterpret_cast<char*>(&ctrls.dds);
86 length = sizeof(FG_DDS_Ctrls);
87 } else {
88 buf = reinterpret_cast<char*>(&ctrls.net);
89 length = sizeof(FGNetCtrls);
90 }
91
92 if ( get_direction() == SG_IO_OUT )
93 {
94 if ( io->get_type() == sgDDSType ) {
95 FGProps2Ctrls( globals->get_props(), &ctrls.dds, true, true );
96 } else {
97 FGProps2Ctrls( globals->get_props(), &ctrls.net, true, true );
98 }
99
100 if ( ! io->write( buf, length ) ) {
101 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
102 return false;
103 }
104 }
105 else if ( get_direction() == SG_IO_IN )
106 {
107 if ( io->get_type() == sgFileType ) {
108 if ( io->read( buf, length ) == length ) {
109 SG_LOG( SG_IO, SG_INFO, "Success reading data." );
110 FGCtrls2Props( globals->get_props(), &ctrls.net, true, true );
111 }
112 } else if ( io->get_type() == sgDDSType ) {
113 while ( io->read( buf, length ) == length ) {
114 SG_LOG( SG_IO, SG_INFO, "Success reading data." );
115 FGCtrls2Props( globals->get_props(), &ctrls.dds, true, true );
116 }
117 } else {
118 while ( io->read( buf, length ) == length ) {
119 SG_LOG( SG_IO, SG_INFO, "Success reading data." );
120 FGCtrls2Props( globals->get_props(), &ctrls.net, true, true );
121 }
122 }
123 }
124
125 return true;
126 }
127
128 // close the channel
129 bool FGNativeCtrls::close() {
130 SGIOChannel *io = get_io_channel();
131
132 set_enabled( false );
133
134 if ( ! io->close() ) {
135 return false;
136 }
137
138 return true;
139 }
140