First commit
[anna.git] / source / comm / DatagramSocket.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/uio.h>
40
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>
45
46 #include <anna/comm/DatagramSocket.hpp>
47
48 using namespace std;
49 using namespace anna;
50
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),
53       a_mode(mode) {
54    if (mode == ReadOnly)
55       a_localAccessPoint = address;
56    else
57       a_remoteAccessPoint = address;
58 }
59
60 void comm::DatagramSocket::connect()
61 throw(RuntimeException) {
62    Guard guard(*this, "comm::DatagramSocket::connect");
63    anna_socket_assert(isConnected() == true, "Already connected");
64
65    if (Socket::isOpened() == false) {
66       Socket::open();
67       getSocketOptions();
68       setBlockingMode(false);
69    }
70
71    // Hace el bind a la localAddress.
72    if (a_mode == ReadOnly && Socket::isBound() == false)
73       Socket::bind();
74
75    activate(Status::Connected);
76    LOGDEBUG(
77       string msg("comm::DatagramSocket::connect | ");
78       msg += asString();
79       Logger::debug(msg, ANNA_FILE_LOCATION);
80    );
81 }
82
83 void comm::DatagramSocket::do_write(const DataBlock& message)
84 throw(RuntimeException) {
85    sockaddr* s(NULL);
86    int len(0);
87    a_remoteAccessPoint.translate(*this, s, len);
88    int r;
89    anna_signal_shield(r, sendto(Socket::a_fd, message.getData(), message.getSize(), 0, s, len));
90
91    if (r < 0) {
92       const int xerrno = errno;
93       string msg(asString());
94       msg += " | Cannot be sent";
95       throw RuntimeException(msg, xerrno, ANNA_FILE_LOCATION);
96    }
97
98    LOGDEBUG(
99       string msg("comm::DatagramSocket::do_write | ");
100       msg += asString();
101       msg += functions::asText(" | Sent: ", message);
102       Logger::write(Logger::Debug, msg, ANNA_FILE_LOCATION)
103    );
104 }
105
106 int comm::DatagramSocket::do_read(const char* data, const int maxSize)
107 throw(RuntimeException) {
108    int result;
109    anna_signal_shield(result, recvfrom(Socket::a_fd, (void*) data, maxSize, 0, (sockaddr*) NULL, NULL));
110
111    if (result < 0) {
112       const int xerrno = errno;
113       string msg(asString());
114       msg += " | Cannot be received";
115       throw RuntimeException(msg, xerrno, ANNA_FILE_LOCATION);
116    }
117
118    LOGDEBUG(
119       string msg("comm::DatagramSocket::do_read | ");
120       msg += asString();
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)
124    );
125    return result;
126 }
127
128