dnxstats.c

Go to the documentation of this file.
00001 /*--------------------------------------------------------------------------
00002 
00003    Copyright (c) 2006-2007, Intellectual Reserve, Inc. All rights reserved.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License version 2 as
00007    published by the Free Software Foundation.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; if not, write to the Free Software
00016    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018   --------------------------------------------------------------------------*/
00019 
00031 #if HAVE_CONFIG_H
00032 # include "config.h"
00033 #endif
00034 
00035 #ifndef VERSION
00036 # define VERSION "<unknown>"
00037 #endif
00038 
00039 #include "dnxProtocol.h"
00040 #include "dnxTransport.h"
00041 #include "dnxError.h"
00042 #include "dnxDebug.h"
00043 
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <string.h>
00047 #include <unistd.h>
00048 #include <assert.h>
00049 
00050 //----------------------------------------------------------------------------
00051 
00056 static void usage(char * base)
00057 {
00058    fprintf(stderr,
00059          "Usage: %s [options]\n"
00060          "Where [options] are:\n"
00061          "  -s <host>     specify target host name (default: localhost).\n"
00062          "  -p <port>     specify target port number (default: 12482).\n"
00063          "  -c \"<cmdstr>\" send <cmdstr> to server. (Hint: Try sending \"HELP\".)\n"
00064          "  -v            print version and exit.\n"
00065          "  -h            print this help and exit.\n\n", base);
00066    exit(-1);
00067 }
00068 
00069 //----------------------------------------------------------------------------
00070 
00079 int main(int argc, char ** argv)
00080 {
00081    extern char * optarg;
00082    extern int optind, opterr, optopt;
00083    int ch, ret;
00084    char * cp, * prog, * cmdstr;
00085    char * hoststr, * portstr;
00086 
00087    // get program base name
00088    prog = (char *)((cp = strrchr(argv[0], '/')) != 0 ? (cp + 1) : argv[0]);
00089 
00090    // parse arguments
00091    hoststr = "localhost";
00092    portstr = "12482";
00093    opterr = 0;
00094    cmdstr = 0;
00095    while ((ch = getopt(argc, argv, "hvc:s:p:")) != -1)
00096    {
00097       switch (ch)
00098       {
00099          case 's':
00100             hoststr = optarg;
00101             break;
00102 
00103          case 'p':
00104             portstr = optarg;
00105             break;
00106 
00107          case 'c':
00108             cmdstr = optarg;
00109             break;
00110 
00111          case 'v':
00112             printf("\n  %s version %s\n  Bug reports: %s.\n\n",
00113                   prog, VERSION, PACKAGE_BUGREPORT);
00114             exit(0);
00115 
00116          case 'h':
00117          default :
00118             usage(prog);
00119       }
00120    }
00121 
00122    // ensure we've been given a command
00123    if (!cmdstr)
00124    {
00125       fprintf(stderr, "%s: No command string specified.\n", prog);
00126       usage(prog);
00127    }
00128 
00129    // init comm sub-system; send command; wait for response
00130    if ((ret = dnxChanMapInit(0)) != 0)
00131       fprintf(stderr, "%s: Error initializing channel map: %s.\n",
00132             prog, dnxErrorString(ret));
00133    else
00134    {
00135       char url[1024];
00136 
00137       snprintf(url, sizeof url, "udp://%s:%s", hoststr, portstr);
00138 
00139       if ((ret = dnxChanMapAdd("MgmtClient", url)) != 0)
00140          fprintf(stderr, "%s: Error adding channel (%s): %s.\n",
00141                prog, url, dnxErrorString(ret));
00142       else
00143       {
00144          DnxChannel * channel;
00145 
00146          if ((ret = dnxConnect("MgmtClient", DNX_MODE_ACTIVE, &channel)) != 0)
00147             fprintf(stderr, "%s: Error connecting to server (%s): %s.\n",
00148                   prog, url, dnxErrorString(ret));
00149          else
00150          {
00151             DnxMgmtRequest req;
00152 
00153             memset(&req, 0, sizeof req);
00154             dnxMakeXID(&req.xid, DNX_OBJ_MANAGER, 0, 0);
00155             req.action = cmdstr;
00156 
00157             if ((ret = dnxSendMgmtRequest(channel, &req)) != 0)
00158                fprintf(stderr, "%s: Error sending request: %s.\n",
00159                      prog, dnxErrorString(ret));
00160             else
00161             {
00162                DnxMgmtReply rsp;
00163 
00164                if ((ret = dnxWaitForMgmtReply(channel, &rsp, 10)) != 0)
00165                   fprintf(stderr, "%s: Error receiving response: %s.\n",
00166                         prog, dnxErrorString(ret));
00167                else
00168                {
00169                   if (rsp.status == DNX_REQ_ACK)
00170                      printf("%s\n", rsp.reply);
00171                   else
00172                      fprintf(stderr, "%s: Request failed on server.\n"
00173                            "Response was (%s)\n", prog, rsp.reply);
00174 
00175                }
00176             }
00177             dnxDisconnect(channel);
00178          }
00179          dnxChanMapDelete("MgmtClient");
00180       }
00181       dnxChanMapRelease();
00182    }
00183 
00184    xheapchk();
00185 
00186    return ret? -1: 0;
00187 }
00188 
00189 //----------------------------------------------------------------------------
00190 

Generated on Tue Apr 13 15:48:07 2010 for DNX by  doxygen 1.5.6