]> git.mxchange.org Git - flightgear.git/blob - src/Network/native.cxx
fffabba3fd8c7f05a21acbec5e340d6d60d69a48
[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 <Debug/logstream.hxx>
25 #include <Math/fg_geodesy.hxx>
26 #include <Time/fg_time.hxx>
27
28 #include "iochannel.hxx"
29 #include "native.hxx"
30
31
32 FGNative::FGNative() {
33 }
34
35 FGNative::~FGNative() {
36 }
37
38
39 // open hailing frequencies
40 bool FGNative::open() {
41     if ( is_enabled() ) {
42         FG_LOG( FG_IO, FG_ALERT, "This shouldn't happen, but the channel " 
43                 << "is already in use, ignoring" );
44         return false;
45     }
46
47     FGIOChannel *io = get_io_channel();
48
49     if ( ! io->open( get_direction() ) ) {
50         FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
51         return false;
52     }
53
54     set_enabled( true );
55
56     return true;
57 }
58
59
60 // process work for this port
61 bool FGNative::process() {
62     FGIOChannel *io = get_io_channel();
63     int length = sizeof(*cur_fdm_state);
64
65     if ( get_direction() == out ) {
66         // cout << "size of cur_fdm_state = " << length << endl;
67         buf = *cur_fdm_state;
68         if ( ! io->write( (char *)(& buf), length ) ) {
69             FG_LOG( FG_IO, FG_ALERT, "Error writing data." );
70             return false;
71         }
72     } else if ( get_direction() == in ) {
73         if ( io->read( (char *)(& buf), length ) == length ) {
74             FG_LOG( FG_IO, FG_ALERT, "Success reading data." );
75             *cur_fdm_state = buf;
76         } else {
77             FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
78             return false;
79         }
80     }
81
82     return true;
83 }
84
85
86 // close the channel
87 bool FGNative::close() {
88     FGIOChannel *io = get_io_channel();
89
90     set_enabled( false );
91
92     if ( ! io->close() ) {
93         return false;
94     }
95
96     return true;
97 }