bda52f2d2ad7a105a19e70c4b298251cceea29c0
[platform/framework/web/wrt-commons.git] / modules / core / src / named_input_pipe.cpp
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        named_input_pipe.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of named input pipe
21  */
22 #include <dpl/named_input_pipe.h>
23 #include <dpl/binary_queue.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <errno.h>
29
30 namespace DPL
31 {
32 namespace // anonymous
33 {
34 const size_t DEFAULT_READ_BUFFER_SIZE = 1024;
35 } // namespace anonymous
36
37 NamedInputPipe::NamedInputPipe()
38     : m_fifo(-1)
39 {
40 }
41
42 NamedInputPipe::~NamedInputPipe()
43 {
44     Close();
45 }
46
47 void NamedInputPipe::Open(const std::string& pipeName)
48 {
49     // Open pipe for reading
50     int fifo = TEMP_FAILURE_RETRY(open(pipeName.c_str(), O_RDONLY | O_NONBLOCK));
51
52     if (fifo == -1)
53         ThrowMsg(Exception::OpenFailed, pipeName);
54
55     m_fifo = fifo;
56 }
57
58 void NamedInputPipe::Close()
59 {
60     if (m_fifo == -1)
61         return;
62
63     if (TEMP_FAILURE_RETRY(close(m_fifo)) == -1)
64         Throw(Exception::CloseFailed);
65
66     m_fifo = -1;
67 }
68
69 BinaryQueueAutoPtr NamedInputPipe::Read(size_t size)
70 {
71     size_t bytesToRead = size > DEFAULT_READ_BUFFER_SIZE ? DEFAULT_READ_BUFFER_SIZE : size;
72
73     // Malloc default read buffer size
74     // It is unmanaged, so it can be then attached directly to binary queue
75     void *buffer = malloc(bytesToRead);
76
77     if (buffer == NULL)
78         throw std::bad_alloc();
79
80     ssize_t result = TEMP_FAILURE_RETRY(read(m_fifo, buffer, bytesToRead));
81
82     if (result > 0)
83     {
84         // Succedded to read socket data
85         BinaryQueueAutoPtr binaryQueue(new BinaryQueue());
86
87         // Append unmanaged memory
88         binaryQueue->AppendUnmanaged(buffer, result, &BinaryQueue::BufferDeleterFree, NULL);
89
90         // Return buffer
91         return binaryQueue;
92     }
93     else if (result == 0)
94     {
95         // Socket was gracefuly closed
96         free(buffer);
97
98         // Return empty buffer
99         return BinaryQueueAutoPtr(new BinaryQueue());
100     }
101     else
102     {
103         // Must first save errno value, because it may be altered
104         int lastErrno = errno;
105
106         // Free buffer
107         free(buffer);
108
109         // Interpret error result
110         (void)lastErrno;
111
112         // FIXME: Handle specific errno
113         Throw(AbstractInput::Exception::ReadFailed);
114     }
115 }
116
117 int NamedInputPipe::WaitableReadHandle() const
118 {
119     return m_fifo;
120 }
121 } // namespace DPL