summaryrefslogtreecommitdiff
path: root/cesar/maximus/usertest/src/main_example.cpp
blob: 90e2ba570e61b1c6c7bd789a139271fb9931748c (plain)
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
163
164
165
166
#include "Maximus.h"
#include "Sta.h"
#include "Msg.h"

#include <signal.h> // for 'raise()' and 'SIGTERM'
#include <iostream> // for 'cout', 'cerr' and 'clog'
using namespace std;

typedef struct my_struct
{
    int i1;
    int i2;
    bool b;
} my_struct_t;

typedef long long my_type_t;

bool receive_fc1_rsp;

void
my_cb (Msg & msg)
{
    cout << __PRETTY_FUNCTION__ << endl;
    
    unsigned long length = sizeof(my_type_t);
    my_type_t result2;
    my_type_t * p_result2 = &result2;
    if (NULL != msg.bind_param("result_2", length, (unsigned char *)p_result2))
    {
        cout << "result 2 = " << result2 << endl;
    }
    receive_fc1_rsp = true;
   
    p_result2 = NULL;
}

int
main ( int argc, char * argv[] )
{
    try
    {
        /* Instantiate a Maximus object and initialize it. */
        Maximus maximus;
        maximus.init(argc, argv);

        /* Create three stations. */
        Sta stationA = maximus.create_sta();
        Sta stationB = maximus.create_sta();
        Sta stationC = maximus.create_sta();

        /* Launch a debugger attached to station B. */
        stationB.debug();

        /* Send an asynchronous function message to station A. */

        /** Create a function message and set the name of the function to call of station A. **/
        Msg fc1 = maximus.create_fc("function_1");

        /** Add  a first parameter to the created message. **/
        my_struct_t param1 = { 1, 2, true };
        fc1.add_param("param_1", sizeof(my_struct_t), (unsigned char *)&param1);

        /** Add a second parameter to the created message. **/
        int param2[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        fc1.add_param("param_2", 10*sizeof(int), (unsigned char *)param2);

        /** Add a third parameter to the created message. **/
        string param3("hello");
        fc1.add_param("param_3", param3);

        /** Add a fourth parameter to the created message. **/
        my_type_t param4 = 123;
        fc1.add_param("param_4", sizeof(my_type_t), (unsigned char *)&param4);

        /** Register a callback function which will be called at message response reception. **/
        fc1.set_cb(&my_cb);

        /** Set destination station of message 1. **/
        fc1.set_sta(stationA);

        /** Send the configured message in an asynchronous mode. **/
        receive_fc1_rsp = false;
        fc1.send_async();

        /* Send a synchronous function message to station B. */

        /** Create a function message and set the name of the function to call into station B. **/
        Msg fc2 = maximus.create_fc("function_2");

        /** Add a first parameter to the created message. **/
        fc2.add_param("param_5", true);

        /** Send the configured message to station B in a synchronous mode. **/
        fc2.send(stationB);

        /** Get the function result. **/
        char result1[FUNCTION_CALL_PARAM_MAX_SIZE];
        unsigned long length = FUNCTION_CALL_PARAM_MAX_SIZE*sizeof(char);
        fc2.bind_param("result_1", length, (unsigned char *)result1);
        cout << "result1 = " << result1 << endl;

        /* Set a parameter value of station B. */
        Msg probe1 = maximus.create_probe();
        unsigned int param6 = 789;
        probe1.add_param("param_6", param6);
        probe1.send(stationB);

        /* Get a parameter value from station B. */
        Msg probe2 = maximus.create_probe()
                            .add_param("param_7")
                            .send(stationB);
        unsigned int param7 = probe2.bind_param<unsigned int>("param_7");
        cout << "param 7 = " << param7 << endl;

        /* Get the same parameter value from station C, re-using existing message. */
        probe2.send(stationC);

        /* Send asynchronous function message(s) to station B. */
        if (456 == param7)
        {
            Msg fc3 = maximus.create_fc("function_3");
            fc3.send_async(stationC);
        }
        Msg fc4 = maximus.create_fc("function_4");
        fc4.send_async(stationC);

        /* Wait for station A response. */
        while(!receive_fc1_rsp)
        {
            maximus.process();
        }

        /* Send an asynchronous function message to station C. */
        Msg fc5 = maximus.create_fc("function_5");
        fc5.send_async(stationC);

        /* Wait for responses to all sent messages in asynchronous mode. */
        maximus.wait();

        /* Get list of all registered parameters of stations C. */
        Msg probe3 = maximus.create_probe();
        probe3.send(stationC);
        if (probe3.is_param("param_8"))
        {
            /* Get parameter 8 value and set parameter 9 value of station C. */
            Msg probe4 = maximus.create_probe();
            probe4.add_param("param_8");
            probe4.add_param("param_9", false);
            probe4.send(stationC);
            bool param8 = probe4.bind_param<bool>("param_8");
            cout << "param 8 = " << param8 << endl;
        }

        /* Wait during 10000 ticks before terminating the program. */
        maximus.wait(10000);
    }
    catch (...)
    {
        cerr << "Catch an exception" << endl;
        raise(SIGTERM);
    }

    cout << endl << "*** END ***" << endl;
    return 0;
}