eventdispatcher.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "eventdispatcher.h"
  19. #include <log4cxx/logger.h>
  20. static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("hedwig."__FILE__));
  21. using namespace Hedwig;
  22. EventDispatcher::EventDispatcher() : service(), dummy_work(NULL), t(NULL) {
  23. }
  24. void EventDispatcher::run_forever() {
  25. LOG4CXX_DEBUG(logger, "Starting event dispatcher");
  26. while (true) {
  27. try {
  28. service.run();
  29. break;
  30. } catch (std::exception &e) {
  31. LOG4CXX_ERROR(logger, "Exception in dispatch handler. " << e.what());
  32. }
  33. }
  34. LOG4CXX_DEBUG(logger, "Event dispatcher done");
  35. }
  36. void EventDispatcher::start() {
  37. if (t) {
  38. return;
  39. }
  40. dummy_work = new boost::asio::io_service::work(service);
  41. t = new boost::thread(boost::bind(&EventDispatcher::run_forever, this));
  42. }
  43. void EventDispatcher::stop() {
  44. if (!t) {
  45. return;
  46. }
  47. delete dummy_work;
  48. dummy_work = NULL;
  49. service.stop();
  50. t->join();
  51. delete t;
  52. t = NULL;
  53. }
  54. EventDispatcher::~EventDispatcher() {
  55. delete dummy_work;
  56. }
  57. boost::asio::io_service& EventDispatcher::getService() {
  58. return service;
  59. }