Files
unison/src/node/socket.cc

367 lines
7.0 KiB
C++
Raw Normal View History

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2006 Georgia Tech Research Corporation
* 2007 INRIA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors: George F. Riley<riley@ece.gatech.edu>
* Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
2007-09-12 16:54:21 -07:00
2007-09-13 21:36:32 -07:00
#include "ns3/log.h"
#include "ns3/packet.h"
#include "node.h"
2007-09-12 16:54:21 -07:00
#include "socket.h"
#include "socket-factory.h"
2008-07-02 03:16:36 -07:00
#include <limits>
2007-09-12 16:54:21 -07:00
2007-09-13 21:36:32 -07:00
NS_LOG_COMPONENT_DEFINE ("Socket");
namespace ns3 {
2008-05-10 21:27:32 -07:00
Socket::Socket (void)
{
NS_LOG_FUNCTION_NOARGS ();
}
Socket::~Socket ()
2007-09-12 16:54:21 -07:00
{
NS_LOG_FUNCTION_NOARGS ();
2007-09-12 16:54:21 -07:00
}
Ptr<Socket>
Socket::CreateSocket (Ptr<Node> node, TypeId tid)
{
Ptr<Socket> s;
Ptr<SocketFactory> socketFactory = node->GetObject<SocketFactory> (tid);
s = socketFactory->CreateSocket ();
NS_ASSERT (s != 0);
return s;
}
void
2007-09-12 16:54:21 -07:00
Socket::SetConnectCallback (
Callback<void, Ptr<Socket> > connectionSucceeded,
2008-06-16 16:25:52 -07:00
Callback<void, Ptr<Socket> > connectionFailed)
{
NS_LOG_FUNCTION_NOARGS ();
m_connectionSucceeded = connectionSucceeded;
m_connectionFailed = connectionFailed;
}
2007-09-12 16:54:21 -07:00
void
2007-09-12 16:54:21 -07:00
Socket::SetAcceptCallback (
Callback<bool, Ptr<Socket>, const Address &> connectionRequest,
2008-06-16 16:25:52 -07:00
Callback<void, Ptr<Socket>, const Address&> newConnectionCreated)
{
NS_LOG_FUNCTION_NOARGS ();
m_connectionRequest = connectionRequest;
m_newConnectionCreated = newConnectionCreated;
}
2007-09-12 16:54:21 -07:00
bool
Socket::SetDataSentCallback (Callback<void, Ptr<Socket>, uint32_t> dataSent)
{
NS_LOG_FUNCTION_NOARGS ();
m_dataSent = dataSent;
return true;
}
void
Socket::SetSendCallback (Callback<void, Ptr<Socket>, uint32_t> sendCb)
{
NS_LOG_FUNCTION_NOARGS ();
m_sendCb = sendCb;
}
2007-09-12 16:54:21 -07:00
void
2008-05-02 09:21:01 -07:00
Socket::SetRecvCallback (Callback<void, Ptr<Socket> > receivedData)
{
NS_LOG_FUNCTION_NOARGS ();
2008-05-02 09:21:01 -07:00
m_receivedData = receivedData;
}
int
Socket::Send (Ptr<Packet> p)
{
NS_LOG_FUNCTION_NOARGS ();
return Send (p, 0);
}
int
Socket::Send (const uint8_t* buf, uint32_t size, uint32_t flags)
2007-12-13 09:40:21 -05:00
{
NS_LOG_FUNCTION_NOARGS ();
2007-12-13 09:40:21 -05:00
Ptr<Packet> p;
if (buf)
{
p = Create<Packet> (buf, size);
}
else
{
p = Create<Packet> (size);
}
return Send (p, flags);
}
int
Socket::SendTo (const uint8_t* buf, uint32_t size, uint32_t flags,
const Address &toAddress)
{
NS_LOG_FUNCTION_NOARGS ();
Ptr<Packet> p;
if(buf)
{
p = Create<Packet> (buf, size);
}
else
{
p = Create<Packet> (size);
}
return SendTo (p, flags, toAddress);
2007-12-13 09:40:21 -05:00
}
Ptr<Packet>
Socket::Recv (void)
{
NS_LOG_FUNCTION_NOARGS ();
return Recv (std::numeric_limits<uint32_t>::max(), 0);
}
2008-05-26 11:22:42 -07:00
int
Socket::Recv (uint8_t* buf, uint32_t size, uint32_t flags)
{
NS_LOG_FUNCTION_NOARGS ();
2008-05-26 11:22:42 -07:00
Ptr<Packet> p = Recv (size, flags); // read up to "size" bytes
if (p == 0)
{
return 0;
}
2008-05-26 11:22:42 -07:00
memcpy (buf, p->PeekData (), p->GetSize());
return p->GetSize ();
}
Ptr<Packet>
Socket::RecvFrom (Address &fromAddress)
2007-12-13 09:40:21 -05:00
{
NS_LOG_FUNCTION_NOARGS ();
return RecvFrom (std::numeric_limits<uint32_t>::max(), 0, fromAddress);
}
int
Socket::RecvFrom (uint8_t* buf, uint32_t size, uint32_t flags,
Address &fromAddress)
{
NS_LOG_FUNCTION_NOARGS ();
Ptr<Packet> p = RecvFrom (size, flags, fromAddress);
if (p == 0)
2007-12-13 09:40:21 -05:00
{
return 0;
2007-12-13 09:40:21 -05:00
}
memcpy (buf, p->PeekData (), p->GetSize());
return p->GetSize ();
2007-12-13 09:40:21 -05:00
}
2007-09-12 16:54:21 -07:00
void
Socket::NotifyConnectionSucceeded (void)
{
NS_LOG_FUNCTION_NOARGS ();
if (!m_connectionSucceeded.IsNull ())
{
m_connectionSucceeded (this);
}
}
2007-09-12 16:54:21 -07:00
void
Socket::NotifyConnectionFailed (void)
{
NS_LOG_FUNCTION_NOARGS ();
if (!m_connectionFailed.IsNull ())
{
m_connectionFailed (this);
}
}
2007-09-12 16:54:21 -07:00
bool
Socket::NotifyConnectionRequest (const Address &from)
{
NS_LOG_FUNCTION_NOARGS ();
if (!m_connectionRequest.IsNull ())
{
return m_connectionRequest (this, from);
}
else
{
2008-03-14 16:37:02 -07:00
// accept all incoming connections by default.
// this way people writing code don't have to do anything
// special like register a callback that returns true
// just to get incoming connections
return true;
}
}
2007-09-12 16:54:21 -07:00
void
Socket::NotifyNewConnectionCreated (Ptr<Socket> socket, const Address &from)
{
NS_LOG_FUNCTION_NOARGS ();
if (!m_newConnectionCreated.IsNull ())
{
m_newConnectionCreated (socket, from);
}
}
2007-09-12 16:54:21 -07:00
void
Socket::NotifyDataSent (uint32_t size)
{
NS_LOG_FUNCTION_NOARGS ();
if (!m_dataSent.IsNull ())
{
m_dataSent (this, size);
}
}
2007-09-12 16:54:21 -07:00
void
Socket::NotifySend (uint32_t spaceAvailable)
{
NS_LOG_FUNCTION_NOARGS ();
if (!m_sendCb.IsNull ())
{
m_sendCb (this, spaceAvailable);
}
}
void
Socket::NotifyDataRecv (void)
{
NS_LOG_FUNCTION_NOARGS ();
2008-05-02 09:21:01 -07:00
if (!m_receivedData.IsNull ())
{
2008-05-02 09:21:01 -07:00
m_receivedData (this);
}
}
2008-05-21 22:40:18 -07:00
/***************************************************************
* Socket Tags
***************************************************************/
SocketAddressTag::SocketAddressTag ()
{
}
void
SocketAddressTag::SetAddress (Address addr)
{
m_address = addr;
}
Address
SocketAddressTag::GetAddress (void) const
{
return m_address;
}
2008-05-17 11:15:02 -07:00
2008-05-21 22:40:18 -07:00
TypeId
SocketAddressTag::GetTypeId (void)
2008-05-17 11:15:02 -07:00
{
static TypeId tid = TypeId ("ns3::SocketAddressTag")
2008-05-21 22:40:18 -07:00
.SetParent<Tag> ()
.AddConstructor<SocketAddressTag> ()
2008-05-21 22:40:18 -07:00
;
return tid;
2008-05-17 11:15:02 -07:00
}
2008-05-21 22:40:18 -07:00
TypeId
SocketAddressTag::GetInstanceTypeId (void) const
2008-05-17 11:15:02 -07:00
{
2008-05-21 22:40:18 -07:00
return GetTypeId ();
2008-05-17 11:15:02 -07:00
}
uint32_t
SocketAddressTag::GetSerializedSize (void) const
{
return m_address.GetSerializedSize ();
2008-05-17 11:15:02 -07:00
}
void
SocketAddressTag::Serialize (TagBuffer i) const
2008-05-17 11:15:02 -07:00
{
2008-05-28 13:03:29 -07:00
m_address.Serialize (i);
2008-05-17 11:15:02 -07:00
}
void
SocketAddressTag::Deserialize (TagBuffer i)
2008-05-17 11:15:02 -07:00
{
2008-05-28 13:03:29 -07:00
m_address.Deserialize (i);
2008-05-17 11:15:02 -07:00
}
void
SocketAddressTag::Print (std::ostream &os) const
2008-05-17 11:15:02 -07:00
{
os << "address=" << m_address;
2008-05-17 11:15:02 -07:00
}
2008-05-21 22:40:18 -07:00
SocketIpTtlTag::SocketIpTtlTag ()
2008-05-17 11:15:02 -07:00
{
}
void
SocketIpTtlTag::SetTtl (uint8_t ttl)
{
m_ttl = ttl;
}
uint8_t
SocketIpTtlTag::GetTtl (void) const
{
return m_ttl;
}
2008-05-21 22:40:18 -07:00
TypeId
SocketIpTtlTag::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::SocketIpTtlTag")
.SetParent<Tag> ()
.AddConstructor<SocketIpTtlTag> ()
;
return tid;
}
TypeId
SocketIpTtlTag::GetInstanceTypeId (void) const
{
return GetTypeId ();
}
2008-05-21 22:40:18 -07:00
uint32_t
SocketIpTtlTag::GetSerializedSize (void) const
{
return 1;
2008-05-21 22:40:18 -07:00
}
void
SocketIpTtlTag::Serialize (TagBuffer i) const
{
i.WriteU8 (m_ttl);
2008-05-21 22:40:18 -07:00
}
void
SocketIpTtlTag::Deserialize (TagBuffer i)
{
m_ttl = i.ReadU8 ();
2008-05-21 22:40:18 -07:00
}
void
SocketIpTtlTag::Print (std::ostream &os) const
{
os << "Ttl=" << (uint32_t) m_ttl;
}
2008-05-21 22:40:18 -07:00
}//namespace ns3