Ejemplo n.º 1
0
void receive(int id)
{
    Channel::ptr_t channel;
    channel = Channel::Create("192.168.8.102");
    std::string e_name = "tieto-chatroom";

    //preparation
    channel->DeclareExchange(e_name, Channel::EXCHANGE_TYPE_FANOUT);
    std::string q_name = channel->DeclareQueue("", false, true, false);
    channel->BindQueue(q_name, e_name, "chat");

    channel->BasicConsume(q_name, "");
    Envelope::ptr_t env;
    while (true)
    {
        if (channel->BasicConsumeMessage(env, -1))
        {
            std::cout << "Received by " << id << ": " <<
                         env->Message()->Body() << " [" <<
                         env->Exchange() << "]" << std::endl;

        } else {
            std::cout << "queue empty" << std::endl;
            break;
        }
    }
}
Ejemplo n.º 2
0
set<uint> ServiceClient::receive(const set<uint>& ids, const uint& timeout) {
  using namespace chrono;
  auto end = high_resolution_clock::now() + milliseconds(timeout);

  set<uint> received;
  
  // check if we have not received the reply already
  for (auto& id : ids) {
    auto iterator = replies.find(id);
    if (iterator != replies.end()) {
      received.insert(id);    
    }
  }
  if (received.size() == ids.size()) {
    return received; // all packets arrived, return
  }

  while (1) {  
    Envelope::ptr_t envelope;
    BasicMessage::ptr_t message; 

    do {  
      auto now = high_resolution_clock::now();
      int delta = duration_cast<milliseconds>(end-now).count();
      if (delta <= 0) {
        return received; // we are out of time already, leave!
      }

      bool newmsg = channel->BasicConsumeMessage(envelope, delta);
      if (newmsg == false) {
        return received; // timeout reached and nothing was received
      }

      message = envelope->Message(); 

    } while (message->ContentType() != "application/msgpack" ||
             message->CorrelationIdIsSet() == false);
      
    uint id = stoi(message->CorrelationId());
    replies.emplace(id, message);
    
    auto iterator = ids.find(id);
    if (iterator != ids.end()) { 
      // we were waiting for this message
      received.insert(id);
      if (received.size() == ids.size()) {
        return received; // all packets arrived, return
      }
    }

  }
}
Ejemplo n.º 3
0
int main()
{
  const std::string EXCHANGE_NAME = "SimpleAmqpClientEnvelopeTest";
  const std::string ROUTING_KEY = "SACRoutingKey";
  const std::string CONSUMER_TAG = "SACConsumerTag";

  try
  {
    Channel::ptr_t channel = Channel::Create();

    channel->DeclareExchange(EXCHANGE_NAME, Channel::EXCHANGE_TYPE_FANOUT);
    std::string queue = channel->DeclareQueue("");
    channel->BindQueue(queue, EXCHANGE_NAME, ROUTING_KEY);

    channel->BasicPublish(EXCHANGE_NAME, ROUTING_KEY, BasicMessage::Create("MessageBody"));
    channel->BasicPublish(EXCHANGE_NAME, ROUTING_KEY, BasicMessage::Create("MessageBody2"));
    channel->BasicPublish(EXCHANGE_NAME, ROUTING_KEY, BasicMessage::Create("MessageBody3"));

    channel->BasicConsume(queue, CONSUMER_TAG);

    Envelope::ptr_t env;
    for (int i = 0; i < 3; ++i)
    {
      if (channel->BasicConsumeMessage(env, 0)) 
      {
        std::cout << "Envelope received: \n" 
          << " Exchange: " << env->Exchange()
          << "\n Routing key: " << env->RoutingKey() 
          << "\n Consumer tag: " << env->ConsumerTag()
          << "\n Delivery tag: " << env->DeliveryTag()
          << "\n Redelivered: " << env->Redelivered() 
          << "\n Body: " << env->Message()->Body() << std::endl;
      }
      else
      {
        std::cout << "Basic Consume failed.\n";
      }
    }
  }
  catch (AmqpResponseServerException& e)
  {
    std::cout << "Failure: " << e.what();
  }
  return 0;
}
Ejemplo n.º 4
0
TEST_F(connected_test, basic_message_empty_table_roundtrip) {
  std::string queue = channel->DeclareQueue("");
  std::string tag = channel->BasicConsume(queue, "");

  Table table_in;

  BasicMessage::ptr_t message_in = BasicMessage::Create("Body");
  message_in->HeaderTable(table_in);

  channel->BasicPublish("", queue, message_in);

  Envelope::ptr_t envelope = channel->BasicConsumeMessage(tag);
  BasicMessage::ptr_t message_out = envelope->Message();
  Table table_out = message_out->HeaderTable();

  EXPECT_EQ(table_in.size(), table_out.size());
  EXPECT_TRUE(std::equal(table_in.begin(), table_in.end(), table_out.begin()));
}
Ejemplo n.º 5
0
TEST_F(connected_test, basic_message_header_roundtrip) {
  Table table_in;
  table_in.insert(TableEntry("void_key", TableValue()));
  table_in.insert(TableEntry("bool_key", true));
  table_in.insert(TableEntry("int8_key", int8_t(8)));
  table_in.insert(TableEntry("int16_key", int16_t(16)));
  table_in.insert(TableEntry("int32_key", int32_t(32)));
  table_in.insert(TableEntry("int64_key", int64_t(64)));
  table_in.insert(TableEntry("float_key", float(1.5)));
  table_in.insert(TableEntry("double_key", double(2.25)));
  table_in.insert(TableEntry("string_key", "A string!"));

  std::vector<TableValue> array_in;
  array_in.push_back(TableValue(false));
  array_in.push_back(TableValue(int32_t(10)));
  array_in.push_back(TableValue(std::string("Another string")));

  table_in.insert(TableEntry("array_key", array_in));

  Table table_inner;
  table_inner.insert(TableEntry("inner_string", "An inner table"));
  table_inner.insert(TableEntry("inner array", array_in));

  table_in.insert(TableEntry("table_key", table_inner));

  std::string queue = channel->DeclareQueue("");
  std::string tag = channel->BasicConsume(queue, "");

  BasicMessage::ptr_t message_in = BasicMessage::Create("Body");
  message_in->HeaderTable(table_in);

  channel->BasicPublish("", queue, message_in);

  Envelope::ptr_t envelope = channel->BasicConsumeMessage(tag);
  BasicMessage::ptr_t message_out = envelope->Message();
  Table table_out = message_out->HeaderTable();

  EXPECT_EQ(table_in.size(), table_out.size());
  EXPECT_TRUE(std::equal(table_in.begin(), table_in.end(), table_out.begin()));
}