Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 255 lines (203 sloc) 7.201 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
/*
Copyright (c) 2010, Yahoo! Inc. All rights reserved.

Redistribution and use of this software in source and binary forms, with or
without modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

* Neither the name of Yahoo! Inc. nor the names of its contributors may be used
to endorse or promote products derived from this software without specific prior
written permission of Yahoo! Inc.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//////////////////////////////////////////////////////////////////////
// A general purpose program to exchange a single request with a
// service. Good for testing/debugging a service.
//////////////////////////////////////////////////////////////////////

#include "config.h"

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>

#include <sys/time.h>

#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "util.h"
#include "pluton/client.h"

using namespace std;


static const char* usage =
"Usage: plSend [-edhno] [-L lookupMap] [-C name=context .. ]\n"
" [-t timeoutSeconds] ServiceKey [requestString]\n"
"\n"
"Exchange a single request with a pluton service. Print the resulting data\n"
"to STDOUT and exit(0) on a successful request, otherwise print the fault\n"
"information to STDERR and exit(1).\n"
"\n"
" -C Set the context for this request - multiple occurrences ok\n"
" -d Turn on pluton::client debugging\n"
" -e Print elapse time of the request to STDERR\n"
" -h Print this usage message on STDOUT and exit(0)\n"
" -L The Lookup Map used to connect with the manager (default: '')\n"
" -n Append a Newline to the output\n"
" -o Oneshot. Set the 'noRetryAttr' attribute\n"
" -t Change the request timeout to 'timeoutSeconds'\n"
"\n"
" ServiceKey: name of service to request (default: system.echo.0.raw)\n"
"\n"
" requestString: if present, place this in the requestData. If not present\n"
" read STDIN for the requestData.\n"
"\n"
"Examples:\n"
"\tplSend -C echo.sleepMS=500 system.echo.0.raw ''\t\t# Sleep for 500ms\n"
"\tcat listfolderrequest | plSend Mail.Folder..XML >results.data\n"
"\n"
"See also: " PACKAGE_URL "\n"
"\n";


//////////////////////////////////////////////////////////////////////
// Split out the key=value tokens from the context string and and them
// into the request.
//////////////////////////////////////////////////////////////////////

static bool
addContext(pluton::clientRequest& R, const char* context)
{
  char* dupe = strdup(context);
  char** dupeP = &dupe;
  const char* key = strsep(dupeP, "=");
  if (key == dupe) {
    cerr << "Error: Context string needs an '=' character" << endl;
    return false;
  }

  if (!R.setContext(key, string(*dupeP))) {
    cerr << "Error: plSend Fault: " << R.getFaultCode() << ":" << R.getFaultText() << endl;
    return false;
  }

  return true;
}

//////////////////////////////////////////////////////////////////////

int
main(int argc, char **argv)
{
  char optionChar;
  bool debugFlag = false;
  bool newlineFlag = false;
  bool elapseTimeFlag = false;
  bool oneShotFlag = false;
  const char* lookupMap = "";
  int timeoutSeconds = -1;

  pluton::clientRequest R;

  while ((optionChar = getopt(argc, argv, "hC:deL:not:")) != -1) {

    switch (optionChar) {

    case 'C':
      if (!addContext(R, optarg)) {
cerr << endl << usage;
exit(1);
      }
      break;

    case 'd': debugFlag = true; break;

    case 'e': elapseTimeFlag = true; break;

    case 'h':
      cout << usage;
      exit(0);

    case 'L': lookupMap = optarg; break;

    case 'n': newlineFlag = true; break;

    case 'o': oneShotFlag = true; break;

    case 't':
      timeoutSeconds = atoi(optarg);
      if (timeoutSeconds <= 0) {
cerr << "Error: timeout seconds must be a positive integer" << endl;
cerr << endl << usage;
exit(1);
      }
      break;

    default:
      cerr << endl << usage;
      exit(1);
    }
  }

  argc -= optind;
  argv += optind;

  if (argc < 1) {
    cerr << "Error: Must supply ServiceKey on the command line" << endl;
    cerr << endl << usage;
    exit(1);
  }
  char* serviceKey = *argv;
  argc--;
  argv++;

  if (argc > 1) {
    cerr << "Error: Too many arguments on the command line" << endl;
    cerr << endl << usage;
    exit(1);
  }

  // If a string is present, use that as the requestStr, otherwise
  // slurp in all of STDIN and use that.

  string requestStr;
  if (argc == 1) {
    requestStr.assign(*argv);
    argc--;
    argv++;
  }
  else {
    stringstream os;
    os << cin.rdbuf();
    requestStr = os.str();
  }

  pluton::client C("plSend");
  C.setDebug(debugFlag);
  if (timeoutSeconds > 0) C.setTimeoutMilliSeconds(timeoutSeconds * 1000);

  if (!C.initialize(lookupMap)) {
    cerr << C.getFault().getMessage("Error: plSend initialize: ", true) << endl;
    exit(1);
  }

  //////////////////////////////////////////////////////////////////////
  // Construct the rest of the request (context is done at getopt()
  //////////////////////////////////////////////////////////////////////

  if (oneShotFlag) R.setAttribute(pluton::noRetryAttr);
  R.setRequestData(requestStr.data(), requestStr.length());

  if (!C.addRequest(serviceKey, R)) {
    cerr << C.getFault().getMessage("Error: plSend addRequest: ", true) << endl;
    exit(1);
  }

  struct timeval startTime;
  gettimeofday(&startTime, 0);

  int res = C.executeAndWaitAll();

  struct timeval endTime;
  gettimeofday(&endTime, 0);

  if (res == 0) {
    cerr << "Error: plSend timed out" << endl;
    exit(1);
  }

  if (res < 0) {
    cerr << C.getFault().getMessage("Error: plSend executeAndWaitAll: ", true) << endl;
    exit(1);
  }

  if (R.hasFault()) {
    cerr << "Error: plSend Fault: " << R.getFaultCode() << ":" << R.getFaultText() << endl;
  }
  else {
    std::string r;
    R.getResponseData(r);
    cout << r;
    if (newlineFlag) cout << endl;
  }

  if (elapseTimeFlag) {
    timeval diff;
    util::timevalDiff(diff, endTime, startTime);
    cerr << "Elapsed Time: " << diff.tv_sec << "." << setfill('0') << setw(6) << diff.tv_usec << endl;
  }

  return(R.hasFault() ? 1 : 0);
}
Something went wrong with that request. Please try again.