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

ippEngine.cpp

Go to the documentation of this file.
00001 // 
00002 // DISCLAIMER: 
00003 //  This software was produced by the National Institute of Standards 
00004 //  and Technology (NIST), an agency of the U.S. government, and by statute is 
00005 //  not subject to copyright in the United States.  Recipients of this 
00006 //  software assume all responsibility associated with its operation,
00007 //  modification,maintenance, and subsequent redistribution. 
00008 //
00009 //  See NIST Administration Manual 4.09.07 b and Appendix I. 
00010 //
00011 // History
00012 // Who When     What
00013 // --- -------- ---------------------------------------------------------------
00014 // ER  09/03/06 Allow multiline transaction
00015 //-----------------------------------------------------------------------------
00016 #include "stdafx.h"
00017 #include "ippdme/Server/ippEngine.h"
00018 #include "ippdme/Server/ippLogger.h"
00019 #include "ippdme/assert.h"
00020 
00021 ippEngine::ippEngine()
00022 :_logger(0)
00023 {
00024         memset(_assemblyBuffer, 0, sizeof(_assemblyBuffer));
00025 }
00026 
00027 
00028 ippEngine::~ippEngine()
00029 {
00030   _socket.Close();
00031 }
00032 
00033 void ippEngine::setLogger(ippLogger* logger)
00034 {
00035   _logger = logger;
00036 }
00037 
00038 void ippEngine::LogMessage(const char* data1,const char* data2)
00039 {
00040   if(_logger) { 
00041     std::string str(data1);
00042     if (data2) {
00043       str+=data2;
00044     }
00045     _logger->WriteLine(str.c_str());
00046   }
00047 }
00048 
00049 
00053 void ippEngine::PerformRead()
00054 // History
00055 // Who When     What
00056 // --- -------- ---------------------------------------------------------------
00057 // ER  08/03/06 Account for multiple lines to be parsed in one packet
00058 //-----------------------------------------------------------------------------
00059 {  
00060   
00061   static char _data[IPPSIZE];
00062   memset(_data,0,sizeof(_data));
00063 
00064   if (!readdata(_data,sizeof(_data)))  {
00065     return; // no data ready or in error state
00066   }
00067   
00068   if(_data[0] == 0 ) { 
00069     return; // we read a empty string
00070   }
00071 
00072         // concatenate with existing data in buffer
00073   strcat(_assemblyBuffer, _data);
00074   
00075 
00076         int lastChar = strlen(_assemblyBuffer);
00077   int endLine = 0;
00078 
00079   while (endLine == 0 ) {
00080     //Scan the buffer for carriage returns and line feeds
00081           int crCount=0; 
00082           int lfCount=0;
00083     int invalidCount = 0;
00084 
00085           for (int i=0; i<lastChar; i++) {
00086       char c = _assemblyBuffer[i];
00087                   if(c==13) { crCount++; }
00088                   else if(c==10) { lfCount++; }
00089       else if ( c<' ' || c>'~') {
00090         invalidCount++; // character must be >32 and <126 except for CR and LF
00091       }
00092       if (_assemblyBuffer[i-1]=='\r' && _assemblyBuffer[i]=='\n') {
00093         endLine=i;
00094         break;
00095       }
00096     }   
00097            
00098           if (endLine>0)
00099     {
00100        // marks the string end after \r\n 
00101        // but remember old character to restore it later
00102        int old = _assemblyBuffer[endLine+1];
00103        _assemblyBuffer[endLine+1] = 0;
00104 
00105       IPP_ASSERT(_assemblyBuffer[endLine-1]==13 && _assemblyBuffer[endLine]==10);
00106       if (crCount==1 && lfCount==1 && invalidCount ==0) {
00107         // we must have only one \r\n
00108               ProcessLine(_assemblyBuffer);
00109       } else {
00110         ProcessMisformedLine(_assemblyBuffer);
00111       }
00112       
00113       // restore old char that we previouslt set to 0
00114       _assemblyBuffer[endLine+1] = old;
00115       
00116       // prepare for next portion of the string
00117       memmove(_assemblyBuffer,_assemblyBuffer+endLine+1,lastChar-endLine+1);
00118       lastChar-=endLine+1;
00119       endLine = 0;
00120 
00121       //xx memset(_assemblyBuffer,0,sizeof(_assemblyBuffer));
00122 
00123     } else {
00124       break;
00125     }
00126   }
00127 }
00128 
00129 
00130 
00131 bool ippEngine::readdata(char* buffer,int sizebuffer)
00132 {
00133   if (!_socket.IsValid()){
00134     return false;
00135   }
00136   buffer[0]=0;
00137   int  nRet = _socket.Read(buffer,sizebuffer);
00138   if (nRet != 0 ) {    
00139     if (nRet == ippSocket::SOCKET_WOULD_BLOCK) { 
00140       // no data ready to read yet
00141       return true;    
00142     } else {
00143       // the socket has disconnected
00144       _socket.Close();
00145       OnConnectionLost();
00146       return false;    
00147     }
00148   }
00149   return true;
00150 }
00151 void ippEngine::OnConnectionLost()
00152 {
00153 }
00154 
00155 void ippEngine::ProbeConnection()
00156 {
00157   if (_socket.IsValid()) { 
00158    
00159     if (_socket.Ping() != ippSocket::SOCKET_OK) { 
00160       _socket.Close();
00161       OnConnectionLost();
00162       return;
00163 
00164     }
00165   }
00166 }
00167 
00168 
00169 
00170 ippLogger* ippEngine::GetLogger() const
00171 {
00172   return _logger;
00173 }

Generated on Wed Nov 8 00:20:06 2006 for IPPDME by  doxygen 1.4.1