2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file named_output_pipe.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of named output pipe
23 #include <dpl/named_output_pipe.h>
24 #include <dpl/binary_queue.h>
25 #include <dpl/scoped_free.h>
26 #include <sys/types.h>
34 NamedOutputPipe::NamedOutputPipe()
39 NamedOutputPipe::~NamedOutputPipe()
44 void NamedOutputPipe::Open(const std::string& pipeName)
46 // Then open it for reading or writing
47 int fifo = TEMP_FAILURE_RETRY(open(pipeName.c_str(), O_WRONLY | O_NONBLOCK));
50 ThrowMsg(Exception::OpenFailed, pipeName);
55 void NamedOutputPipe::Close()
60 if (TEMP_FAILURE_RETRY(close(m_fifo)) == -1)
61 Throw(Exception::CloseFailed);
66 size_t NamedOutputPipe::Write(const BinaryQueue &buffer, size_t bufferSize)
69 if (bufferSize > buffer.Size())
70 bufferSize = buffer.Size();
72 // FIXME: User write visitor to write !
73 // WriteVisitor visitor
75 ScopedFree<void> flattened(malloc(bufferSize));
76 buffer.Flatten(flattened.Get(), bufferSize);
78 ssize_t result = TEMP_FAILURE_RETRY(write(m_fifo, flattened.Get(), bufferSize));
82 // Successfuly written some bytes
83 return static_cast<size_t>(result);
87 // This is abnormal result
88 ThrowMsg(CommonException::InternalError, "Invalid socket write result, 0 bytes written");
92 // Interpret error result
93 // FIXME: Handle errno
94 Throw(AbstractOutput::Exception::WriteFailed);
98 int NamedOutputPipe::WaitableWriteHandle() const