Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1592 runge 1
#include <iostream>
2
 
3
#include <boost/bind.hpp>
4
 
5
#include "net/Manager.h"
6
#include "net/Types.h"
7
 
1593 runge 8
#include "timer/Manager.h"
9
#include "timer/Types.h"
10
 
11
 
1592 runge 12
using namespace atom;
1593 runge 13
 
1592 runge 14
 
15
net::Manager::Pointer NM = net::Manager::Instance();
16
 
1593 runge 17
net::ServerId server_id = 0;
18
timer::TimerId timer_id;
19
 
20
void Main_SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
1592 runge 21
{
22
    std::cout << "client:" << client_id << " - New state: " << client_state << std::endl;
23
}
24
 
1593 runge 25
void Main_SlotOnNewData(net::ClientId client_id, net::ServerId server_id, net::Buffer data)
1592 runge 26
{
27
    std::string s(data.data());
28
 
29
    std::cout << "client:" << client_id << " - New data: " << s << std::endl;
30
 
1593 runge 31
    net::Buffer out_data;
1592 runge 32
 
1593 runge 33
    strncpy(out_data.c_array(), data.data(), out_data.size());
1592 runge 34
 
35
    NM->SendTo(client_id, out_data);
36
}
37
 
1593 runge 38
void Main_SlotOnTimeout(timer::TimerId timer_id)
39
{
40
    std::cout << "timer:" << timer_id << " - expired" << std::endl;
41
 
42
    std::string out_string = "Timeout!";
43
 
44
    net::Buffer out_data;
45
 
46
    strncpy(out_data.c_array(), out_string.data(), out_data.size());
47
 
48
    NM->SendToAll(server_id, out_data);
49
}
50
 
1592 runge 51
int main(int argc, char **argv)
52
{
53
    std::cout << "connect slots" << std::endl;
54
 
1593 runge 55
    timer::Manager::Pointer TM = timer::Manager::Instance();
1592 runge 56
 
57
 
1593 runge 58
    //TM->Set(10000, true);
59
 
60
    TM->ConnectSlots(boost::bind(&Main_SlotOnTimeout, _1));
61
 
62
 
63
    std::cout << "connect slots2" << std::endl;
64
    NM->ConnectSlots(boost::bind(&Main_SlotOnNewState, _1, _2, _3), boost::bind(&Main_SlotOnNewData, _1, _2, _3));
65
 
66
 
1592 runge 67
    try
68
    {
1593 runge 69
        server_id = NM->StartServer(net::PROTOCOL_TCP, 2000);
70
 
71
        timer_id = TM->Set(10000, true);
72
 
1592 runge 73
        /*
74
        net::ClientId client_id_udp = NM->Connect(net::PROTOCOL_UDP, "192.168.1.250", 1100);
75
 
76
        std::cout << "connected" << std::endl;
77
 
78
        char c[2] = { 251, 0 };
79
        */
80
        //NM->SendTo(client_id_udp, std::string(c));
81
    }
82
    catch (std::exception e)
83
    {
84
        std::cerr << e.what() << std::endl;
85
    }
86
 
87
    std::cout << "sleep" << std::endl;
88
 
89
    sleep(100000000);
90
 
91
    return 0;
92
}