File: TypeLookupService_main.cpp

package info (click to toggle)
fastdds 3.3.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 60,540 kB
  • sloc: cpp: 793,735; xml: 15,283; python: 5,902; sh: 219; makefile: 95; ansic: 12
file content (162 lines) | stat: -rw-r--r-- 5,459 bytes parent folder | download
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
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @file TypeLookupService_main.cpp
 *
 */

#include "TypeLookupServicePublisher.h"
#include "TypeLookupServiceSubscriber.h"

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

struct CommandLineArgs
{
    int kind;
    int samples;
    int timeout;
    int expected_matches;
    std::vector<std::string> known_types;
    uint32_t seed {10800};
    uint32_t builtin_flow_controller_bytes {0};
};

CommandLineArgs parse_args(
        int argc,
        char** argv)
{
    CommandLineArgs args = {0, 0, 0, 0, {}};

    for (int i = 1; i < argc; ++i)
    {
        std::string arg = argv[i];
        std::stringstream ss(arg);

        std::string key, value;
        std::getline(ss, key, '=');
        std::getline(ss, value, '=');

        if (key == "kind")
        {
            if (value == "publisher")
            {
                args.kind = 1;
            }
            else if (value == "subscriber")
            {
                args.kind = 2;
            }
        }
        else if (key == "samples")
        {
            args.samples = std::stoi(value);
        }
        else if (key == "timeout")
        {
            args.timeout = std::stoi(value);
        }
        else if (key == "expected_matches")
        {
            args.expected_matches = std::stoi(value);
        }
        else if (key == "known_types")
        {
            std::replace(value.begin(), value.end(), ',', ' ');
            std::stringstream types_ss(value);
            std::string type;
            while (types_ss >> type)
            {
                args.known_types.push_back(type);
            }
        }
        else if (key == "seed")
        {
            args.seed = strtol(value.c_str(), nullptr, 10);
        }
        else if (key == "builtin_flow_controller_bytes")
        {
            args.builtin_flow_controller_bytes = strtol(value.c_str(), nullptr, 10);
        }
    }

    return args;
}

int main(
        int argc,
        char** argv)
{
    // Print all command-line arguments
    // std::cout << "Command-line arguments:" << std::endl;
    // for (int i = 0; i < argc; ++i)
    // {
    //     std::cout << "argv[" << i << "]: " << argv[i] << std::endl;
    // }

    CommandLineArgs args = parse_args(argc, argv);

    try
    {
        switch (args.kind){
            case 1: {
                eprosima::fastdds::dds::TypeLookupServicePublisher pub;
                // This case checks that the TypeLookUpService is controller by builtin flow controller, so it will
                // not be able to send the type object because the builtin flow controller is too small, thus there will
                // be no data writer to discover.
                if (args.builtin_flow_controller_bytes > 0 && args.builtin_flow_controller_bytes <= 3000)
                {
                    return (pub.init(args.seed % 230, args.known_types, args.builtin_flow_controller_bytes) &&
                           !pub.wait_discovery(args.expected_matches, args.timeout)) ? 0 : -1;
                }
                else
                {
                    return (pub.init(args.seed % 230, args.known_types, args.builtin_flow_controller_bytes) &&
                           pub.wait_discovery(args.expected_matches, args.timeout) &&
                           pub.run(args.samples, args.timeout) &&
                           pub.wait_discovery(0, args.timeout)) ? 0 : -1;
                }
            }
            case 2: {
                eprosima::fastdds::dds::TypeLookupServiceSubscriber sub;
                if (args.builtin_flow_controller_bytes > 0 && args.builtin_flow_controller_bytes <= 3000)
                {
                    return (sub.init(args.seed % 230, args.known_types, 0) &&
                           sub.wait_participant_discovery(args.expected_matches, args.timeout) &&
                           // Once asserted that the participant is discovered, we assert that no type is discovered thus
                           // not creating a data reader to match with the data writer.
                           !sub.wait_discovery(args.expected_matches, args.timeout)) ? 0 : -1;
                }
                else
                {
                    return (sub.init(args.seed % 230, args.known_types, args.builtin_flow_controller_bytes) &&
                           sub.wait_discovery(args.expected_matches, args.timeout) &&
                           sub.run(args.samples, args.timeout)) ? 0 : -1;
                }
            }
            default:
                std::cout << "Invalid participant type. Use 'publisher' or 'subscriber'." << std::endl;
                return -1;
        }
    }
    catch (std::exception const& e)
    {
        std::cout << "Tests failed: " << e.what() << std::endl;
        return -1;
    }
    return 0;
}