<commit_before>/*
** Author(s):
**  - <NAME> <<EMAIL>>
**
** Copyright (C) 2010, 2012 Aldebaran Robotics
*/


#include <vector>
#include <iostream>
#include <sstream>

#include <boost/thread.hpp>
#include <boost/bind.hpp>

#include <qimessaging/transport.hpp>
#include <qimessaging/session.hpp>
#include <qimessaging/perf/dataperftimer.hpp>

#include <qimessaging/service_directory.hpp>
#include <qimessaging/gateway.hpp>

#include <qimessaging/transport/transport_server.hpp>
#include <qimessaging/transport/network_thread.hpp>
#include <qimessaging/transport/url.hpp>

static int gLoopCount = 10000;
static const int gThreadCount = 1;



class ServerEventPrivate : public qi::TransportServerInterface, public qi::TransportSocketInterface {
public:
  ServerEventPrivate()
  {
    _dp = new qi::perf::DataPerfTimer("Transport synchronous call");
    _msgRecv = 0;
    _sizeM = 0;
  }

  virtual void newConnection() {
    qi::TransportSocket *socket = _ts.nextPendingConnection();
    if (!socket)
      return;
    socket->setDelegate(this);
  }

  virtual void onReadyRead(qi::TransportSocket *client, const qi::Message &msg)
  {
//    qi::DataStream ds(msg.data());
//    qi::DataStream rs;

    if (_msgRecv == 0)
    {
      unsigned int numBytes = (unsigned int)pow(2.0f, (int)_sizeM);
      _dp->start(gLoopCount, numBytes);
      _msgRecv++;
    }
    else if (_msgRecv == gLoopCount)
    {
      _dp->stop(1);
      _msgRecv = 0;
      _sizeM++;
    }
    else
    {
      _msgRecv++;
    }

//    qi::Object *obj;

//    obj = _services[msg.service()];

//    obj->metaCall(msg.function(), "", ds, rs);

//    qi::Message retval;
//    retval.setType(qi::Message::Reply);
//    retval.setId(msg.id());
//    retval.setService(msg.service());
//    retval.setData(rs.str());
//    client->send(retval);
  };

  virtual void onWriteDone(qi::TransportSocket *client)
  {
  }

  virtual void onConnected(qi::TransportSocket *client)
  {
  }

  virtual void onDisconnected(qi::TransportSocket *client)
  {
  }

public:
  qi::perf::DataPerfTimer           *_dp;
  std::map<std::string, qi::Object*> _services;
  qi::TransportServer                _ts;
  std::string                        _url;
  qi::Session                       *_session;
  int                                _msgRecv;
  int                                _sizeM;
};

class ServerEvent
{
public:
  ServerEvent()
    : _p(new ServerEventPrivate())
  {
  }

  ~ServerEvent()
  {
    delete _p;
  }

  void listen(qi::Session *session, const std::string &url) {
    _p->_url = url;
    _p->_session = session;

    qi::Url urlo(_p->_url);

    _p->_ts.setDelegate(_p);
    _p->_ts.start(urlo.host(), urlo.port(), _p->_session->_nthd->getEventBase());
  }


  void registerService(const std::string &name, qi::Object *obj) {
    _p->_services[name] = obj;

    qi::Message msg;
    msg.setType(qi::Message::Call);
    msg.setService("qi.master");
    msg.setFunction("registerService");

    qi::DataStream d;
    d << name;
    d << _p->_url;
    msg.setData(d.str());

    _p->_session->tc->send(msg);
    _p->_session->tc->waitForId(msg.id());
    qi::Message ans;
    _p->_session->tc->read(msg.id(), &ans);
  };

  void stop() {
    }

  private:
    ServerEventPrivate *_p;
};


int main_client(std::string src)
{
  qi::Session session;
  session.connect("tcp://127.0.0.1:5555");
  session.waitForConnected();

  qi::TransportSocket *sock = session.serviceSocket("serviceTest");

  for (int i = 0; i < 12; ++i)
  {
    char character = 'c';
    unsigned int numBytes = (unsigned int)pow(2.0f, (int)i);
    std::string requeststr = std::string(numBytes, character);

    for (int j = 0; j < gLoopCount; ++j)
    {
      static int id = 1;
      id++;
      char c = 1;
      c++;
      if (c == 0)
        c++;
      requeststr[2] = c;

      std::cout << j << std::endl;

      qi::DataStream d;
      d << requeststr;

      qi::Message msg;
      msg.setType(qi::Message::Call);
      msg.setService("serviceTest");
      msg.setFunction("reply");
      msg.setData(d.str());
      sock->send(msg);
//      sock->waitForId(msg.id());
    }
  }
  return 0;
}

