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