Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/yazproxy
[yazproxy-moved-to-github.git] / src / tstthreads.cpp
1 /* This file is part of YAZ proxy
2    Copyright (C) 1998-2009 Index Data
3
4 YAZ proxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include <stdlib.h>
20 #include <ctype.h>
21
22 #include <yazpp/pdu-assoc.h>
23 #include <yazpp/socket-manager.h>
24 #include <yaz/log.h>
25 #include "msg-thread.h"
26
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 using namespace yazpp_1;
32
33 class My_Msg : public IMsg_Thread {
34 public:
35     IMsg_Thread *handle();
36     void result();
37     int m_val;
38 };
39
40 IMsg_Thread *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 #if HAVE_UNISTD_H
48     sleep(sl);
49 #endif
50     return res;
51 }
52
53 void My_Msg::result()
54 {
55     printf("My_Msg::result val=%d\n", m_val);
56 }
57
58 class My_Timer_Thread : public ISocketObserver {
59 private:
60     ISocketObservable *m_obs;
61     int m_fd[2];
62     Msg_Thread *m_t;
63 public:
64     My_Timer_Thread(ISocketObservable *obs, Msg_Thread *t);
65     void socketNotify(int event);
66 };
67
68 My_Timer_Thread::My_Timer_Thread(ISocketObservable *obs,
69                                  Msg_Thread *t) : m_obs(obs) 
70 {
71     pipe(m_fd);
72     m_t = t;
73     obs->addObserver(m_fd[0], this);
74     obs->maskObserver(this, SOCKET_OBSERVE_READ);
75     obs->timeoutObserver(this, 1);
76 }
77
78 void My_Timer_Thread::socketNotify(int event)
79 {
80     static int seq = 1;
81     printf("Add %d\n", seq);
82     My_Msg *m = new My_Msg;
83     m->m_val = seq++;
84     m_t->put(m);
85 }
86
87 int main(int argc, char **argv)
88 {
89     SocketManager mySocketManager;
90
91     Msg_Thread m(&mySocketManager, 3);
92     My_Timer_Thread t(&mySocketManager, &m) ;
93     int i = 0;
94     while (++i < 5 && mySocketManager.processEvent() > 0)
95         ;
96     return 0;
97 }
98 /*
99  * Local variables:
100  * c-basic-offset: 4
101  * c-file-style: "Stroustrup"
102  * indent-tabs-mode: nil
103  * End:
104  * vim: shiftwidth=4 tabstop=8 expandtab
105  */
106