File: client.cpp

package info (click to toggle)
lcm 1.3.1%2Brepack1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,848 kB
  • sloc: ansic: 16,186; java: 6,843; cs: 2,266; cpp: 1,594; python: 989; makefile: 352; xml: 252; sh: 59
file content (150 lines) | stat: -rw-r--r-- 3,879 bytes parent folder | download | duplicates (5)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <gtest/gtest.h>

#include <lcm/lcm.h>

#include "common.hpp"

#define info(...) do { printf("cpp_client: "); printf(__VA_ARGS__); \
  printf("\n"); } while(0)

class EchoTest {
 public:
  EchoTest()
      : lcm_(),
        num_trials_(100),
        test_channel_("TEST_ECHO"),
        response_count_(0),
        echo_data_(NULL),
        echo_msg_len_(0),
        subscription_() {
  }

  bool Run(void) {
    int maxlen = 10000;
    int minlen = 10;
    echo_data_ = (uint8_t*) malloc(maxlen);
    subscription_ = lcm_.subscribe(test_channel_ + "_REPLY",
                                    &EchoTest::Handler, this);
    response_count_ = 0;

    for (int iter = 0; iter < num_trials_; iter++) {
      echo_msg_len_ = rand() % (maxlen - minlen) + minlen;
      for (int i = 0; i < echo_msg_len_; i++)
        echo_data_[i] = rand() % 256;

      lcm_.publish(test_channel_, echo_data_, echo_msg_len_);

      if (lcm_.handleTimeout(500) <= 0 || (response_count_ != iter + 1)) {
        info("echo test failed to receive response on iteration %d", iter);
        free(echo_data_);
        return false;
      }
    }

    lcm_.unsubscribe(subscription_);
    free(echo_data_);
    return true;
  }

 private:
  void Handler(const lcm::ReceiveBuffer *rbuf, const std::string& channel) {
    if (rbuf->data_size != echo_msg_len_)
      return;
    if (memcmp(rbuf->data, echo_data_, rbuf->data_size))
      return;
    response_count_++;
  }

  lcm::LCM lcm_;
  int num_trials_;
  int echo_msg_len_;
  uint8_t* echo_data_;
  int response_count_;
  lcm::Subscription* subscription_;
  std::string test_channel_;
};

TEST(LCM_CPP, Echo) {
  srand(time(NULL));

  EXPECT_TRUE(EchoTest().Run());
}

template<class LcmType>
class TypedTest {
 public:
  TypedTest(const std::string test_name, int num_trials)
      : lcm_(),
      test_channel_(test_name),
      response_count_(0),
      num_trials_(num_trials),
      subscription_() {
  }

  bool Run(void) {
    LcmType msg;
    lcm::Subscription* subscription = lcm_.subscribe(
        test_channel_ + "_reply", &TypedTest<LcmType>::Handler, this);
    bool result = true;
    for (int trial = 0; trial < num_trials_ && result; trial++) {
      FillLcmType(trial, &msg);
      lcm_.publish(test_channel_, &msg);
      if (lcm_.handleTimeout(500) <= 0) {
        info("%s test: Timeout waiting for reply", test_channel_.c_str());
        result = false;
        break;
      } else if (response_count_ != trial + 1) {
        info("%s test: failed on iteration %d", test_channel_.c_str(), trial);
        result = false;
        break;
      }
      ClearLcmType(&msg);
    }
    lcm_.unsubscribe(subscription);
    return result;
  }

 private:
  void Handler(const lcm::ReceiveBuffer* rbuf, const std::string& channel,
               const LcmType* msg) {
    if (CheckLcmType(msg, response_count_ + 1)) {
      response_count_++;
      return;
    }
  }

  lcm::LCM lcm_;
  int response_count_;
  int num_trials_;
  lcm::Subscription* subscription_;
  std::string test_channel_;
};

TEST(LCM_CPP, primitives_t) {
    EXPECT_TRUE(TypedTest<lcmtest::primitives_t>("test_lcmtest_primitives_t",
                                          1000).Run());
}

TEST(LCM_CPP, primitives_list_t) {
    EXPECT_TRUE(TypedTest<lcmtest::primitives_list_t>(
                "test_lcmtest_primitives_list_t", 100).Run());
}

TEST(LCM_CPP, node_t) {
    EXPECT_TRUE(TypedTest<lcmtest::node_t>("test_lcmtest_node_t",
                                          7).Run());
}

TEST(LCM_CPP, multidim_array_t) {
    EXPECT_TRUE(TypedTest<lcmtest::multidim_array_t>(
          "test_lcmtest_multidim_array_t", 5).Run());
}

TEST(LCM_CPP, cross_package_t) {
    EXPECT_TRUE(TypedTest<lcmtest2::cross_package_t>(
          "test_lcmtest2_cross_package_t", 100).Run());
}