void start_client(int count)
{
  boost::thread thd[100];

  assert(count < 100);

  for (int i = 0; i < count; ++i)
  {
    std::stringstream ss;
    ss << "remote" << i;
    std::cout << "starting thread: " << ss.str() << std::endl;
    thd[i] = boost::thread(boost::bind(&main_client, ss.str()));
  }

  for (int i = 0; i < count; ++i)
    thd[i].join();
}


#include <qi/os.hpp>


int main_gateway()
{
  qi::Session       session;
  qi::Object        obj;
  qi::Gateway       gate;

  session.connect("tcp://127.0.0.1:5555");
  session.waitForConnected();

  gate.listen(&session, "tcp://127.0.0.1:12345");
  std::cout << "ready." << std::endl;

  while (1)
    qi::os::sleep(1);
}

#include <qimessaging/server.hpp>

std::string reply(const std::string &msg)
{
  return msg;
}

int main_server()
{
  qi::ServiceDirectory sd;
  sd.listen("tcp://127.0.0.1:5555");
  std::cout << "Service Directory ready." << std::endl;

  qi::Session       session;
  qi::Object        obj;
  ServerEvent       srv;
  obj.advertiseMethod("reply", &reply);

  session.connect("tcp://127.0.0.1:5555");
  session.waitForConnected();

  srv.listen(&session, "tcp://127.0.0.1:9559");
  srv.registerService("serviceTest", &obj);
  std::cout << "serviceTest ready." << std::endl;

  while (1)
    qi::os::sleep(1);

  srv.stop();
  session.disconnect();
}

int main(int argc, char **argv)
{
  if (argc > 1 && !strcmp(argv[1], "--client"))
  {
    int threadc = 1;
    if (argc > 2)
      threadc = atoi(argv[2]);
    start_client(threadc);
  }
  else if (argc > 1 && !strcmp(argv[1], "--server"))
  {
    return main_server();
  }
  else if (argc > 1 && !strcmp(argv[1], "--gateway"))
  {
    return main_gateway();
  }
  else
  {
    //start the server
    boost::thread threadServer1(boost::bind(&main_server));
//    boost::thread threadServer2(boost::bind(&main_gateway));
    sleep(1);
    start_client(gThreadCount);
  }
  return 0;
}
<commit_msg>fix compilation (headers moved)<commit_after>/*
** Author(s):
**  - <NAME> <<EMAIL>>
**
** Copyright (C) 2010, 2012 Aldebaran Robotics
*/


#include <vector>
#include <iostream>
#include <sstream>

#include <boost/thread.hpp>
#include <boost/bind.hpp>

#include <qimessaging/transport.hpp>
#include <qimessaging/session.hpp>
#include <qimessaging/perf/dataperftimer.hpp>

#include <qimessaging/service_directory.hpp>
#include <qimessaging/gateway.hpp>

#include <qimessaging/transport/transport_server.hpp>
#include "../src/transport/url.hpp"
#include "../src/transport/network_thread.hpp"

static int gLoopCount = 10000;
static const int gThreadCount = 1;



class ServerEventPrivate : public qi::TransportServerInterface, public qi::TransportSocketInterface {
public:
  ServerEventPrivate()
  {
    _dp = new qi::perf::DataPerfTimer("Transport synchronous call");
    _msgRecv = 0;
    _sizeM = 0;
  }

  virtual void newConnection() {
    qi::TransportSocket *socket = _ts.nextPendingConnection();
    if (!socket)
      return;
    socket->setDelegate(this);
  }

  virtual void onReadyRead(qi::TransportSocket *client, const qi::Message &msg)
  {
//    qi::DataStream ds(msg.data());
//    qi::DataStream rs;

    if (_msgRecv == 0)
    {
      unsigned int numBytes = (unsigned int)pow(2.0f, (int)_sizeM);
      _dp->start(gLoopCount, numBytes);
      _msgRecv++;
    }
    else if (_msgRecv == gLoopCount)
    {
      _dp->stop(1);
      _msgRecv = 0;
      _sizeM++;
    }
    else
    {
      _msgRecv++;
    }

//    qi::Object *obj;

//    obj = _services[msg.service()];

//    obj->metaCall(msg.function(), "", ds, rs);

//    qi::Message retval;
//    retval.setType(qi::Message::Reply);
//    retval.setId(msg.id());
//    retval.setService(msg.service());
//    retval.setData(rs.str());
//    client->send(retval);
  };

  virtual void onWriteDone(qi::TransportSocket *client)
  {
  }

  virtual void onConnected(qi::TransportSocket *client)
  {
  }

  virtual void onDisconnected(qi::TransportSocket *client)
  {
  }

public:
  qi::perf::DataPerfTimer           *_dp;
  std::map<std::string, qi::Object*> _services;
  qi::TransportServer                _ts;
  std::string                        _url;
  qi::Session                       *_session;
  int                                _msgRecv;
  int                                _sizeM;
};

