- ipc_client class is a helper for client.
- ipc_client class that makes it easy to connect a channel to use for clients.
- it creates and connects socket/channel/channel event_handler.
Change-Id: If75a4ab542d3623a785ccf8bccbd66628b9646db
Signed-off-by: kibak.yoon <kibak.yoon@samsung.com>
--- /dev/null
+/*
+ * sensord
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "channel_event_handler.h"
+
+#include "channel.h"
+#include "channel_handler.h"
+#include "sensor_log.h"
+
+using namespace ipc;
+
+channel_event_handler::channel_event_handler(channel *ch, channel_handler *handler)
+: m_ch(ch)
+, m_handler(handler)
+{
+}
+
+channel_event_handler::~channel_event_handler()
+{
+ m_ch = NULL;
+ m_handler = NULL;
+}
+
+bool channel_event_handler::handle(int fd, event_condition condition)
+{
+ message msg;
+
+ if (!m_ch || !m_ch->is_connected())
+ return false;
+
+ if (condition & (EVENT_HUP)) {
+ m_ch->disconnect();
+ delete m_ch;
+ m_ch = NULL;
+ return false;
+ }
+
+ if (!m_ch->read_sync(msg)) {
+ m_ch->disconnect();
+ delete m_ch;
+ m_ch = NULL;
+ return false;
+ }
+
+ return true;
+}
+
+void channel_event_handler::connected(channel *ch)
+{
+ if (m_handler)
+ m_handler->connected(ch);
+}
+
+void channel_event_handler::disconnected(channel *ch)
+{
+ if (m_handler)
+ m_handler->disconnected(ch);
+}
+
+void channel_event_handler::read(channel *ch, message &msg)
+{
+ if (m_handler)
+ m_handler->read(ch, msg);
+}
+
+void channel_event_handler::read_complete(channel *ch)
+{
+ if (m_handler)
+ m_handler->read_complete(ch);
+}
+
+void channel_event_handler::error_caught(channel *ch, int error)
+{
+ if (m_handler)
+ m_handler->error_caught(ch, error);
+}
--- /dev/null
+/*
+ * sensord
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef __CHANNEL_EVENT_HANDLER_H__
+#define __CHANNEL_EVENT_HANDLER_H__
+
+#include <unordered_map>
+
+#include "event_handler.h"
+#include "channel_handler.h"
+
+namespace ipc {
+
+class channel;
+
+class channel_event_handler : public event_handler, public channel_handler {
+public:
+ channel_event_handler(channel *ch, channel_handler *handler);
+ virtual ~channel_event_handler();
+
+ bool handle(int fd, event_condition condition);
+
+ void connected(channel *ch);
+ void disconnected(channel *ch);
+ void read(channel *ch, message &msg);
+ void read_complete(channel *ch);
+ void error_caught(channel *ch, int error);
+
+private:
+ channel *m_ch;
+ channel_handler *m_handler;
+};
+
+}
+
+#endif /* __CHANNEL_EVENT_HANDLER_H__ */
--- /dev/null
+/*
+ * sensord
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "ipc_client.h"
+
+#include "sensor_log.h"
+#include "stream_socket.h"
+#include "event_handler.h"
+#include "channel_event_handler.h"
+
+using namespace ipc;
+
+ipc_client::ipc_client(const std::string &path)
+{
+ m_path = path;
+}
+
+ipc_client::~ipc_client()
+{
+}
+
+bool ipc_client::set_option(int option, int value)
+{
+ return true;
+}
+
+bool ipc_client::set_option(const std::string &option, int value)
+{
+ return true;
+}
+
+channel *ipc_client::connect(channel_handler *handler)
+{
+ return connect(handler, NULL);
+}
+
+channel *ipc_client::connect(channel_handler *handler, event_loop *loop)
+{
+ socket *sock;
+ channel *ch;
+ channel_event_handler *ev_handler;
+
+ sock = new(std::nothrow) stream_socket();
+ retvm_if(!sock, NULL, "Failed to allocate memory");
+
+ if (!sock->create(m_path)) {
+ delete sock;
+ return NULL;
+ }
+
+ ch = new(std::nothrow) channel(sock);
+ if (!ch) {
+ delete sock;
+ _E("Failed to allocate memory");
+ return NULL;
+ }
+
+ ev_handler = new(std::nothrow) channel_event_handler(ch, handler);
+ if (!ev_handler) {
+ delete ch;
+ delete sock;
+ _E("Faield to allocate memory");
+ return NULL;
+ }
+
+ ch->connect(ev_handler, loop);
+
+ if (loop) {
+ uint64_t id = loop->add_event(sock->get_fd(),
+ (EVENT_IN | EVENT_HUP | EVENT_NVAL), ev_handler);
+ ch->set_event_id(id);
+ }
+
+ _I("Connected");
+ return ch;
+}
--- /dev/null
+/*
+ * sensord
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef __IPC_CLIENT_H__
+#define __IPC_CLIENT_H__
+
+#include <string>
+
+#include "channel.h"
+#include "channel_handler.h"
+#include "event_loop.h"
+
+namespace ipc {
+
+class ipc_client {
+public:
+ ipc_client(const std::string &path);
+ ~ipc_client();
+
+ bool set_option(int option, int value);
+ bool set_option(const std::string &option, int value);
+
+ /* call channel->disconnect() after you have used it */
+ channel *connect(channel_handler *handler);
+ channel *connect(channel_handler *handler, event_loop *loop);
+
+private:
+ std::string m_path;
+};
+
+}
+
+#endif /* __IPC_CLIENT_H__ */