e9ea4fb65cd1246c031ade93de5843b4ee31a6fb
[yazpp-moved-to-github.git] / include / yaz++ / pdu-assoc.h
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: pdu-assoc.h,v 1.6 2005-06-02 06:40:21 adam Exp $
6  */
7
8 #ifndef YAZ_PDU_ASSOC_INCLUDED
9 #define YAZ_PDU_ASSOC_INCLUDED
10
11 #include <yaz/comstack.h>
12 #include <yaz++/socket-observer.h>
13 #include <yaz++/pdu-observer.h>
14
15 namespace yazpp_1 {
16 /** Simple Protocol Data Unit Assocation.
17     This object sends - and receives PDU's using the COMSTACK
18     network utility. To use the association in client role, use
19     the method connect. The server role is initiated by using the
20     listen method.
21  */
22 class YAZ_EXPORT Yaz_PDU_Assoc : public IYaz_PDU_Observable, yazpp_1::IYazSocketObserver {
23     friend class Yaz_PDU_AssocThread;
24  private:
25     enum { 
26         Connecting,
27         Listen,
28         Ready,
29         Closed,
30         Writing,
31         Accepting
32     } m_state;
33     class PDU_Queue {
34     public:
35         PDU_Queue(const char *buf, int len);
36         PDU_Queue::~PDU_Queue();
37         char *m_buf;
38         int m_len;
39         PDU_Queue *m_next;
40     };
41     Yaz_PDU_Assoc *m_parent;
42     Yaz_PDU_Assoc *m_children;
43     Yaz_PDU_Assoc *m_next;
44     COMSTACK m_cs;
45     yazpp_1::IYazSocketObservable *m_socketObservable;
46     IYaz_PDU_Observer *m_PDU_Observer;
47     char *m_input_buf;
48     int m_input_len;
49     PDU_Queue *m_queue_out;
50     PDU_Queue *m_queue_in;
51     int Yaz_PDU_Assoc::flush_PDU();
52     int *m_destroyed;
53     int m_idleTime;
54     int m_log;
55     void init(yazpp_1::IYazSocketObservable *socketObservable);
56  public:
57     COMSTACK comstack(const char *type_and_host, void **vp);
58     /// Create object using specified socketObservable
59     Yaz_PDU_Assoc(yazpp_1::IYazSocketObservable *socketObservable);
60     /// Create Object using existing comstack
61     Yaz_PDU_Assoc(yazpp_1::IYazSocketObservable *socketObservable,
62                   COMSTACK cs);
63     /// Close socket and destroy object.
64     /// virtual ~Yaz_PDU_Assoc();
65     /// Clone the object
66     IYaz_PDU_Observable *clone();
67     /// Send PDU
68     int send_PDU(const char *buf, int len);
69     /// connect to server (client role)
70     int connect(IYaz_PDU_Observer *observer, const char *addr);
71     /// listen for clients (server role)
72     int listen(IYaz_PDU_Observer *observer, const char *addr);
73     /// Socket notification
74     void socketNotify(int event);
75     /// Close socket
76     void close();
77     /// Close and destroy
78     void destroy();
79     /// Set Idle Time
80     void idleTime (int timeout);
81     /// Child start...
82     virtual void childNotify(COMSTACK cs);
83     const char *getpeername();
84 };
85
86 class YAZ_EXPORT Yaz_PDU_AssocThread : public Yaz_PDU_Assoc {
87  public:
88     Yaz_PDU_AssocThread(yazpp_1::IYazSocketObservable *socketObservable);
89  private:
90     void childNotify(COMSTACK cs);
91
92 };
93 };
94
95 #endif
96