1 /*
2 * Copyright 2015 MongoDB, Inc.
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
18 #ifndef DEBUG_STREAM_H
19 #define DEBUG_STREAM_H
20
21 #include <mongoc.h>
22 #include <mongoc-client-private.h>
23
24 #include "test-libmongoc.h"
25
26
27 #define MONGOC_STREAM_DEBUG 7
28 typedef struct _mongoc_stream_debug_t {
29 mongoc_stream_t vtable;
30 mongoc_stream_t *wrapped;
31 debug_stream_stats_t *stats;
32 } mongoc_stream_debug_t;
33
34
35 static int
_mongoc_stream_debug_close(mongoc_stream_t * stream)36 _mongoc_stream_debug_close (mongoc_stream_t *stream)
37 {
38 return mongoc_stream_close (((mongoc_stream_debug_t *) stream)->wrapped);
39 }
40
41
42 static void
_mongoc_stream_debug_destroy(mongoc_stream_t * stream)43 _mongoc_stream_debug_destroy (mongoc_stream_t *stream)
44 {
45 mongoc_stream_debug_t *debug_stream = (mongoc_stream_debug_t *) stream;
46
47 debug_stream->stats->n_destroyed++;
48
49 mongoc_stream_destroy (debug_stream->wrapped);
50 bson_free (debug_stream);
51 }
52
53
54 static void
_mongoc_stream_debug_failed(mongoc_stream_t * stream)55 _mongoc_stream_debug_failed (mongoc_stream_t *stream)
56 {
57 mongoc_stream_debug_t *debug_stream = (mongoc_stream_debug_t *) stream;
58
59 debug_stream->stats->n_failed++;
60
61 mongoc_stream_failed (debug_stream->wrapped);
62 bson_free (debug_stream);
63 }
64
65
66 static int
_mongoc_stream_debug_setsockopt(mongoc_stream_t * stream,int level,int optname,void * optval,mongoc_socklen_t optlen)67 _mongoc_stream_debug_setsockopt (mongoc_stream_t *stream,
68 int level,
69 int optname,
70 void *optval,
71 mongoc_socklen_t optlen)
72 {
73 return mongoc_stream_setsockopt (((mongoc_stream_debug_t *) stream)->wrapped,
74 level,
75 optname,
76 optval,
77 optlen);
78 }
79
80
81 static int
_mongoc_stream_debug_flush(mongoc_stream_t * stream)82 _mongoc_stream_debug_flush (mongoc_stream_t *stream)
83 {
84 return mongoc_stream_flush (((mongoc_stream_debug_t *) stream)->wrapped);
85 }
86
87
88 static ssize_t
_mongoc_stream_debug_readv(mongoc_stream_t * stream,mongoc_iovec_t * iov,size_t iovcnt,size_t min_bytes,int32_t timeout_msec)89 _mongoc_stream_debug_readv (mongoc_stream_t *stream,
90 mongoc_iovec_t *iov,
91 size_t iovcnt,
92 size_t min_bytes,
93 int32_t timeout_msec)
94 {
95 return mongoc_stream_readv (((mongoc_stream_debug_t *) stream)->wrapped,
96 iov,
97 iovcnt,
98 min_bytes,
99 timeout_msec);
100 }
101
102
103 static ssize_t
_mongoc_stream_debug_writev(mongoc_stream_t * stream,mongoc_iovec_t * iov,size_t iovcnt,int32_t timeout_msec)104 _mongoc_stream_debug_writev (mongoc_stream_t *stream,
105 mongoc_iovec_t *iov,
106 size_t iovcnt,
107 int32_t timeout_msec)
108 {
109 return mongoc_stream_writev (
110 ((mongoc_stream_debug_t *) stream)->wrapped, iov, iovcnt, timeout_msec);
111 }
112
113
114 static bool
_mongoc_stream_debug_check_closed(mongoc_stream_t * stream)115 _mongoc_stream_debug_check_closed (mongoc_stream_t *stream)
116 {
117 return mongoc_stream_check_closed (
118 ((mongoc_stream_debug_t *) stream)->wrapped);
119 }
120
121
122 static bool
_mongoc_stream_debug_timed_out(mongoc_stream_t * stream)123 _mongoc_stream_debug_timed_out (mongoc_stream_t *stream)
124 {
125 return mongoc_stream_timed_out (
126 ((mongoc_stream_debug_t *) stream)->wrapped);
127 }
128
129
130 static mongoc_stream_t *
_mongoc_stream_debug_get_base_stream(mongoc_stream_t * stream)131 _mongoc_stream_debug_get_base_stream (mongoc_stream_t *stream)
132 {
133 mongoc_stream_t *wrapped = ((mongoc_stream_debug_t *) stream)->wrapped;
134
135 /* "wrapped" is typically a mongoc_stream_buffered_t, get the real
136 * base stream */
137 if (wrapped->get_base_stream) {
138 return wrapped->get_base_stream (wrapped);
139 }
140
141 return wrapped;
142 }
143
144
145 mongoc_stream_t *
debug_stream_new(mongoc_stream_t * stream,debug_stream_stats_t * stats)146 debug_stream_new (mongoc_stream_t *stream, debug_stream_stats_t *stats)
147 {
148 mongoc_stream_debug_t *debug_stream;
149
150 if (!stream) {
151 return NULL;
152 }
153
154 debug_stream = (mongoc_stream_debug_t *) bson_malloc0 (sizeof *debug_stream);
155
156 debug_stream->vtable.type = MONGOC_STREAM_DEBUG;
157 debug_stream->vtable.close = _mongoc_stream_debug_close;
158 debug_stream->vtable.destroy = _mongoc_stream_debug_destroy;
159 debug_stream->vtable.failed = _mongoc_stream_debug_failed;
160 debug_stream->vtable.flush = _mongoc_stream_debug_flush;
161 debug_stream->vtable.readv = _mongoc_stream_debug_readv;
162 debug_stream->vtable.writev = _mongoc_stream_debug_writev;
163 debug_stream->vtable.setsockopt = _mongoc_stream_debug_setsockopt;
164 debug_stream->vtable.check_closed = _mongoc_stream_debug_check_closed;
165 debug_stream->vtable.timed_out = _mongoc_stream_debug_timed_out;
166 debug_stream->vtable.get_base_stream = _mongoc_stream_debug_get_base_stream;
167
168 debug_stream->wrapped = stream;
169 debug_stream->stats = stats;
170
171 return (mongoc_stream_t *) debug_stream;
172 }
173
174
175 mongoc_stream_t *
debug_stream_initiator(const mongoc_uri_t * uri,const mongoc_host_list_t * host,void * user_data,bson_error_t * error)176 debug_stream_initiator (const mongoc_uri_t *uri,
177 const mongoc_host_list_t *host,
178 void *user_data,
179 bson_error_t *error)
180 {
181 debug_stream_stats_t *stats;
182 mongoc_stream_t *default_stream;
183
184 stats = (debug_stream_stats_t *) user_data;
185
186 default_stream =
187 mongoc_client_default_stream_initiator (uri, host, stats->client, error);
188
189 return debug_stream_new (default_stream, stats);
190 }
191
192
193 void
test_framework_set_debug_stream(mongoc_client_t * client,debug_stream_stats_t * stats)194 test_framework_set_debug_stream (mongoc_client_t *client,
195 debug_stream_stats_t *stats)
196 {
197 stats->client = client;
198 mongoc_client_set_stream_initiator (client, debug_stream_initiator, stats);
199 }
200
201 #endif /* DEBUG_STREAM_H */
202