00001 00002 /*************************************************************************** 00003 * transceiver.cpp - Fawkes transceiver 00004 * 00005 * Created: Tue Nov 21 23:46:13 2006 00006 * Copyright 2006 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #include <netcomm/fawkes/transceiver.h> 00025 #include <netcomm/fawkes/message.h> 00026 #include <netcomm/fawkes/message_queue.h> 00027 #include <netcomm/socket/stream.h> 00028 #include <netcomm/utils/exceptions.h> 00029 00030 #include <netinet/in.h> 00031 #include <cstdlib> 00032 00033 namespace fawkes { 00034 00035 /** @class FawkesNetworkTransceiver transceiver.h <netcomm/fawkes/transceiver.h> 00036 * Fawkes Network Transceiver. 00037 * Utility class that provides methods to send and receive messages via 00038 * the network. Operates on message queues and a given socket. 00039 * 00040 * @ingroup NetComm 00041 * @author Tim Niemueller 00042 */ 00043 00044 /** Send messages. 00045 * @param s socket over which the data shall be transmitted. 00046 * @param msgq message queue that contains the messages that have to be sent 00047 * @exception ConnectionDiedException Thrown if any error occurs during the 00048 * operation since for any error the conncetion is considered dead. 00049 */ 00050 void 00051 FawkesNetworkTransceiver::send(StreamSocket *s, FawkesNetworkMessageQueue *msgq) 00052 { 00053 msgq->lock(); 00054 try { 00055 while ( ! msgq->empty() ) { 00056 FawkesNetworkMessage *m = msgq->front(); 00057 m->pack(); 00058 const fawkes_message_t &f = m->fmsg(); 00059 unsigned int payload_size = m->payload_size(); 00060 s->write(&(f.header), sizeof(f.header)); 00061 s->write(f.payload, payload_size); 00062 m->unref(); 00063 msgq->pop(); 00064 } 00065 } catch (SocketException &e) { 00066 msgq->unlock(); 00067 throw ConnectionDiedException("Write failed"); 00068 } 00069 msgq->unlock(); 00070 } 00071 00072 00073 /** Receive data. 00074 * This method receives all messages currently available from the network, or 00075 * a limited number depending on max_num_msgs. If max_num_msgs is 0 then all 00076 * messages are read. Note that on a busy connection this may cause recv() to 00077 * never return! The default is to return after 8 messages. 00078 * The messages are stored in the supplied message queue. 00079 * @param s socket to gather messages from 00080 * @param msgq message queue to store received messages in 00081 * @param max_num_msgs maximum number of messages to read from stream in one go. 00082 * @exception ConnectionDiedException Thrown if any error occurs during the 00083 * operation since for any error the conncetion is considered dead. 00084 */ 00085 void 00086 FawkesNetworkTransceiver::recv(StreamSocket *s, FawkesNetworkMessageQueue *msgq, 00087 unsigned int max_num_msgs) 00088 { 00089 msgq->lock(); 00090 00091 try { 00092 unsigned int num_msgs = 0; 00093 while ( s->available() && (num_msgs++ < max_num_msgs) ) { 00094 fawkes_message_t msg; 00095 s->read(&(msg.header), sizeof(msg.header)); 00096 00097 unsigned int payload_size = ntohl(msg.header.payload_size); 00098 00099 if ( payload_size > 0 ) { 00100 msg.payload = malloc(payload_size); 00101 s->read(msg.payload, payload_size); 00102 } else { 00103 msg.payload = NULL; 00104 } 00105 00106 FawkesNetworkMessage *m = new FawkesNetworkMessage(msg); 00107 msgq->push(m); 00108 } 00109 } catch (SocketException &e) { 00110 msgq->unlock(); 00111 throw ConnectionDiedException("Read failed"); 00112 } 00113 msgq->unlock(); 00114 } 00115 00116 } // end namespace fawkes