Working on UrsulaRenewal, Request, and Update
[yazpp-moved-to-github.git] / src / yaz-pdu-assoc-thread.cpp
1 /*
2  * Copyright (c) 1998-2001, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Log: yaz-pdu-assoc-thread.cpp,v $
6  * Revision 1.2  2001-03-27 14:47:45  adam
7  * New server facility scheme.
8  *
9  * Revision 1.1  2001/03/26 14:43:49  adam
10  * New threaded PDU association.
11  *
12  */
13
14 #ifdef WIN32
15 #include <process.h>
16 #else
17 #include <pthread.h>
18 #include <unistd.h>
19 #endif
20
21
22 #include <errno.h>
23 #include <yaz/log.h>
24 #include <yaz/tcpip.h>
25
26 #include <yaz++/yaz-pdu-assoc.h>
27 #include <yaz++/yaz-socket-manager.h>
28
29
30
31 Yaz_PDU_AssocThread::Yaz_PDU_AssocThread(
32     IYazSocketObservable *socketObservable)
33     : Yaz_PDU_Assoc(socketObservable)
34 {
35     
36 }
37
38 #ifdef WIN32
39 void __cdecl
40 #else
41 void *
42 #endif 
43 events(void *p)
44 {
45     Yaz_SocketManager *s = (Yaz_SocketManager *) p;
46     
47     logf (LOG_LOG, "thread started");
48     while (s->processEvent() > 0)
49         ;
50     logf (LOG_LOG, "thread finished");
51 #ifdef WIN32
52 #else
53     return 0;
54 #endif
55 }
56
57 void Yaz_PDU_AssocThread::childNotify(COMSTACK cs)
58 {
59     Yaz_SocketManager *socket_observable = new Yaz_SocketManager;
60     Yaz_PDU_Assoc *new_observable = new Yaz_PDU_Assoc (socket_observable, cs);
61     
62     /// Clone PDU Observer
63     new_observable->m_PDU_Observer =
64         m_PDU_Observer->sessionNotify(new_observable, cs_fileno(cs));
65 #ifdef WIN32
66     long t_id;
67     t_id = _beginthread (events, 0, socket_observable);
68     if (t_id == -1)
69     {
70         yaz_log (LOG_FATAL|LOG_ERRNO, "_beginthread failed");
71         exit (1);
72     }
73 #else
74     pthread_t tid;
75
76     int id = pthread_create (&tid, 0, events, socket_observable);
77     if (id)
78         yaz_log (LOG_ERRNO|LOG_FATAL, "pthread_create returned id=%d", id);
79     else
80         pthread_detach (tid);
81 #endif
82 }