summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/tests/DispatcherTest.cpp
blob: c2f6bca12a36b2a016e66e0efbc374bef58ef232 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 *
 */

#include "qpid/sys/Poller.h"
#include "qpid/sys/IOHandle.h"
#include "qpid/sys/Dispatcher.h"
#include "qpid/sys/DispatchHandle.h"
#include "qpid/sys/posix/PrivatePosix.h"
#include "qpid/sys/Thread.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>

#include <iostream>
#include <boost/bind.hpp>

using namespace std;
using namespace qpid::sys;

int writeALot(int fd, const string& s) {
    int bytesWritten = 0;
    do {
        errno = 0;
        int lastWrite = ::write(fd, s.c_str(), s.size());
        if ( lastWrite >= 0) {
            bytesWritten += lastWrite;
        } 
    } while (errno != EAGAIN);
    return bytesWritten;
}

int readALot(int fd) {
    int bytesRead = 0;
    char buf[10240];
    
    do {
        errno = 0;
        int lastRead = ::read(fd, buf, sizeof(buf));
        if ( lastRead >= 0) {
            bytesRead += lastRead;
        } 
    } while (errno != EAGAIN);
    return bytesRead;
}

int64_t writtenBytes = 0;
int64_t readBytes = 0;

void writer(DispatchHandle& h, int fd, const string& s) {
    writtenBytes += writeALot(fd, s);
    h.rewatch();
}

void reader(DispatchHandle& h, int fd) {
    readBytes += readALot(fd);
    h.rewatch();
}

DispatchHandle* rh = 0;
DispatchHandle* wh = 0;

void rInterrupt(DispatchHandle&) {
	cerr << "R";
}

void wInterrupt(DispatchHandle&) {
	cerr << "W";	
}

DispatchHandle::Callback rcb = rInterrupt;
DispatchHandle::Callback wcb = wInterrupt;

void timer_handler(int /*signo*/, siginfo_t* /*info*/, void* /*context*/) {
	rh->call(rcb);
	wh->call(wcb);
}

int main(int /*argc*/, char** /*argv*/)
{
    // Create poller
    Poller::shared_ptr poller(new Poller);
    
    // Create dispatcher thread
    Dispatcher d(poller);
    Dispatcher d1(poller);
    Dispatcher d2(poller);
    Dispatcher d3(poller);
    Thread dt(d);
    Thread dt1(d1);
    Thread dt2(d2);
    Thread dt3(d3);

    // Setup sender and receiver
    int sv[2];
    int rc = ::socketpair(AF_LOCAL, SOCK_STREAM, 0, sv);
    assert(rc >= 0);
    
    // Set non-blocking
    rc = ::fcntl(sv[0], F_SETFL, O_NONBLOCK);
    assert(rc >= 0);

    rc = ::fcntl(sv[1], F_SETFL, O_NONBLOCK);
    assert(rc >= 0);
    
    // Make up a large string
    string testString = "This is only a test ... 1,2,3,4,5,6,7,8,9,10;";
    for (int i = 0; i < 8; i++)
        testString += testString;

    PosixIOHandle f0(sv[0]);
    PosixIOHandle f1(sv[1]);

    rh = new DispatchHandle(f0, boost::bind(reader, _1, sv[0]), 0, 0);
    wh = new DispatchHandle(f1, 0, boost::bind(writer, _1, sv[1], testString), 0);    

    rh->startWatch(poller);
    wh->startWatch(poller);

    // Set up a regular itimer interupt
    
    // Ignore signal in this thread
    ::sigset_t sm;
    ::sigemptyset(&sm);
    ::sigaddset(&sm, SIGRTMIN);
    ::pthread_sigmask(SIG_BLOCK, &sm, 0);
    
    // Signal handling
    struct ::sigaction sa;
    sa.sa_sigaction = timer_handler;
    sa.sa_flags = SA_RESTART | SA_SIGINFO;
    ::sigemptyset(&sa.sa_mask);
    rc = ::sigaction(SIGRTMIN, &sa,0);
    assert(rc == 0);
    
    ::sigevent se;
    se.sigev_notify = SIGEV_SIGNAL;
    se.sigev_signo = SIGRTMIN;
    se.sigev_value.sival_ptr = 0;
    timer_t timer;
    rc = ::timer_create(CLOCK_REALTIME, &se, &timer);
    assert(rc == 0);
    
    itimerspec ts = {
    /*.it_value = */ {2, 0},  // s, ns
    /*.it_interval = */ {2, 0}}; // s, ns
    
    rc = ::timer_settime(timer, 0, &ts, 0);
    assert(rc == 0);

    // wait 2 minutes then shutdown
    ::sleep(60);

    rc = ::timer_delete(timer);
    assert(rc == 0);
    poller->shutdown();
    dt.join();
    dt1.join();
    dt2.join();
    dt3.join();

    cout << "\nWrote: " << writtenBytes << "\n";
    cout << "Read: " << readBytes << "\n";
    
    return 0;
}