Changeset 091d894 in nscp for trunk/include


Ignore:
Timestamp:
03/22/05 22:18:05 (8 years ago)
Author:
Michael Medin <michael@…>
Children:
f705d34
Parents:
5d8cb39
Message:
  • Fixed NSClientListener return codes for basic commands
  • Extracted arrayBuffer code into new files under /include
  • Minor tweaks in the API (fixed minor issues) + Added some basic "tests" to a few commands (getToken, and arrayBuffer related)
Location:
trunk/include
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/NSCHelper.cpp

    r55b33c1 r091d894  
    3737} 
    3838#endif 
    39 /** 
    40  * Make a list out of a array of char arrays (arguments type) 
    41  * @param argLen Length of argument array 
    42  * @param *argument[] Argument array 
    43  * @return Argument wrapped as a list 
    44  */ 
    45 std::list<std::string> NSCHelper::arrayBuffer2list(const unsigned int argLen, char *argument[]) { 
    46   std::list<std::string> ret; 
    47   int i=0; 
    48   for (unsigned int i=0;i<argLen;i++) { 
    49     std::string s = argument[i]; 
    50     ret.push_back(s); 
    51   } 
    52   return ret; 
    53 } 
    54 /** 
    55  * Create an arrayBuffer from a list. 
    56  * This is the reverse of arrayBuffer2list. 
    57  * <b>Notice</b> it is up to the caller to free the memory allocated in the returned buffer. 
    58  * 
    59  * @param lst A list to convert. 
    60  * @param &argLen Write the length to this argument. 
    61  * @return A pointer that is managed by the caller. 
    62  */ 
    63 char ** NSCHelper::list2arrayBuffer(const std::list<std::string> lst, unsigned int &argLen) { 
    64   argLen = static_cast<unsigned int>(lst.size()); 
    65   char **arrayBuffer = new char*[argLen]; 
    66   std::list<std::string>::const_iterator it = lst.begin(); 
    67   for (int i=0;it!=lst.end();++it,i++) { 
    68     std::string::size_type alen = (*it).size(); 
    69     arrayBuffer[i] = new char[alen+2]; 
    70     strncpy(arrayBuffer[i], (*it).c_str(), alen+1); 
    71   } 
    72   assert(i == argLen); 
    73   return arrayBuffer; 
    74 } 
    75 /** 
    76  * Creates an empty arrayBuffer (only used to allow consistency) 
    77  * @param &argLen [OUT] The length (items) of the arrayBuffer 
    78  * @return The arrayBuffer 
    79  */ 
    80 char ** NSCHelper::createEmptyArrayBuffer(unsigned int &argLen) { 
    81   argLen = 0; 
    82   char **arrayBuffer = new char*[0]; 
    83   return arrayBuffer; 
    84 } 
    85 /** 
    86  * Joins an arrayBuffer back into a string 
    87  * @param **argument The ArrayBuffer 
    88  * @param argLen The length of the ArrayBuffer 
    89  * @param join The char to use as separators when joining 
    90  * @return The joined arrayBuffer 
    91  */ 
    92 std::string NSCHelper::arrayBuffer2string(char **argument, const unsigned int argLen, std::string join) { 
    93   std::string ret; 
    94   for (unsigned int i=0;i<argLen;i++) { 
    95     ret += argument[i]; 
    96     if (i != argLen-1) 
    97       ret += join; 
    98   } 
    99   return ret; 
    100 } 
    101 /** 
    102  * Split a string into elements as an arrayBuffer 
    103  * @param buffer The CharArray to split along 
    104  * @param splitChar The char to use as splitter 
    105  * @param &argLen [OUT] The length of the Array 
    106  * @return The arrayBuffer 
    107  */ 
    108 char ** NSCHelper::split2arrayBuffer(const char* buffer, char splitChar, unsigned int &argLen) { 
    109   assert(buffer); 
    110   argLen = 0; 
    111   const char *p = buffer; 
    112   while (*p) { 
    113     if (*p == splitChar) 
    114       argLen++; 
    115     p++; 
    116   } 
    117   argLen++; 
    118   char **arrayBuffer = new char*[argLen]; 
    119   p = buffer; 
    120   for (unsigned int i=0;i<argLen;i++) { 
    121     char *q = strchr(p, (i<argLen-1)?splitChar:0); 
    122     unsigned int len = static_cast<int>(q-p); 
    123     arrayBuffer[i] = new char[len+1]; 
    124     strncpy(arrayBuffer[i], p, len); 
    125     arrayBuffer[i][len] = 0; 
    126     p = ++q; 
    127   } 
    128   return arrayBuffer; 
    129 } 
    130  
    131 /** 
    132  * Destroy an arrayBuffer. 
    133  * The buffer should have been created with list2arrayBuffer. 
    134  * 
    135  * @param **argument  
    136  * @param argLen  
    137  */ 
    138 void NSCHelper::destroyArrayBuffer(char **argument, const unsigned int argLen) { 
    139   for (unsigned int i=0;i<argLen;i++) { 
    140     delete [] argument[i]; 
    141   } 
    142   delete [] argument; 
    143 } 
    14439 
    14540 
     
    266161  char ** aBuffer; 
    267162  if (buffer) 
    268     aBuffer= NSCHelper::split2arrayBuffer(buffer, splitChar, argLen); 
     163    aBuffer= arrayBuffer::split2arrayBuffer(buffer, splitChar, argLen); 
    269164  else 
    270     aBuffer= NSCHelper::createEmptyArrayBuffer(argLen); 
     165    aBuffer= arrayBuffer::createEmptyArrayBuffer(argLen); 
    271166  NSCAPI::nagiosReturn ret = InjectCommand(command, argLen, aBuffer, message, perf); 
    272   NSCHelper::destroyArrayBuffer(aBuffer, argLen); 
     167  arrayBuffer::destroyArrayBuffer(aBuffer, argLen); 
     168  return ret; 
     169} 
     170NSCAPI::nagiosReturn NSCModuleHelper::InjectSplitAndCommand(const std::string command, const std::string buffer, char splitChar, std::string & message, std::string & perf) 
     171{ 
     172  if (!fNSAPIInject) 
     173    throw NSCMHExcpetion("NSCore has not been initiated..."); 
     174  unsigned int argLen = 0; 
     175  char ** aBuffer; 
     176  if (buffer.empty()) 
     177    aBuffer= arrayBuffer::createEmptyArrayBuffer(argLen); 
     178  else 
     179    aBuffer= arrayBuffer::split2arrayBuffer(buffer, splitChar, argLen); 
     180  NSCAPI::nagiosReturn ret = InjectCommand(command.c_str(), argLen, aBuffer, message, perf); 
     181  arrayBuffer::destroyArrayBuffer(aBuffer, argLen); 
    273182  return ret; 
    274183} 
  • trunk/include/NSCHelper.h

    r55b33c1 r091d894  
    66#include <iostream> 
    77#include <charEx.h> 
     8#include <arrayBuffer.h> 
    89 
    910namespace NSCHelper 
     
    1516  int wrapReturnString(char *buffer, unsigned int bufLen, std::string str, int defaultReturnCode); 
    1617#endif 
    17  
    18   std::list<std::string> arrayBuffer2list(const unsigned int argLen, char **argument); 
    19   char ** list2arrayBuffer(const std::list<std::string> lst, unsigned int &argLen); 
    20   char ** split2arrayBuffer(const char* buffer, char splitChar, unsigned int &argLen); 
    21   std::string arrayBuffer2string(char **argument, const unsigned int argLen, std::string join); 
    22   char ** createEmptyArrayBuffer(unsigned int &argLen); 
    23   void destroyArrayBuffer(char **argument, const unsigned int argLen); 
    24  
    2518  std::string translateMessageType(NSCAPI::messageTypes msgType); 
    2619  std::string translateReturn(NSCAPI::nagiosReturn returnCode); 
     
    10295  NSCAPI::nagiosReturn InjectCommand(const char* command, const unsigned int argLen, char **argument, std::string & message, std::string & perf); 
    10396  NSCAPI::nagiosReturn InjectSplitAndCommand(const char* command, char* buffer, char splitChar, std::string & message, std::string & perf); 
     97  NSCAPI::nagiosReturn InjectSplitAndCommand(const std::string command, const std::string buffer, char splitChar, std::string & message, std::string & perf); 
    10498  void StopService(void); 
    10599  std::string getBasePath(); 
  • trunk/include/Socket.h

    r6db9fb2 r091d894  
    4343  typedef std::pair<char*,unsigned int> readAllDataBlock; 
    4444  static readAllDataBlock readAll(SOCKET socket) { 
    45     // @todo Is this even working ? 
     45    // @bug Is this even working ? 
    4646    // @todo Nedds *alot* more work... 
    4747    unsigned int buffLen = RECV_BUFFER_LEN; 
     
    4949    char *buff = new char[buffLen]; 
    5050    int n=recv(socket,buff,RECV_BUFFER_LEN,0); 
    51     while ((n==SOCKET_ERROR )||(n==0)) { 
    52       if (n == buffLen) { 
     51    while ((n!=SOCKET_ERROR )||(n!=0)) { 
     52      if (n == RECV_BUFFER_LEN) { 
    5353        char* newBuf = new char[buffLen+RECV_BUFFER_LEN]; 
    5454        memcpy(newBuf, buff, buffLen); 
  • trunk/include/charEx.h

    r4f2e807 r091d894  
    2222    return ret; 
    2323  } 
     24 
     25 
     26 
    2427  typedef std::pair<std::string,char*> token; 
    2528  inline token getToken(char *buffer, char split) { 
    2629    assert(buffer != NULL); 
    2730    char *p = strchr(buffer, split); 
    28     if (!p) { 
     31    if (!p) 
    2932      return token(buffer, NULL); 
    30     } 
     33    if (!p[1]) 
     34      return token(std::string(buffer, p-buffer), NULL); 
    3135    p++; 
    3236    return token(std::string(buffer, p-buffer-1), p); 
    3337  } 
     38#ifdef _DEBUG 
     39  inline void test_getToken(char* in1, char in2, std::string out1, char * out2) { 
     40    token t = getToken(in1, in2); 
     41    std::cout << "charEx::test_getToken(" << in1 << ", " << in2 << ") : "; 
     42    if (t.first == out1)  { 
     43      if ((t.second == NULL) && (out2 == NULL)) 
     44        std::cout << "Succeeded" << std::endl; 
     45      else if (t.second == NULL) 
     46        std::cout << "Failed [NULL=" << out2 << "]" << std::endl; 
     47      else if (out2 == NULL) 
     48        std::cout << "Failed [" << t.second << "=NULL]" << std::endl; 
     49      else if (strcmp(t.second, out2) == 0) 
     50        std::cout << "Succeeded" << std::endl; 
     51      else 
     52        std::cout << "Failed" << std::endl; 
     53    } else 
     54      std::cout << "Failed [" << out1 << "=" << t.first << "]" << std::endl; 
     55  } 
     56  inline void run_test_getToken() { 
     57    test_getToken("", '&', "", NULL); 
     58    test_getToken("&", '&', "", NULL); 
     59    test_getToken("&&", '&', "", "&"); 
     60    test_getToken("foo", '&', "foo", NULL); 
     61    test_getToken("foo&", '&', "foo", NULL); 
     62    test_getToken("foo&bar", '&', "foo", "bar"); 
     63    test_getToken("foo&bar&test", '&', "foo", "bar&test"); 
     64  } 
     65#endif 
     66 
    3467}; 
  • trunk/include/strEx.h

    r107bd0f r091d894  
    5555    return std::pair<std::string,std::string>(str.substr(0, pos), str.substr(pos+key.length())); 
    5656  } 
     57  typedef std::pair<std::string,std::string> token; 
     58  inline token getToken(std::string buffer, char split) { 
     59    std::string::size_type pos = buffer.find(split); 
     60    if (pos == std::string::npos) 
     61      return token(buffer, ""); 
     62    if (pos == buffer.length()-1) 
     63      return token(buffer.substr(0, pos), ""); 
     64    return token(buffer.substr(0, pos-1), buffer.substr(++pos)); 
     65  } 
     66#ifdef _DEBUG 
     67  inline void test_getToken(std::string in1, char in2, std::string out1, std::string out2) { 
     68    token t = getToken(in1, in2); 
     69    std::cout << "strEx::test_getToken(" << in1 << ", " << in2 << ") : "; 
     70    if ((t.first == out1) && (t.second == out2)) 
     71      std::cout << "Succeeded" << std::endl; 
     72    else 
     73      std::cout << "Failed [" << out1 << "=" << t.first << ", " << out2 << "=" << t.second << "]" << std::endl; 
     74  } 
     75  inline void run_test_getToken() { 
     76    test_getToken("", '&', "", ""); 
     77    test_getToken("&", '&', "", ""); 
     78    test_getToken("&&", '&', "", "&"); 
     79    test_getToken("foo", '&', "foo", ""); 
     80    test_getToken("foo&", '&', "foo", ""); 
     81    test_getToken("foo&bar", '&', "foo", "bar"); 
     82    test_getToken("foo&bar&test", '&', "foo", "bar&test"); 
     83  } 
     84#endif 
    5785} 
Note: See TracChangeset for help on using the changeset viewer.