tizen 2.4 release
[framework/web/wrt-commons.git] / modules / core / src / named_output_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_output_pipe.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of named output pipe
21  */
22 #include <stddef.h>
23 #include <dpl/named_output_pipe.h>
24 #include <dpl/binary_queue.h>
25 #include <dpl/free_deleter.h>
26 #include <memory>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32
33 namespace DPL {
34 NamedOutputPipe::NamedOutputPipe() :
35     m_fifo(-1)
36 {}
37
38 NamedOutputPipe::~NamedOutputPipe()
39 {
40     Close();
41 }
42
43 void NamedOutputPipe::Open(const std::string& pipeName)
44 {
45     // Then open it for reading or writing
46     int fifo = TEMP_FAILURE_RETRY(open(pipeName.c_str(), O_WRONLY | O_NONBLOCK));
47
48     if (fifo == -1) {
49         ThrowMsg(Exception::OpenFailed, pipeName);
50     }
51
52     m_fifo = fifo;
53 }
54
55 void NamedOutputPipe::Close()
56 {
57     if (m_fifo == -1) {
58         return;
59     }
60
61     if (TEMP_FAILURE_RETRY(close(m_fifo)) == -1) {
62         Throw(Exception::CloseFailed);
63     }
64
65     m_fifo = -1;
66 }
67
68 size_t NamedOutputPipe::Write(const BinaryQueue &buffer, size_t bufferSize)
69 {
70     // Adjust write size
71     if (bufferSize > buffer.Size()) {
72         bufferSize = buffer.Size();
73     }
74
75     // FIXME: User write visitor to write !
76     // WriteVisitor visitor
77
78     std::unique_ptr<void,free_deleter> flattened(malloc(bufferSize));
79     buffer.Flatten(flattened.get(), bufferSize);
80
81     ssize_t result =
82         TEMP_FAILURE_RETRY(write(m_fifo, flattened.get(), bufferSize));
83
84     if (result > 0) {
85         // Successfuly written some bytes
86         return static_cast<size_t>(result);
87     } else if (result == 0) {
88         // This is abnormal result
89         ThrowMsg(CommonException::InternalError,
90                  "Invalid socket write result, 0 bytes written");
91     } else {
92         // Interpret error result
93         // FIXME: Handle errno
94         Throw(AbstractOutput::Exception::WriteFailed);
95     }
96 }
97
98 int NamedOutputPipe::WaitableWriteHandle() const
99 {
100     return m_fifo;
101 }
102 } // namespace DPL