class ServerEvent
{
public:
  ServerEvent()
    : _p(new ServerEventPrivate())
  {
  }

  ~ServerEvent()
  {
    delete _p;
  }

  void listen(qi::Session *session, const std::string &url) {
    _p->_url = url;
    _p->_session = session;

    qi::Url urlo(_p->_url);

    _p->_ts.setDelegate(_p);
    _p->_ts.start(urlo.host(), urlo.port(), _p->_session->_nthd->getEventBase());
  }


  void registerService(const std::string &name, qi::Object *obj) {
    _p->_services[name] = obj;

    qi::Message msg;
    msg.setType(qi::Message::Call);
    msg.setService("qi.master");
    msg.setFunction("registerService");

    qi::DataStream d;
    d << name;
    d << _p->_url;
    msg.setData(d.str());

    _p->_session->tc->send(msg);
    _p->_session->tc->waitForId(msg.id());
    qi::Message ans;
    _p->_session->tc->read(msg.id(), &ans);
  };

  void stop() {
    }

  private:
    ServerEventPrivate *_p;
};


int main_client(std::string src)
{
  qi::Session session;
  session.connect("tcp://127.0.0.1:5555");
  session.waitForConnected();

  qi::TransportSocket *sock = session.serviceSocket("serviceTest");

  for (int i = 0; i < 12; ++i)
  {
    char character = 'c';
    unsigned int numBytes = (unsigned int)pow(2.0f, (int)i);
    std::string requeststr = std::string(numBytes, character);

    for (int j = 0; j < gLoopCount; ++j)
    {
      static int id = 1;
      id++;
      char c = 1;
      c++;
      if (c == 0)
        c++;
      requeststr[2] = c;

      std::cout << j << std::endl;

      qi::DataStream d;
      d << requeststr;

      qi::Message msg;
      msg.setType(qi::Message::Call);
      msg.setService("serviceTest");
      msg.setFunction("reply");
      msg.setData(d.str());
      sock->send(msg);
//      sock->waitForId(msg.id());
    }
  }
  return 0;
}

void start_client(int count)
{
  boost::thread thd[100];

  assert(count < 100);

  for (int i = 0; i < count; ++i)
  {
    std::stringstream ss;
    ss << "remote" << i;
    std::cout << "starting thread: " << ss.str() << std::endl;
    thd[i] = boost::thread(boost::bind(&main_client, ss.str()));
  }

  for (int i = 0; i < count; ++i)
    thd[i].join();
}


#include <qi/os.hpp>


int main_gateway()
{
  qi::Session       session;
  qi::Object        obj;
  qi::Gateway       gate;

  session.connect("tcp://127.0.0.1:5555");
  session.waitForConnected();

  gate.listen(&session, "tcp://127.0.0.1:12345");
  std::cout << "ready." << std::endl;

  while (1)
    qi::os::sleep(1);
}

#include <qimessaging/server.hpp>

std::string reply(const std::string &msg)
{
  return msg;
}

int main_server()
{
  qi::ServiceDirectory sd;
  sd.listen("tcp://127.0.0.1:5555");
  std::cout << "Service Directory ready." << std::endl;

  qi::Session       session;
  qi::Object        obj;
  ServerEvent       srv;
  obj.advertiseMethod("reply", &reply);

  session.connect("tcp://127.0.0.1:5555");
  session.waitForConnected();

  srv.listen(&session, "tcp://127.0.0.1:9559");
  srv.registerService("serviceTest", &obj);
  std::cout << "serviceTest ready." << std::endl;

  while (1)
    qi::os::sleep(1);

  srv.stop();
  session.disconnect();
}

int main(int argc, char **argv)
{
  if (argc > 1 && !strcmp(argv[1], "--client"))
  {
    int threadc = 1;
    if (argc > 2)
      threadc = atoi(argv[2]);
    start_client(threadc);
  }
  else if (argc > 1 && !strcmp(argv[1], "--server"))
  {
    return main_server();
  }
  else if (argc > 1 && !strcmp(argv[1], "--gateway"))
  {
    return main_gateway();
  }
  else
  {
    //start the server
    boost::thread threadServer1(boost::bind(&main_server));
//    boost::thread threadServer2(boost::bind(&main_gateway));
    sleep(1);
    start_client(gThreadCount);
  }
  return 0;
}

{'fraction_non_alphanumeric': 0.17849497777045942, 'fraction_numerical': 0.01943026510785444, 'mean_word_length': 2.8808306709265175, 'pattern_counts': {'":': 0, '<': 113, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 12, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3104668', 'n_tokens_mistral': 4602, 'n_tokens_neox': 4316, 'n_words': 833}