bed8a7e88a322eabb99feec8b647a1134f6225d6
[platform/core/system/tlm.git] / src / common / tlm-pipe-stream.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of tlm
5  *
6  * Copyright (C) 2013 Intel Corporation.
7  *
8  * Contact: Imran Zaman <imran.zaman@linux.intel.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25 #include <gio/gunixinputstream.h>
26 #include <gio/gunixoutputstream.h>
27
28 #include "tlm-pipe-stream.h"
29 #include "tlm-log.h"
30
31 #define TLM_PIPE_STREAM_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj),\
32     TLM_TYPE_PIPE_STREAM, TlmPipeStreamPrivate))
33
34 struct _TlmPipeStreamPrivate
35 {
36     GInputStream  *input_stream;
37     GOutputStream *output_stream;
38 };
39
40 G_DEFINE_TYPE (TlmPipeStream, tlm_pipe_stream, G_TYPE_IO_STREAM);
41
42 static GInputStream *
43 _tlm_pipe_stream_get_input_stream (GIOStream *io_stream)
44 {
45     return TLM_PIPE_STREAM (io_stream)->priv->input_stream;
46 }
47
48 static GOutputStream *
49 _tlm_pipe_stream_get_output_stream (GIOStream *io_stream)
50 {
51     return TLM_PIPE_STREAM (io_stream)->priv->output_stream;
52 }
53
54 static void
55 _tlm_pipe_stream_dispose (GObject *gobject)
56 {
57     g_return_if_fail (TLM_IS_PIPE_STREAM (gobject));
58
59     DBG ("%p", gobject);
60     /* Chain up to the parent class */
61     G_OBJECT_CLASS (tlm_pipe_stream_parent_class)->dispose (gobject);
62
63 }
64
65 static void
66 _tlm_pipe_stream_finalize (GObject *gobject)
67 {
68     TlmPipeStream *stream = TLM_PIPE_STREAM (gobject);
69
70     /* g_io_stream needs streams to be valid in its dispose still
71      */
72     if (stream->priv->input_stream) {
73         g_clear_object (&stream->priv->input_stream);
74     }
75
76     if (stream->priv->output_stream) {
77         g_clear_object (&stream->priv->output_stream);
78     }
79
80     G_OBJECT_CLASS (tlm_pipe_stream_parent_class)->finalize (gobject);
81 }
82
83 static void
84 tlm_pipe_stream_class_init (TlmPipeStreamClass *klass)
85 {
86     GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
87     GIOStreamClass *stream_class = G_IO_STREAM_CLASS (klass);
88
89     gobject_class->finalize = _tlm_pipe_stream_finalize;
90     gobject_class->dispose = _tlm_pipe_stream_dispose;
91
92     /* virtual methods */
93     stream_class->get_input_stream = _tlm_pipe_stream_get_input_stream;
94     stream_class->get_output_stream = _tlm_pipe_stream_get_output_stream;
95
96     g_type_class_add_private (klass, sizeof (TlmPipeStreamPrivate));
97 }
98
99 static void
100 tlm_pipe_stream_init (TlmPipeStream *self)
101 {
102     self->priv = TLM_PIPE_STREAM_GET_PRIVATE (self);
103     self->priv->input_stream = NULL;
104     self->priv->output_stream = NULL;
105 }
106
107 TlmPipeStream *
108 tlm_pipe_stream_new (
109         gint in_fd,
110         gint out_fd,
111         gboolean close_fds)
112 {
113     TlmPipeStream *stream = TLM_PIPE_STREAM (g_object_new (
114             TLM_TYPE_PIPE_STREAM, NULL));
115     if (stream) {
116         stream->priv->input_stream = G_INPUT_STREAM (
117                 g_unix_input_stream_new (in_fd, close_fds));
118         stream->priv->output_stream = G_OUTPUT_STREAM (
119                 g_unix_output_stream_new (out_fd, close_fds));
120     }
121     DBG ("%p", stream);
122     return stream;
123 }
124
125