listener.cpp

Go to the documentation of this file.
00001 /*
00002  * listener.cpp
00003  *
00004  *  Created on: Jan 10, 2012
00005  *      Author: sushil
00006  */
00007 
00008 #include <iostream>
00009 
00010 #include "DEBUG.h"
00011 #include <messages.h>
00012 #include <listener.h>
00013 #include <socket.h>
00014 //#include <utils.h>
00015 
00016 #include <boost/asio.hpp>
00017 #include <boost/thread.hpp>
00018 
00019 using namespace FastEcslent;
00020 using namespace boost::asio::ip;
00021 
00022 void Listener::init() { //initialize sockets, public
00023         DEBUG(std::cout << "Initializing Listener before starting listener thread. port: " << port << std::endl;)
00024         quit = false;
00025 /*
00026         boost::asio::socket_base::broadcast option(true);
00027         socket.set_option(option);
00028 
00029         , udp::endpoint(udp::v4(), port));
00030 */
00031 }
00032 
00033 /*
00034 void Listener::start() { //start thread, public
00035         //listenerThread = boost::thread(&Listener::run, this);
00036         //std::cout << "Started Net thread's run method" << listenerThread.get_id() << std::endl;
00037 }
00038 */
00039 void Listener::addMessage(Message *m) {
00040         DEBUG(std::cout << "Listener Added message of Type: " << (int) m->head.msgType << std::endl;)
00041         //printMessageHeader(m->head);
00042         recBuffer.push_back(m);
00043 }
00044 
00045 Message *Listener::dequeMessage(){
00046         DEBUG(std::cout << "Listener Dequeueing message" << std::endl;)
00047         if (recBuffer.empty()) {
00048                 return NULL;
00049         } else {
00050                 Message *m = recBuffer.front();
00051                 recBuffer.pop_front();
00052                 return m;
00053         }
00054 }
00055 
00056 void FastEcslent::Listener::run() { //start thread, public
00057         listenerThread = boost::thread(&FastEcslent::Listener::runThread, this);
00058         DEBUG(std::cout << "Running Listener thread: " << listenerThread.get_id() << std::endl;)
00059 }
00060 
00061 
00062 void FastEcslent::Listener::runThread(){ // run the netManaager thread, private (each tick)
00063         DEBUG(std::cout << "..........................................Running listener......................................" << std::endl;)
00064         try {
00065 
00066                 socket = makeUDPBroadcastSocket();
00067                 //boost::asio::socket_base::non_blocking_io command(true);
00068                 //socket->io_control(command);
00069 
00070                 //udp::endpoint anyEndpoint(udp::v4(), (unsigned short) port);
00071                 //udp::endpoint any(boost::asio::ip::address_v4::any(), port);
00072                 //DEBUG(std::cout << "Listener Endpoint Address: " << anyEndpoint.address().to_string() << " Port: " << anyEndpoint.port() << std::endl;)
00073                 //DEBUG(std::cout << "Listener Any Address: " << any.address().to_string() << " Port: " << anyEndpoint.port() << std::endl;)
00074                 //socket->bind(boost::asio::ip::address_v4::any());
00075                 socket->bind(udp::endpoint(boost::asio::ip::address_v4::broadcast( ), port));
00076                 DEBUG(std::cout << "Listener Bound Socket" << std::endl;)
00077 
00078                 udp::endpoint recFrom;
00079                 while(!quit){
00080                         DEBUG(std::cout << "Running Listener with id: " << listenerThread.get_id() << std::endl;)
00081                         size_t len;
00082                         Message *m = new Message;
00083                         len = socket->receive_from(boost::asio::buffer((char *) m, MaxMessageSize), recFrom);
00084                         if (recFrom != myIP ) {
00085                                 DEBUG(std::cout << "Listener got message with length: " << len << " from " << recFrom.address() << std::endl;)
00086                                 DEBUG(std::cout << "Type: " << (int) m->head.msgType << std::endl;)
00087                                 addMessage((Message *) m);
00088                         }
00089                 }//end while
00090                 } catch (std::exception& e) {
00091                         DEBUG(std::cerr << "Listener Exception: " << e.what() << std::endl;)
00092                         //quit = true;
00093                         return;
00094                 }
00095 
00096                 DEBUG(std::cout << "..........................................Listener Dead......................................" << std::endl;)
00097 }
00098 
00099 void FastEcslent::Listener::stop(){ // end thread, public
00100         DEBUG(std::cout << "Listener stopping..." << std::endl;)
00101         boost::mutex::scoped_lock scoped_lock(quitLock);
00102         quit = true;
00103 }
00104 
00105 void FastEcslent::Listener::kill(){ // end thread, public
00106         DEBUG(std::cout << "Listener killed..." << std::endl;)
00107         quit = true;
00108         listenerThread.interrupt();
00109 }
00110 
00111 void FastEcslent::Listener::join(){ // end thread, public
00112         DEBUG(std::cout << "Listener shutting down socket..." << quit << std::endl;)
00113         std::flush(std::cout);
00114         listenerThread.join();
00115         try {
00116                         boost::system::error_code ec;                   //socket->cancel(ec);
00117                         //socket->shutdown(boost::asio::ip::udp::socket::shutdown_receive, ec);
00118                         if(socket->is_open())
00119                                 socket->close(ec);
00120                         //socket->close();
00121                         if (ec){
00122                                 DEBUG(std::cerr << "Error in closing listener socket:" << ec.message() << std::endl;)
00123                         }
00124                 } catch (std::exception& e) {
00125                         DEBUG(std::cerr << "Exception: in closing listener socket:" << e.what() << std::endl;)
00126                 }
00127         DEBUG(std::cout << "Listener Socket shutdown." << std::endl;)
00128 }
00129 
00130 

Generated on Fri Dec 13 14:54:18 2013 for FastECSLent by  doxygen 1.5.4