simple_append.cpp 3 KB
Newer Older
1
// -*- compile-command: "g++ -g -std=c++1y simple_append.cpp -o simple_append -lstdc++ -lztsdb_client -lboost_system" -*-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

// Copyright (C) 2016 Leonardo Silvestri
//
// This file is part of ztsdb.
//
// ztsdb is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ztsdb is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ztsdb.  If not, see <http://www.gnu.org/licenses/>.


#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
24 25
#include <cstring>
#include <cstdlib>
26 27 28 29 30
#include <string>
#include <iostream>
#include <limits>
#include <vector>
#include <system_error>
31
#include <unistd.h>
32
#include "ztsdb/zcpp_stdlib.hpp"
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


static void simple_append(const std::string ip, 
                          int port,
                          const std::string varname, 
                          size_t ncols)
{
  // open TCP connection:
  sockaddr_in addr;
  bzero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr=inet_addr(ip.c_str());
  addr.sin_port=htons(port);
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1) {
    throw std::system_error(std::error_code(errno, std::system_category()), "socket");
  }
  int cres = connect(fd, (sockaddr *) &addr, sizeof(struct sockaddr_in));
  if (cres == -1) {
    close(fd);
    throw std::system_error(std::error_code(errno, std::system_category()), "connect");
  }

  const auto now = std::chrono::system_clock::now();
        
  // create and populate a data vector:
  std::vector<double> data(ncols);
  for (unsigned j=0; j<ncols; ++j) {
    data[j] = j;   // set at position j the value j
  }
        
  // create the append message:
65 66 67
  auto msg = arr::make_append_msg(std::vector<std::string>{varname},
                                  std::vector<Global::dtime>{now},
                                  data);
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

  // send it:
  ssize_t wres = write(fd, msg.first.get(), msg.second);
  if (wres < 0) {
    throw std::system_error(std::error_code(errno, std::system_category()), "write");
  }

  if (close(fd) < 0) {
    throw std::system_error(std::error_code(errno, std::system_category()), "close");
  }
}


// params are
// 1. IP
// 2. port
// 3. name of variable to append to (assumed to be a zts)
// 4. number of columns to append
int main(int argc, char* argv[]) {
  enum { IP=1, PORT, VARNAME, NCOLS };

  // grab a message rate (# per second)
  if (argc != 5) {
    std::cerr << "usage: " << argv[0] << " <ip> <port> <varname> <ncols>" << std::endl;
    return -1;
  }
  
  int port = std::stoi(argv[PORT]);
  size_t ncols = std::stoull(argv[NCOLS]);

  simple_append(argv[IP], port, argv[VARNAME], ncols);

  return 0;
}