File: dev_intr_event.cpp

package info (click to toggle)
tango 10.0.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 89,936 kB
  • sloc: cpp: 201,786; sh: 1,645; python: 953; java: 800; perl: 467; javascript: 447; xml: 325; makefile: 272; sql: 72; ruby: 24
file content (215 lines) | stat: -rw-r--r-- 5,556 bytes parent folder | download | duplicates (3)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// NOLINTBEGIN(*)

#include "old_common.h"

class EventCallBack : public Tango::CallBack
{
    void push_event(Tango::DevIntrChangeEventData *);

  public:
    int cb_executed;
    int cb_err;
    size_t nb_cmd;
    size_t nb_att;
    bool dev_start;
};

void EventCallBack::push_event(Tango::DevIntrChangeEventData *event_data)
{
    cb_executed++;

    try
    {
        TEST_LOG << "EventCallBack::push_event(): called device " << event_data->device_name << " event "
                 << event_data->event << "\n";
        if(!event_data->err)
        {
            TEST_LOG << "Received device interface change event for device " << event_data->device_name << std::endl;
            nb_cmd = event_data->cmd_list.size();
            nb_att = event_data->att_list.size();
            dev_start = event_data->dev_started;
        }
        else
        {
            TEST_LOG << "Error send to callback" << std::endl;
            Tango::Except::print_error_stack(event_data->errors);
            cb_err++;
        }
    }
    catch(...)
    {
        TEST_LOG << "EventCallBack::push_event(): could not extract data !\n";
    }
}

int main(int argc, char **argv)
{
    DeviceProxy *device;

    if(argc == 1)
    {
        TEST_LOG << "usage: %s device" << std::endl;
        exit(-1);
    }

    std::string device_name = argv[1];

    try
    {
        device = new DeviceProxy(device_name);
    }
    catch(CORBA::Exception &e)
    {
        Except::print_exception(e);
        exit(1);
    }

    TEST_LOG << std::endl << "new DeviceProxy(" << device->name() << ") returned" << std::endl << std::endl;

    try
    {
        EventCallBack cb;
        cb.cb_executed = 0;
        cb.cb_err = 0;

        //
        // subscribe to a interface change event
        //

        int eve_id1 = device->subscribe_event(Tango::INTERFACE_CHANGE_EVENT, &cb);

        //
        // The callback should have been executed once
        //

        assert(cb.cb_executed == 1);
        assert(cb.cb_err == 0);
        assert(cb.dev_start == true);

        size_t old_cmd_nb = cb.nb_cmd;
        size_t old_att_nb = cb.nb_att;
        cb.dev_start = false;

        TEST_LOG << "   subscribe_event --> OK" << std::endl;

        //
        // Add a dynamic command -> Should generate a device interface change event
        //

        Tango::DevLong in = 0;
        DeviceData d_in;
        d_in << in;
        device->command_inout("IOAddCommand", d_in);

        std::this_thread::sleep_for(std::chrono::seconds(1));

        assert(cb.cb_executed == 2);
        assert(cb.cb_err == 0);
        assert(cb.nb_cmd == old_cmd_nb + 1);
        assert(cb.nb_att == old_att_nb);

        //
        // Remove the dynamic command -> Should generate a device interface change event
        //

        device->command_inout("IORemoveCommand", d_in);

        std::this_thread::sleep_for(std::chrono::seconds(1));

        assert(cb.cb_executed == 3);
        assert(cb.cb_err == 0);
        assert(cb.nb_cmd == old_cmd_nb);
        assert(cb.nb_att == old_att_nb);

        //
        // Add dynamic commands in loop -> 1 event
        //

        in = 2;
        d_in << in;
        device->command_inout("IOAddCommand", d_in);

        std::this_thread::sleep_for(std::chrono::seconds(1));

        assert(cb.cb_executed == 4);
        assert(cb.cb_err == 0);
        assert(cb.nb_cmd == old_cmd_nb + 3);
        assert(cb.nb_att == old_att_nb);

        TEST_LOG << "   Dev. interface change event after add/remove dynamic command --> OK" << std::endl;

        //
        // Init and DevRestart command without any dev. interface change -> No event
        //

        device->command_inout("Init");
        std::this_thread::sleep_for(std::chrono::seconds(1));

        assert(cb.cb_executed == 4);
        assert(cb.cb_err == 0);
        std::string adm_name = device->adm_name();
        DeviceProxy adm(adm_name);

        Tango::DeviceData d_in_restart;
        d_in_restart << device_name;
        adm.command_inout("DevRestart", d_in_restart);
        std::this_thread::sleep_for(std::chrono::seconds(1));

        assert(cb.cb_executed == 5);
        assert(cb.cb_err == 0);
        assert(cb.nb_cmd == old_cmd_nb);
        assert(cb.nb_att == old_att_nb);

        TEST_LOG << "   Dev. interface change event after Init or DevRestart command --> OK" << std::endl;

        //
        // Restart server cmd -> event generated
        //

        in = 0;
        d_in << in;
        device->command_inout("IOAddCommand", d_in);

        std::this_thread::sleep_for(std::chrono::seconds(1));

        assert(cb.cb_executed == 6);
        assert(cb.cb_err == 0);
        assert(cb.nb_cmd == old_cmd_nb + 1);
        assert(cb.nb_att == old_att_nb);

        adm.command_inout("RestartServer");
        std::this_thread::sleep_for(std::chrono::seconds(5));

        assert(cb.cb_executed == 7);
        assert(cb.cb_err == 0);
        assert(cb.nb_cmd == old_cmd_nb);
        assert(cb.nb_att == old_att_nb);
        assert(cb.dev_start == false);

        TEST_LOG << "   Event sent after RestartServer command --> OK" << std::endl;

        //
        // unsubscribe to the event
        //

        device->unsubscribe_event(eve_id1);

        TEST_LOG << "   unsubscribe_event --> OK" << std::endl;
    }
    catch(Tango::DevFailed &e)
    {
        Except::print_exception(e);
        exit(-1);
    }
    catch(CORBA::Exception &ex)
    {
        Except::print_exception(ex);
        exit(-1);
    }

    delete device;

    return 0;
}

// NOLINTEND(*)