Implement max-connect facility which makes the server sleep if a
[yazproxy-moved-to-github.git] / src / tstthreads.cpp
1 /* $Id: tstthreads.cpp,v 1.10 2006-03-30 10:32:16 adam Exp $
2    Copyright (c) 1998-2006, Index Data.
3
4 This file is part of the yazproxy.
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 <stdlib.h>
23 #include <ctype.h>
24
25 #include <yazpp/pdu-assoc.h>
26 #include <yazpp/socket-manager.h>
27 #include <yaz/log.h>
28 #include "msg-thread.h"
29
30 using namespace yazpp_1;
31
32 class My_Msg : public IMsg_Thread {
33 public:
34     IMsg_Thread *handle();
35     void result();
36     int m_val;
37 };
38
39 IMsg_Thread *My_Msg::handle()
40 {
41     My_Msg *res = new My_Msg;
42     int sl = rand() % 5;
43
44     res->m_val = m_val;
45     printf("My_Msg::handle val=%d sleep=%d\n", m_val, sl);
46     sleep(sl);
47     return res;
48 }
49
50 void My_Msg::result()
51 {
52     printf("My_Msg::result val=%d\n", m_val);
53 }
54
55 class My_Timer_Thread : public ISocketObserver {
56 private:
57     ISocketObservable *m_obs;
58     int m_fd[2];
59     Msg_Thread *m_t;
60 public:
61     My_Timer_Thread(ISocketObservable *obs, Msg_Thread *t);
62     void socketNotify(int event);
63 };
64
65 My_Timer_Thread::My_Timer_Thread(ISocketObservable *obs,
66                                  Msg_Thread *t) : m_obs(obs) 
67 {
68     pipe(m_fd);
69     m_t = t;
70     obs->addObserver(m_fd[0], this);
71     obs->maskObserver(this, SOCKET_OBSERVE_READ);
72     obs->timeoutObserver(this, 1);
73 }
74
75 void My_Timer_Thread::socketNotify(int event)
76 {
77     static int seq = 1;
78     printf("Add %d\n", seq);
79     My_Msg *m = new My_Msg;
80     m->m_val = seq++;
81     m_t->put(m);
82 }
83
84 int main(int argc, char **argv)
85 {
86     SocketManager mySocketManager;
87
88     Msg_Thread m(&mySocketManager, 3);
89     My_Timer_Thread t(&mySocketManager, &m) ;
90     int i = 0;
91     while (++i < 5 && mySocketManager.processEvent() > 0)
92         ;
93     return 0;
94 }
95 /*
96  * Local variables:
97  * c-basic-offset: 4
98  * indent-tabs-mode: nil
99  * End:
100  * vim: shiftwidth=4 tabstop=8 expandtab
101  */
102