1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #include <sys/types.h>
38 #include <sys/socket.h>
41 #include <anna/core/tracing/Logger.hpp>
42 #include <anna/core/DataBlock.hpp>
43 #include <anna/config/defines.hpp>
44 #include <anna/core/functions.hpp>
46 #include <anna/comm/DatagramSocket.hpp>
51 comm::DatagramSocket::DatagramSocket(const comm::DatagramSocket::Mode mode, const comm::INetAddress& address, comm::TransportFactory* transportFactory) :
52 comm::ClientSocket(transportFactory, comm::Socket::Domain::Inet, comm::Socket::Type::Datagram),
55 a_localAccessPoint = address;
57 a_remoteAccessPoint = address;
60 void comm::DatagramSocket::connect()
61 throw(RuntimeException) {
62 Guard guard(*this, "comm::DatagramSocket::connect");
63 anna_socket_assert(isConnected() == true, "Already connected");
65 if (Socket::isOpened() == false) {
68 setBlockingMode(false);
71 // Hace el bind a la localAddress.
72 if (a_mode == ReadOnly && Socket::isBound() == false)
75 activate(Status::Connected);
77 string msg("comm::DatagramSocket::connect | ");
79 Logger::debug(msg, ANNA_FILE_LOCATION);
83 void comm::DatagramSocket::do_write(const DataBlock& message)
84 throw(RuntimeException) {
87 a_remoteAccessPoint.translate(*this, s, len);
89 anna_signal_shield(r, sendto(Socket::a_fd, message.getData(), message.getSize(), 0, s, len));
92 const int xerrno = errno;
93 string msg(asString());
94 msg += " | Cannot be sent";
95 throw RuntimeException(msg, xerrno, ANNA_FILE_LOCATION);
99 string msg("comm::DatagramSocket::do_write | ");
101 msg += functions::asText(" | Sent: ", message);
102 Logger::write(Logger::Debug, msg, ANNA_FILE_LOCATION)
106 int comm::DatagramSocket::do_read(const char* data, const int maxSize)
107 throw(RuntimeException) {
109 anna_signal_shield(result, recvfrom(Socket::a_fd, (void*) data, maxSize, 0, (sockaddr*) NULL, NULL));
112 const int xerrno = errno;
113 string msg(asString());
114 msg += " | Cannot be received";
115 throw RuntimeException(msg, xerrno, ANNA_FILE_LOCATION);
119 string msg("comm::DatagramSocket::do_read | ");
121 msg += functions::asText(" | N-bytes", result);
122 msg += functions::asText(" | Received: ", DataBlock(data, result, false));
123 Logger::write(Logger::Debug, msg, ANNA_FILE_LOCATION)