source: source/ariba/utility/transport/asio/asio_io_service.cpp@ 5435

Last change on this file since 5435 was 5284, checked in by mies, 15 years ago

+ added new transport modules and adapted ariba to them
+ exchange endpoint descriptors an link establishment
+ clean up of base communication
+ link establishment with in the presence of multiple endpoints
+ local discovery for ipv6, ipv4 and bluetooth mac addresses

File size: 2.1 KB
RevLine 
[5284]1// Internal version: Please do not publish!
2// (... until released under FreeBSD-like license *g*)
3// Code: Sebastian Mies <mies@tm.uka.de>
4
5#include "asio_io_service.h"
6
7#include <boost/asio.hpp>
8#include <boost/thread.hpp>
9
10namespace ariba {
11namespace transport {
12namespace detail {
13
14using namespace boost::asio;
15using namespace std;
16
17asio_io_service* asio_io_service::singleton = NULL;
18
19//#define DBG(x) cout << x << endl;
20#define DBG(x)
21
22void asio_io_service::operator ()() {
23 running = true;
24 DBG("io_service started");
25 boost::asio::io_service::work work(*service);
26 service->run();
27 DBG("io_service stopped");
28 if (destroy) {
29 delete singleton;
30 singleton = NULL;
31 DBG(cout << "asio io_service singleton destroyed" << endl);
32 }
33 running = false;
34}
35
36asio_io_service::asio_io_service() :
37 references(1), running(false), destroy(false), thread(NULL), service(NULL) {
38 service = new io_service();
39}
40
41asio_io_service::~asio_io_service() {
42 if (thread != NULL) delete thread;
43 if (service != NULL) delete service;
44 thread = NULL;
45 service = NULL;
46}
47
48void asio_io_service::internal_start() {
49 if (!running) {
50 if (thread != NULL) delete thread;
51 thread = new boost::thread(boost::ref(*this));
52 }
53}
54
55void asio_io_service::internal_stop() {
56 service->stop();
57 singleton->running = false;
58}
59
60io_service& asio_io_service::alloc() {
61 if (singleton != NULL) {
62 DBG("new asio io_service reference");
63 singleton->references++;
64 return *singleton->service;
65 } else {
66 DBG("creating new asio io_service singleton");
67 singleton = new asio_io_service();
68 return *singleton->service;
69 }
70}
71
72void asio_io_service::free() {
73 if (singleton != NULL) {
74 DBG("decreasing asio io_service reference");
75 singleton->references--;
76 if (singleton->references == 0) {
77 DBG("request asio io_service destruction");
78 if (singleton->running == false) {
79 delete singleton;
80 singleton = NULL;
81 DBG("asio io_service singleton destroyed");
82 } else {
83 singleton->destroy = true;
84 singleton->service->stop();
85 }
86 }
87 }
88}
89
90void asio_io_service::start() {
91 singleton->internal_start();
92}
93
94void asio_io_service::stop() {
95 singleton->internal_stop();
96}
97
98
99}}} // namespace ariba::transport::detail
Note: See TracBrowser for help on using the repository browser.