Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_wearable / 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/scoped_free.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <errno.h>
31
32 namespace DPL {
33 NamedOutputPipe::NamedOutputPipe() :
34     m_fifo(-1)
35 {}
36
37 NamedOutputPipe::~NamedOutputPipe()
38 {
39     Close();
40 }
41
42 void NamedOutputPipe::Open(const std::string& pipeName)
43 {
44     // Then open it for reading or writing
45     int fifo = TEMP_FAILURE_RETRY(open(pipeName.c_str(), O_WRONLY | O_NONBLOCK));
46
47     if (fifo == -1) {
48         ThrowMsg(Exception::OpenFailed, pipeName);
49     }
50
51     m_fifo = fifo;
52 }
53
54 void NamedOutputPipe::Close()
55 {
56     if (m_fifo == -1) {
57         return;
58     }
59
60     if (TEMP_FAILURE_RETRY(close(m_fifo)) == -1) {
61         Throw(Exception::CloseFailed);
62     }
63
64     m_fifo = -1;
65 }
66
67 size_t NamedOutputPipe::Write(const BinaryQueue &buffer, size_t bufferSize)
68 {
69     // Adjust write size
70     if (bufferSize > buffer.Size()) {
71         bufferSize = buffer.Size();
72     }
73
74     // FIXME: User write visitor to write !
75     // WriteVisitor visitor
76
77     ScopedFree<void> flattened(malloc(bufferSize));
78     buffer.Flatten(flattened.Get(), bufferSize);
79
80     ssize_t result =
81         TEMP_FAILURE_RETRY(write(m_fifo, flattened.Get(), bufferSize));
82
83     if (result > 0) {
84         // Successfuly written some bytes
85         return static_cast<size_t>(result);
86     } else if (result == 0) {
87         // This is abnormal result
88         ThrowMsg(CommonException::InternalError,
89                  "Invalid socket write result, 0 bytes written");
90     } else {
91         // Interpret error result
92         // FIXME: Handle errno
93         Throw(AbstractOutput::Exception::WriteFailed);
94     }
95 }
96
97 int NamedOutputPipe::WaitableWriteHandle() const
98 {
99     return m_fifo;
100 }
101 } // namespace DPL