1 // Copyright Maciej Sobczak 2008-2019.
2 // This file is part of YAMI4.
3 //
4 // YAMI4 is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // YAMI4 is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with YAMI4.  If not, see <http://www.gnu.org/licenses/>.
16 
17 #include <yami4-cpp/yami.h>
18 
19 #include <cstdlib>
20 #include <iostream>
21 
22 #include "../common_utils/string_to_int.h"
23 
main(int argc,char * argv[])24 int main(int argc, char * argv[])
25 {
26     if (argc != 2 && argc != 5)
27     {
28         std::cout << "need 1 or 4 parameters:\n"
29             "   - server address\n"
30             "   - outgoing high water mark\n"
31             "   - outgoing low water mark\n"
32             "   - number of iterations\n"
33             "If only server address is given,"
34             " the limits will have default values"
35             " and the loop will be infinite\n";
36 
37         return EXIT_FAILURE;
38     }
39 
40     const std::string server_address = argv[1];
41     int num_of_iterations = -1;
42 
43     yami::parameters options;
44     if (argc == 5)
45     {
46         int outgoing_high_water_mark;
47         int outgoing_low_water_mark;
48 
49         if (examples::string_to_int(argv[2],
50                 outgoing_high_water_mark) == false ||
51             examples::string_to_int(argv[3],
52                 outgoing_low_water_mark) == false ||
53             examples::string_to_int(argv[4],
54                 num_of_iterations) == false)
55         {
56             std::cout << "invalid arguments\n";
57             return EXIT_FAILURE;
58         }
59 
60         options.set_integer("outgoing_high_water_mark",
61             outgoing_high_water_mark);
62         options.set_integer("outgoing_low_water_mark",
63             outgoing_low_water_mark);
64     }
65 
66     try
67     {
68         yami::agent client_agent(options);
69 
70         yami::parameters params;
71 
72         int index = 1;
73         while (true)
74         {
75             params.set_integer("index", index);
76 
77             client_agent.send_one_way(server_address,
78                 "object", "message", params);
79 
80             std::cout
81                 << "posted message " << index << std::endl;
82 
83             if (num_of_iterations > 0)
84             {
85                 if (index == num_of_iterations)
86                 {
87                     break;
88                 }
89             }
90 
91             ++index;
92         }
93     }
94     catch (const std::exception & e)
95     {
96         std::cout << "error: " << e.what() << std::endl;
97     }
98 }
99