]> git.mxchange.org Git - flightgear.git/blob - src/Network/native.cxx
f60f0cc90ae308816e4bf2ed25c08f74aff9b2e2
[flightgear.git] / src / Network / native.cxx
1 // native.cxx -- FGFS "Native" protocal class
2 //
3 // Written by Curtis Olson, started November 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/math/fg_geodesy.hxx>
26
27 #include "iochannel.hxx"
28 #include "native.hxx"
29
30
31 FGNative::FGNative() {
32 }
33
34 FGNative::~FGNative() {
35 }
36
37
38 // open hailing frequencies
39 bool FGNative::open() {
40     if ( is_enabled() ) {
41         FG_LOG( FG_IO, FG_ALERT, "This shouldn't happen, but the channel " 
42                 << "is already in use, ignoring" );
43         return false;
44     }
45
46     FGIOChannel *io = get_io_channel();
47
48     if ( ! io->open( get_direction() ) ) {
49         FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
50         return false;
51     }
52
53     set_enabled( true );
54
55     return true;
56 }
57
58
59 // process work for this port
60 bool FGNative::process() {
61     FGIOChannel *io = get_io_channel();
62     int length = sizeof(*cur_fdm_state);
63
64     if ( get_direction() == out ) {
65         // cout << "size of cur_fdm_state = " << length << endl;
66         buf = *cur_fdm_state;
67         if ( ! io->write( (char *)(& buf), length ) ) {
68             FG_LOG( FG_IO, FG_ALERT, "Error writing data." );
69             return false;
70         }
71     } else if ( get_direction() == in ) {
72         if ( io->get_type() == fgFileType ) {
73             if ( io->read( (char *)(& buf), length ) == length ) {
74                 FG_LOG( FG_IO, FG_ALERT, "Success reading data." );
75                 *cur_fdm_state = buf;
76             }
77         } else {
78             while ( io->read( (char *)(& buf), length ) == length ) {
79                 FG_LOG( FG_IO, FG_ALERT, "Success reading data." );
80                 *cur_fdm_state = buf;
81             }
82         }
83     }
84
85     return true;
86 }
87
88
89 // close the channel
90 bool FGNative::close() {
91     FGIOChannel *io = get_io_channel();
92
93     set_enabled( false );
94
95     if ( ! io->close() ) {
96         return false;
97     }
98
99     return true;
100 }