Mailbox.h
Go to the documentation of this file.
00001 /* 00002 * 00003 * Copyright 2006 Savarese Software Research Corporation 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.savarese.com/software/ApacheLicense-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00023 #ifndef __SSRC_SPREAD_MAILBOX_H 00024 #define __SSRC_SPREAD_MAILBOX_H 00025 00026 #include <ssrc/spread/ScatterMessage.h> 00027 00028 __BEGIN_NS_SSRC_SPREAD 00029 00038 class Timeout { 00039 Spread::sp_time time; 00040 00041 public: 00048 Timeout(const Spread::sp_time time) : time(time) { } 00049 00059 Timeout(const long sec = 0, const long usec = 0) { 00060 time.sec = sec, time.usec = usec; 00061 } 00062 00068 operator Spread::sp_time() const { return time; } 00069 }; 00070 00160 class Mailbox { 00161 public: 00163 typedef Spread::mailbox descriptor_type; 00164 00165 static const Timeout ZeroTimeout; 00166 00171 enum Priority { 00172 Low = LOW_PRIORITY, Medium = MEDIUM_PRIORITY, High = HIGH_PRIORITY 00173 }; 00174 00175 private: 00176 bool _group_membership, _drop_receive, _killed; 00177 string _connection, _name, _private_group; 00178 descriptor_type _mbox; 00179 ScatterMessage _scatter; 00180 GroupList _groups; 00181 00182 public: 00183 00184 explicit Mailbox(const string & connection = "", const string & name = "", 00185 const bool group_membership = true, 00186 const Timeout & timeout = ZeroTimeout, 00187 const Priority priority = Medium) SSRC_DECL_THROW(Error); 00188 00193 ~Mailbox() { 00194 if(!killed()) 00195 Spread::SP_disconnect(_mbox); 00196 } 00197 00202 const string & connection() const { 00203 return _connection; 00204 } 00205 00210 const string & name() const { 00211 return _name; 00212 } 00213 00220 descriptor_type descriptor() const { 00221 return _mbox; 00222 } 00223 00228 const string & private_group() const { 00229 return _private_group; 00230 } 00231 00238 bool group_membership() const { 00239 return _group_membership; 00240 } 00241 00249 void set_drop_receive(const bool drop = true) { 00250 _drop_receive = drop; 00251 } 00252 00257 bool drop_receive() const { 00258 return _drop_receive; 00259 } 00260 00267 void join(const string & group) SSRC_DECL_THROW(Error) { 00268 int result = Spread::SP_join(_mbox, group.c_str()); 00269 if(result != 0) 00270 throw Error(result); 00271 } 00272 00279 void leave(const string & group) SSRC_DECL_THROW(Error) { 00280 int result = Spread::SP_leave(_mbox, group.c_str()); 00281 if(result != 0) 00282 throw Error(result); 00283 } 00284 00285 #ifdef LIBSSRCSPREAD_ENABLE_MAILBOX_KILL 00286 00296 void kill() { 00297 Spread::SP_kill(_mbox); 00298 _killed = true; 00299 } 00300 #endif 00301 00310 bool killed() const { 00311 return _killed; 00312 } 00313 00322 unsigned int poll() const SSRC_DECL_THROW(Error) { 00323 int result = Spread::SP_poll(_mbox); 00324 if(result < 0) 00325 throw Error(result); 00326 return result; 00327 } 00328 00335 bool add_message_part(const void *data, const unsigned int size) { 00336 return _scatter.add(data, size); 00337 } 00338 00348 bool add_message_part(const Message & message) { 00349 return _scatter.add(message); 00350 } 00351 00357 void add_group(const string & group) { 00358 _groups.add(group); 00359 } 00360 00366 void add_groups(const GroupList & groups) { 00367 _groups.add(groups); 00368 } 00369 00378 string group(const unsigned int index) const { 00379 return _groups[index]; 00380 } 00381 00387 void copy_groups(GroupList & groups) { 00388 groups = _groups; 00389 } 00390 00395 unsigned int count_groups() const { 00396 return _groups.size(); 00397 } 00398 00402 void clear_groups() { 00403 _groups.clear(); 00404 } 00405 00410 unsigned int count_message_parts() const { 00411 return _scatter.count_message_parts(); 00412 } 00413 00417 void clear_message_parts() { 00418 _scatter.clear(); 00419 } 00420 00421 int send(const Message & message, const string & group) 00422 SSRC_DECL_THROW(Error); 00423 00424 int send(const Message & message, const GroupList & groups) 00425 SSRC_DECL_THROW(Error); 00426 00427 int send(const ScatterMessage & message, const GroupList & groups) 00428 SSRC_DECL_THROW(Error); 00429 00434 int send(const ScatterMessage & message, const string & group) 00435 SSRC_DECL_THROW(Error) 00436 { 00437 clear_groups(); 00438 add_group(group); 00439 return send(message, _groups); 00440 } 00441 00449 int send(const GroupList & groups, const BaseMessage::message_type type = 0, 00450 const BaseMessage::service_type service = BaseMessage::Safe) 00451 SSRC_DECL_THROW(Error) 00452 { 00453 _scatter.set_type(type); 00454 _scatter.set_service(service); 00455 return send(_scatter, groups); 00456 } 00457 00464 int send(const string & group, const BaseMessage::message_type type = 0, 00465 const BaseMessage::service_type service = BaseMessage::Safe) 00466 SSRC_DECL_THROW(Error) 00467 { 00468 clear_groups(); 00469 add_group(group); 00470 return send(_groups, type, service); 00471 } 00472 00479 int send(const BaseMessage::message_type type = 0, 00480 const BaseMessage::service_type service = BaseMessage::Safe) 00481 SSRC_DECL_THROW(Error) 00482 { 00483 return send(_groups, type, service); 00484 } 00485 00491 int send(const Message & message) SSRC_DECL_THROW(Error) { 00492 return send(message, _groups); 00493 } 00494 00500 int send(const ScatterMessage & message) SSRC_DECL_THROW(Error) { 00501 return send(message, _groups); 00502 } 00503 00504 int receive(ScatterMessage & message, GroupList & groups) 00505 SSRC_DECL_THROW(BufferSizeError, Error); 00506 00513 int receive(Message & message, GroupList & groups) 00514 SSRC_DECL_THROW(BufferSizeError, Error) 00515 { 00516 clear_message_parts(); 00517 add_message_part(message); 00518 return receive(_scatter, groups); 00519 } 00520 00528 int receive(GroupList & groups) SSRC_DECL_THROW(BufferSizeError, Error) { 00529 return receive(_scatter, groups); 00530 } 00531 00539 int receive(Message & message) SSRC_DECL_THROW(BufferSizeError, Error) { 00540 return receive(message, _groups); 00541 } 00542 00550 int receive(ScatterMessage & message) 00551 SSRC_DECL_THROW(BufferSizeError, Error) 00552 { 00553 return receive(message, _groups); 00554 } 00555 00563 int receive() SSRC_DECL_THROW(BufferSizeError, Error) { 00564 return receive(_scatter, _groups); 00565 } 00566 00567 }; 00568 00569 __END_NS_SSRC_SPREAD 00570 00571 #endif
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.
Copyright © 2011 Savarese Software Research Corporation. All rights reserved