00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef SAGA_COMM_STREAM_STREAM_HPP
00010 #define SAGA_COMM_STREAM_STREAM_HPP
00011
00012
00013 #include <string>
00014 #include <vector>
00015
00016
00017 #include <saga/saga/util.hpp>
00018 #include <saga/saga/call.hpp>
00019 #include <saga/saga/base.hpp>
00020 #include <saga/saga/session.hpp>
00021 #include <saga/saga/task.hpp>
00022 #include <saga/saga/buffer.hpp>
00023 #include <saga/saga/url.hpp>
00024 #include <saga/saga/context.hpp>
00025
00026 #include <saga/saga/detail/attribute.hpp>
00027 #include <saga/saga/detail/monitorable.hpp>
00028
00029
00030 #include <saga/saga/packages/stream/config.hpp>
00031
00032
00033 #if defined(BOOST_MSVC)
00034 #pragma warning(push)
00035 #pragma warning(disable: 4251 4231 4660)
00036 #endif
00037
00038 namespace saga
00039 {
00042 namespace stream {
00043
00046 namespace attributes
00047 {
00048
00049 char const* const stream_bufsize = "Bufsize";
00050 char const* const stream_timeout = "Timeout";
00051 char const* const stream_blocking = "Blocking";
00052 char const* const stream_compression = "Compression";
00053 char const* const stream_nodelay = "Nodelay";
00054 char const* const stream_reliable = "Reliable";
00055 }
00056
00060 namespace metrics
00061 {
00062 char const* const stream_state = "stream.State";
00063 char const* const stream_read = "stream.Read";
00064 char const* const stream_write = "stream.Write";
00065 char const* const stream_exception = "stream.Exception";
00066 char const* const stream_dropped = "stream.Dropped";
00067 }
00068
00069 namespace attributes
00070 {
00071
00072 char const* const stream_state_unknown = "Unknown";
00073 char const* const stream_state_new = "New";
00074 char const* const stream_state_open = "open";
00075 char const* const stream_state_closed = "Closed";
00076 char const* const stream_state_dropped = "Dropped";
00077 char const* const stream_state_error = "Error";
00078 }
00079
00092 enum state
00093 {
00094 Unknown = -1,
00095 New = 1,
00096 Open = 2,
00097 Closed = 3,
00098 Dropped = 4,
00099 Error = 5
00100 };
00101
00110 enum activity
00111 {
00112 Read = 1,
00113 Write = 2,
00114 Exception = 4
00115 };
00116
00121 class SAGA_STREAM_PACKAGE_EXPORT stream
00122 : public saga::object,
00123 public saga::detail::attribute<stream>,
00124 public saga::detail::monitorable<stream>
00125 {
00127
00128 friend struct saga::detail::attribute<stream>;
00129 friend struct saga::detail::monitorable<stream>;
00130
00131 typedef saga::detail::attribute<stream> attribute_base_type;
00132 typedef saga::detail::monitorable<stream> monitorable_base_type;
00134
00135 public:
00136
00141
00142
00143 protected:
00145
00146 TR1::shared_ptr <saga::impl::stream> get_impl_sp(void) const;
00147 saga::impl::stream* get_impl (void) const;
00148 friend class saga::impl::stream;
00149 friend struct saga::detail::create_default<stream>;
00150
00151 explicit stream (saga::impl::stream *);
00152 explicit stream (int);
00154
00155 private:
00156
00157 SAGA_CALL_CREATE_PRIV_2(session const&, saga::url)
00158
00159
00160 SAGA_CALL_CONST_PRIV_0(get_url)
00161 SAGA_CALL_CONST_PRIV_0(get_context)
00162
00163
00164 SAGA_CALL_PRIV_1(connect, double)
00165 SAGA_CALL_PRIV_2(wait, saga::stream::activity, double)
00166 SAGA_CALL_PRIV_1(close, double)
00167 SAGA_CALL_PRIV_2(read, saga::mutable_buffer, saga::ssize_t)
00168 SAGA_CALL_PRIV_2(write, saga::const_buffer, saga::ssize_t)
00169
00170 void init_attributes();
00171 void init_metrics();
00172
00173 public:
00178 explicit stream (session const & s, saga::url url = saga::url());
00179
00184 explicit stream (saga::url url);
00185
00190 stream ();
00191
00196 explicit stream (saga::object const& o);
00197
00202 ~stream (void);
00203
00207 static stream create(session const& s, saga::url name = saga::url())
00208 {
00209 return stream(s, name);
00210 }
00212 SAGA_CALL_CREATE_2_DEF_1(session const&, saga::url, saga::url())
00214
00217 static stream create(saga::url name = saga::url())
00218 {
00219 return stream(name);
00220 }
00221 template <typename Tag>
00222 static saga::task create(saga::url name = saga::url())
00223 {
00224 return create<Tag>(detail::get_the_session(), name);
00225 }
00226
00231 stream &operator= (saga::object const& o);
00232
00239 saga::url get_url() const
00240 {
00241 saga::task t = get_urlpriv(saga::task_base::Sync());
00242 return t.get_result<saga::url>();
00243 }
00244 SAGA_CALL_CONST_PUB_0_DEF_0(get_url)
00245
00246
00252 saga::context get_context() const
00253 {
00254 saga::task t = get_contextpriv(saga::task_base::Sync());
00255 return t.get_result<saga::context>();
00256 }
00257 SAGA_CALL_CONST_PUB_0_DEF_0(get_context)
00258
00259
00265 void connect(double timeout = -1.0)
00266 {
00267 saga::task t = connectpriv(timeout, saga::task_base::Sync());
00268 return t.get_result();
00269 }
00270 SAGA_CALL_PUB_1_DEF_1(connect, double, -1.0)
00271
00272
00281 saga::stream::activity
00282 wait(saga::stream::activity what, double timeout = -1.0)
00283 {
00284 saga::task t = waitpriv(what, timeout, saga::task_base::Sync());
00285 return t.get_result<saga::stream::activity>();
00286 }
00287 SAGA_CALL_PUB_2_DEF_1(wait, saga::stream::activity, double, -1.0)
00288
00289
00294 void close(double timeout = 0.0)
00295 {
00296 saga::task t = closepriv(timeout, saga::task_base::Sync());
00297 t.get_result ();
00298 }
00299 SAGA_CALL_PUB_1_DEF_1(close, double, 0.0)
00300
00301
00309 saga::ssize_t read(saga::mutable_buffer buffer, saga::ssize_t length = 0)
00310 {
00311 saga::task t = readpriv(buffer, length, saga::task_base::Sync());
00312 return t.get_result<saga::ssize_t>();
00313 }
00314 SAGA_CALL_PUB_2_DEF_1(read, saga::mutable_buffer, saga::ssize_t, 0)
00315
00316
00324 saga::ssize_t write(saga::const_buffer buffer, saga::ssize_t length = 0)
00325 {
00326 saga::task t = writepriv(buffer, length, saga::task_base::Sync());
00327 return t.get_result<saga::ssize_t>();
00328 }
00329 SAGA_CALL_PUB_2_DEF_1(write, saga::const_buffer, saga::ssize_t, 0)
00330 };
00331
00332 }
00333
00334 namespace detail
00335 {
00336
00337
00338
00339 template<>
00340 struct create_default<saga::stream::stream>
00341 {
00342 static saga::stream::stream* call()
00343 {
00344 return new saga::stream::stream(1);
00345 }
00346 template <typename T_> static void call(T_* obj)
00347 {
00348 new (obj) saga::stream::stream(1);
00349 }
00350 };
00351 }
00352
00354 namespace adaptors
00355 {
00356 saga::stream::state SAGA_STREAM_PACKAGE_EXPORT
00357 stream_state_value_to_enum(std::string const& val);
00358 std::string SAGA_STREAM_PACKAGE_EXPORT
00359 stream_state_enum_to_value(int s);
00360 }
00362 }
00363
00364
00365 #if defined(BOOST_MSVC)
00366 #pragma warning(pop)
00367 #endif
00368
00369 #endif // SAGA_COMM_STREAM_STREAM_HPP
00370