blob: aaf3d067913d943f1cccb4006af3810f8676a519 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonbc53c522013-07-17 17:11:48 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonbc53c522013-07-17 17:11:48 -07005 * See COPYING for copyright and distribution information.
6 */
7
8#include <stdexcept>
Jeff Thompson53430e02013-10-23 10:12:41 -07009#include <stdlib.h>
Jeff Thompson25b4e612013-10-10 16:03:24 -070010#include <ndn-cpp/face.hpp>
11#include "../c/transport/udp-transport.h"
12#include "../c/encoding/binary-xml-element-reader.h"
Jeff Thompsonbc53c522013-07-17 17:11:48 -070013#include "../c/util/ndn_realloc.h"
Jeff Thompson25b4e612013-10-10 16:03:24 -070014#include <ndn-cpp/transport/udp-transport.hpp>
Jeff Thompsonbc53c522013-07-17 17:11:48 -070015
16using namespace std;
17
18namespace ndn {
19
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080020UdpTransport::UdpTransport(const char *host, unsigned short port/* = 6363*/)
21 : host_(host), port_(port)
22 , isConnected_(false), transport_(new struct ndn_UdpTransport), elementReader_(new struct ndn_BinaryXmlElementReader)
Jeff Thompson25b4e612013-10-10 16:03:24 -070023{
24 ndn_UdpTransport_initialize(transport_.get());
25 elementReader_->partialData.array = 0;
26}
27
Jeff Thompson0050abe2013-09-17 12:50:25 -070028void
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080029UdpTransport::connect(ElementListener& elementListener)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070030{
31 ndn_Error error;
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080032 if ((error = ndn_UdpTransport_connect(transport_.get(), (char *)host_.c_str(), port_)))
Jeff Thompson4affbf52013-10-18 14:36:46 -070033 throw runtime_error(ndn_getErrorString(error));
Jeff Thompsonbc53c522013-07-17 17:11:48 -070034
35 // TODO: This belongs in the socket listener.
Jeff Thompson97223af2013-09-24 17:01:27 -070036 const size_t initialLength = 1000;
Jeff Thompson10e34382013-08-22 13:34:46 -070037 // Automatically cast elementReader_ to (struct ndn_ElementListener *)
Jeff Thompsond1427fb2013-08-29 17:20:32 -070038 ndn_BinaryXmlElementReader_initialize
Jeff Thompson25b4e612013-10-10 16:03:24 -070039 (elementReader_.get(), &elementListener, (uint8_t *)malloc(initialLength), initialLength, ndn_realloc);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070040
Jeff Thompsona4056972013-08-22 11:52:21 -070041 isConnected_ = true;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070042}
43
Jeff Thompson0050abe2013-09-17 12:50:25 -070044void
Jeff Thompson97223af2013-09-24 17:01:27 -070045UdpTransport::send(const uint8_t *data, size_t dataLength)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070046{
47 ndn_Error error;
Jeff Thompson25b4e612013-10-10 16:03:24 -070048 if ((error = ndn_UdpTransport_send(transport_.get(), (uint8_t *)data, dataLength)))
Jeff Thompson4affbf52013-10-18 14:36:46 -070049 throw runtime_error(ndn_getErrorString(error));
Jeff Thompsonbc53c522013-07-17 17:11:48 -070050}
51
Jeff Thompson0050abe2013-09-17 12:50:25 -070052void
53UdpTransport::processEvents()
Jeff Thompsonbc53c522013-07-17 17:11:48 -070054{
Jeff Thompson432c8be2013-08-09 16:16:08 -070055 int receiveIsReady;
56 ndn_Error error;
Jeff Thompson25b4e612013-10-10 16:03:24 -070057 if ((error = ndn_UdpTransport_receiveIsReady(transport_.get(), &receiveIsReady)))
Jeff Thompson4affbf52013-10-18 14:36:46 -070058 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson432c8be2013-08-09 16:16:08 -070059 if (!receiveIsReady)
60 return;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070061
Jeff Thompson10ad12a2013-09-24 16:19:11 -070062 uint8_t buffer[8000];
Jeff Thompson97223af2013-09-24 17:01:27 -070063 size_t nBytes;
Jeff Thompson25b4e612013-10-10 16:03:24 -070064 if ((error = ndn_UdpTransport_receive(transport_.get(), buffer, sizeof(buffer), &nBytes)))
Jeff Thompson4affbf52013-10-18 14:36:46 -070065 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson432c8be2013-08-09 16:16:08 -070066
Jeff Thompson25b4e612013-10-10 16:03:24 -070067 ndn_BinaryXmlElementReader_onReceivedData(elementReader_.get(), buffer, nBytes);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070068}
69
Jeff Thompson0050abe2013-09-17 12:50:25 -070070bool
71UdpTransport::getIsConnected()
Jeff Thompsona4056972013-08-22 11:52:21 -070072{
73 return isConnected_;
74}
75
Jeff Thompson0050abe2013-09-17 12:50:25 -070076void
77UdpTransport::close()
Jeff Thompson57963882013-08-05 16:01:25 -070078{
79 ndn_Error error;
Jeff Thompson25b4e612013-10-10 16:03:24 -070080 if ((error = ndn_UdpTransport_close(transport_.get())))
Jeff Thompson4affbf52013-10-18 14:36:46 -070081 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson57963882013-08-05 16:01:25 -070082}
83
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070084UdpTransport::~UdpTransport()
85{
Jeff Thompson25b4e612013-10-10 16:03:24 -070086 if (elementReader_->partialData.array)
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070087 // Free the memory allocated in connect.
Jeff Thompson25b4e612013-10-10 16:03:24 -070088 free(elementReader_->partialData.array);
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070089}
90
Jeff Thompsonbc53c522013-07-17 17:11:48 -070091}