Bug fix: Handle the case where an APDU can be decoded but not encoded.
[yazproxy-moved-to-github.git] / src / tstthreads.cpp
1 /* $Id: tstthreads.cpp,v 1.11 2006-03-30 14:19:19 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 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 using namespace yazpp_1;
35
36 class My_Msg : public IMsg_Thread {
37 public:
38     IMsg_Thread *handle();
39     void result();
40     int m_val;
41 };
42
43 IMsg_Thread *My_Msg::handle()
44 {
45     My_Msg *res = new My_Msg;
46     int sl = rand() % 5;
47
48     res->m_val = m_val;
49     printf("My_Msg::handle val=%d sleep=%d\n", m_val, sl);
50 #if HAVE_UNISTD_H
51     sleep(sl);
52 #endif
53     return res;
54 }
55
56 void My_Msg::result()
57 {
58     printf("My_Msg::result val=%d\n", m_val);
59 }
60
61 class My_Timer_Thread : public ISocketObserver {
62 private:
63     ISocketObservable *m_obs;
64     int m_fd[2];
65     Msg_Thread *m_t;
66 public:
67     My_Timer_Thread(ISocketObservable *obs, Msg_Thread *t);
68     void socketNotify(int event);
69 };
70
71 My_Timer_Thread::My_Timer_Thread(ISocketObservable *obs,
72                                  Msg_Thread *t) : m_obs(obs) 
73 {
74     pipe(m_fd);
75     m_t = t;
76     obs->addObserver(m_fd[0], this);
77     obs->maskObserver(this, SOCKET_OBSERVE_READ);
78     obs->timeoutObserver(this, 1);
79 }
80
81 void My_Timer_Thread::socketNotify(int event)
82 {
83     static int seq = 1;
84     printf("Add %d\n", seq);
85     My_Msg *m = new My_Msg;
86     m->m_val = seq++;
87     m_t->put(m);
88 }
89
90 int main(int argc, char **argv)
91 {
92     SocketManager mySocketManager;
93
94     Msg_Thread m(&mySocketManager, 3);
95     My_Timer_Thread t(&mySocketManager, &m) ;
96     int i = 0;
97     while (++i < 5 && mySocketManager.processEvent() > 0)
98         ;
99     return 0;
100 }
101 /*
102  * Local variables:
103  * c-basic-offset: 4
104  * indent-tabs-mode: nil
105  * End:
106  * vim: shiftwidth=4 tabstop=8 expandtab
107  */
108