Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules / socket / include / dpl / socket / waitable_input_output_execution_context_support.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        waitable_input_output_execution_context_support.h
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the header file for waitable input-output execution
21  * context support
22  */
23 #ifndef DPL_WAITABLE_INPUT_OUTPUT_EXECUTION_CONTEXT_SUPPORT_H
24 #define DPL_WAITABLE_INPUT_OUTPUT_EXECUTION_CONTEXT_SUPPORT_H
25
26 #include <dpl/abstract_waitable_input_output.h>
27 #include <dpl/waitable_handle_watch_support.h>
28 #include <dpl/binary_queue.h>
29
30 namespace DPL {
31 namespace Socket {
32 class WaitableInputOutputExecutionContextSupport :
33     private WaitableHandleWatchSupport::WaitableHandleListener
34 {
35   public:
36     class Exception
37     {
38       public:
39         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
40         DECLARE_EXCEPTION_TYPE(Base, AlreadyOpened)
41         DECLARE_EXCEPTION_TYPE(Base, NotOpened)
42         DECLARE_EXCEPTION_TYPE(Base, OpenFailed)
43         DECLARE_EXCEPTION_TYPE(Base, CloseFailed)
44     };
45
46   private:
47     bool m_opened;
48     AbstractWaitableInputOutput *m_waitableInputOutput;
49
50     // Watch state
51     bool m_hasReadWatch;
52     bool m_hasWriteWatch;
53
54     void AddReadWatch();
55     void RemoveReadWatch();
56     void AddWriteWatch();
57     void RemoveWriteWatch();
58
59     void ReadInput();
60
61     void CheckedRemoveReadWatch();
62     void CheckedRemoveWriteWatch();
63
64     void CheckedRemoveReadWriteWatch();
65
66     virtual void OnWaitableHandleEvent(WaitableHandle waitableHandle,
67                                        WaitMode::Type mode);
68
69   protected:
70     // Incoming/Outgoing streams
71     BinaryQueue m_inputStream;
72     BinaryQueue m_outputStream;
73
74     // Support calbback methods
75     virtual void OnInputStreamRead() = 0;
76     virtual void OnInputStreamClosed() = 0;
77     virtual void OnInputStreamBroken() = 0;
78
79     // Trigger feeding output - after updating output stream
80     void FeedOutput();
81
82     // Open/Close destination waitable input-output
83     void Open(AbstractWaitableInputOutput *waitableInputOutput);
84     void Close();
85
86   public:
87     /**
88      * Constructor
89      */
90     explicit WaitableInputOutputExecutionContextSupport();
91
92     /**
93      * Destructor
94      */
95     virtual ~WaitableInputOutputExecutionContextSupport();
96 };
97 }
98 } // namespace DPL
99
100 #endif // DPL_WAITABLE_INPUT_OUTPUT_EXECUTION_CONTEXT_SUPPORT_H