Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

OrbitObserver/AccelerationClient.cpp

Go to the documentation of this file.
00001 
00002 
00003 #include "AccelerationClient.h"
00004 
00005 using namespace std;
00006 
00007 /*! \brief Default constructor. Object will be useless until <code>Connect()</code>
00008  *   is called.
00009  */
00010 AccelerationMessage::AccelerationMessage( )
00011     : m_clientSocket( NULL )
00012 {
00013         m_deltaV = 0;
00014 }
00015 
00016 /*! \brief Constructs the device and calls Connect().  This method has no return
00017  *  value so failures in the Connect() method may not be reported.  If
00018  * failures must be handled, you should use the default constructor and
00019  * call Connect() explicitly.
00020  *
00021  * @param serverName argument is the hostname or dotted decimal IP address at
00022  * which Sim Propagator is to be found.  The port number is presumed to be the
00023  * default port.
00024  * @param port
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 /*! \brief Destroys the AccelerationMessage.
00034  */
00035 AccelerationMessage::~AccelerationMessage( )
00036 {
00037         delete m_clientSocket;
00038 }
00039 
00040 /*! \brief Attempts to connect the SimPropagator.
00041  *
00042  * @param serverName argument is the hostname or dotted decimal IP address at
00043  * which the Sim Propagator is to be found.
00044  * @param portNumber argument is the port to access on the server.
00045  * @return 0 on success or -1 on failure.
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 /*! \brief Send a message that is stored in m_message to the acceleration server
00073  *
00074  *  returns the length of the message or -1 for failure.
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 /*! \brief Receive a message from the acceleration server
00096  *
00097  *  returns the length of the message or -1 for failure.
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 /*! \brief Parses response from Acceleration Server.
00119  *
00120  *  $PASHR ACK
00121  *  $PASHR NACK
00122  *  $PASHR VEL deltaV
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 /*! \brief Convert string to int for switch case
00162  *
00163  *  @param data
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 /*! \brief Send acceleration message to acceleration server
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 }

Generated on Wed Sep 5 12:54:17 2007 for DSACSS Operational Code by  doxygen 1.3.9.1