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

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