00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "AccelerationClient.h"
00014
00015 using namespace std;
00016
00017
00018
00019
00020 AccelerationMessage::AccelerationMessage( )
00021 : m_clientSocket( NULL )
00022 {
00023 m_deltaV = 0;
00024 }
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 AccelerationMessage::AccelerationMessage( const char *serverName, int port )
00037 : m_clientSocket( NULL )
00038 {
00039 Connect( serverName, port );
00040 m_deltaV = 0;
00041 }
00042
00043
00044
00045 AccelerationMessage::~AccelerationMessage( )
00046 {
00047 delete m_clientSocket;
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057 int AccelerationMessage::Connect( const char *serverName, int portNumber )
00058 {
00059 int returnValue = -1;
00060
00061 try
00062 {
00063
00064 if ( m_clientSocket != NULL)
00065 {
00066 delete m_clientSocket;
00067 }
00068
00069 m_clientSocket = new ClientSocket;
00070
00071 m_clientSocket -> Connect( serverName, portNumber );
00072 }
00073 catch ( SocketException& error )
00074 {
00075 std::cout << "Exception was caught: " << error.description( ) << "\n";
00076 return( returnValue );
00077 }
00078 returnValue = 0;
00079 return( returnValue );
00080 }
00081
00082
00083
00084
00085
00086 int AccelerationMessage::SendMessage( )
00087 {
00088 int returnValue = -1;
00089
00090 try
00091 {
00092 *m_clientSocket << m_message;
00093 }
00094 catch ( SocketException& error )
00095 {
00096 std::cout << "Exception was caught: " << error.description( ) << "\n";
00097 return( returnValue );
00098 }
00099
00100 returnValue = m_message.length( );
00101 return ( returnValue );
00102 }
00103
00104
00105
00106
00107
00108
00109 int AccelerationMessage::ReceiveMessage( )
00110 {
00111 int returnValue = -1;
00112
00113 try
00114 {
00115 *m_clientSocket >> m_response;
00116 }
00117 catch ( SocketException& error )
00118 {
00119 std::cout << "Exception was caught: " << error.description( ) << "\n";
00120 return( returnValue );
00121 }
00122
00123 returnValue = m_response.length( );
00124 return ( returnValue );
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134 void AccelerationMessage::CheckResponse( )
00135 {
00136 stringstream _response( m_response );
00137 string command;
00138 string function;
00139
00140 _response >> command;
00141
00142 switch ( ConvertString( command ) )
00143 {
00144 case $PASHR:
00145 _response >> function;
00146 switch( ConvertString( function ) )
00147 {
00148 case VEL:
00149 _response >> m_deltaV;
00150 break;
00151 case ACK:
00152 cout << "Command Successfully Sent." << endl;
00153 break;
00154 case NACK:
00155 cout << "WARNING: Command NOT Successfully Sent. " << endl;
00156 break;
00157 default:
00158 cout << "WARNING: BAD FUNCTION RESPONSE FROM ACCELERATION SERVER " << function << endl;
00159 break;
00160 }
00161 break;
00162
00163 default:
00164 cout << "WARNING: BAD RESPONSE FROM ACCELERATION SERVER " << m_response << endl;
00165 break;
00166
00167 }
00168
00169 }
00170
00171
00172
00173
00174
00175 int AccelerationMessage::ConvertString( string data )
00176 {
00177 if( data == "$PASHR" ) return( $PASHR );
00178 else if( data == "VEL" ) return( VEL );
00179 else if( data == "ACK" ) return( ACK );
00180 else if( data == "NACK" ) return( NACK );
00181 else return( -1 );
00182 }
00183
00184
00185
00186
00187 void AccelerationMessage::SendAccelerationMessage( int _spaceVehicleNumber, double _startTime, double _duration, CAMdoubleVector _acceleration, double _mass )
00188 {
00189 stringstream _message;
00190 _message.precision( 16 );
00191 _message << "$PASHS ACC " << _spaceVehicleNumber << " "
00192 << _startTime << " "
00193 << _duration << " "
00194 << _acceleration(1) << " "
00195 << _acceleration(2) << " "
00196 << _acceleration(3) << " "
00197 << _mass << endl;
00198 m_message = _message.str( );
00199
00200 if( SendMessage( ) == -1 )
00201 cerr << "WARNING: MESSAGE FAILED TO SEND CORRECTLY TO ACCELERATION SERVER " << endl;
00202
00203 if( ReceiveMessage( ) == -1 )
00204 cerr << "WARNING: MESSAGE FAILED TO RECEIVE CORRECTLY FROM ACCELERATION SERVER " << endl;
00205
00206 CheckResponse( );
00207 }
00208
00209
00210 double AccelerationMessage::GetDeltaV( )
00211 {
00212 stringstream _message;
00213 _message.precision( 18 );
00214 _message << "$PASHQ VEL" << endl;
00215 m_message = _message.str( );
00216
00217 if( SendMessage( ) == -1 )
00218 cerr << "WARNING: MESSAGE FAILED TO SEND CORRECTLY TO ACCELERATION SERVER " << endl;
00219
00220 if( ReceiveMessage( ) == -1 )
00221 cerr << "WARNING: MESSAGE FAILED TO RECEIVE CORRECTLY FROM ACCELERATION SERVER " << endl;
00222
00223 CheckResponse( );
00224
00225 return( m_deltaV );
00226 }
00227
00228
00229 void AccelerationMessage::ResetValues( )
00230 {
00231 stringstream _message;
00232 _message.precision( 18 );
00233 _message << "$PASHS RST" << endl;
00234 m_message = _message.str( );
00235
00236 if( SendMessage( ) == -1 )
00237 cerr << "WARNING: MESSAGE FAILED TO SEND CORRECTLY TO ACCELERATION SERVER " << endl;
00238
00239 if( ReceiveMessage( ) == -1 )
00240 cerr << "WARNING: MESSAGE FAILED TO RECEIVE CORRECTLY FROM ACCELERATION SERVER " << endl;
00241
00242 CheckResponse( );
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254