Include config.hpp in all .cpp files
[metaproxy-moved-to-github.git] / src / test_thread_pool_observer.cpp
1 /* $Id: test_thread_pool_observer.cpp,v 1.2 2005-10-08 23:29:32 adam Exp $
2    Copyright (c) 1998-2005, Index Data.
3
4 This file is part of the yaz-proxy.
5
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include "config.hpp"
23 #include <stdlib.h>
24 #include <ctype.h>
25
26 #include <yaz++/pdu-assoc.h>
27 #include <yaz++/socket-manager.h>
28 #include <yaz/log.h>
29 #include "thread_pool_observer.h"
30
31 using namespace yazpp_1;
32
33 class My_Msg : public IThreadPoolMsg {
34 public:
35     IThreadPoolMsg *handle();
36     void result();
37     int m_val;
38 };
39
40 IThreadPoolMsg *My_Msg::handle()
41 {
42     My_Msg *res = new My_Msg;
43     int sl = rand() % 5;
44
45     res->m_val = m_val;
46     printf("My_Msg::handle val=%d sleep=%d\n", m_val, sl);
47     sleep(sl);
48     return res;
49 }
50
51 void My_Msg::result()
52 {
53     printf("My_Msg::result val=%d\n", m_val);
54 }
55
56 class My_Timer_Thread : public ISocketObserver {
57 private:
58     ISocketObservable *m_obs;
59     int m_fd[2];
60     ThreadPoolSocketObserver *m_t;
61 public:
62     My_Timer_Thread(ISocketObservable *obs, ThreadPoolSocketObserver *t);
63     void socketNotify(int event);
64 };
65
66 My_Timer_Thread::My_Timer_Thread(ISocketObservable *obs,
67                                  ThreadPoolSocketObserver *t) : m_obs(obs) 
68 {
69     pipe(m_fd);
70     m_t = t;
71     obs->addObserver(m_fd[0], this);
72     obs->maskObserver(this, SOCKET_OBSERVE_READ);
73     obs->timeoutObserver(this, 1);
74 }
75
76 void My_Timer_Thread::socketNotify(int event)
77 {
78     static int seq = 1;
79     printf("Add %d\n", seq);
80     My_Msg *m = new My_Msg;
81     m->m_val = seq++;
82     m_t->put(m);
83 }
84
85 int main(int argc, char **argv)
86 {
87     SocketManager mySocketManager;
88
89     ThreadPoolSocketObserver m(&mySocketManager, 3);
90     My_Timer_Thread t(&mySocketManager, &m) ;
91     int i = 0;
92     while (++i < 5 && mySocketManager.processEvent() > 0)
93         ;
94     return 0;
95 }
96 /*
97  * Local variables:
98  * c-basic-offset: 4
99  * indent-tabs-mode: nil
100  * End:
101  * vim: shiftwidth=4 tabstop=8 expandtab
102  */
103