NS3 scheduling simulation. Custom client/server application using ARQ on application layer. Problem in NS_LOG_FUNCTION results - sockets

I"m new in ns3 guys so please take it easy with me, this is a very basic question I hope someone can answer me.
I'm working on a custom application using UDP sockets in ns3. I wrote a simple two functions, one is sendPacket(Ptr packet) and the second is HandleReadOne() to output the content of the packet received. By creating 2 node and setup 1 of them as a source node and the second as a destination. when I send 1000 packet all the packets are sent by sendPacket function but in the output using NS_LOG_INFO in handleReadOne() function, I get only 3 packets in output!! Please guys HELP ((
the main class and the class where the two function are wrote is follow:
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/internet-module.h"
#include "source-application.h"
#include "sink-application.h"
#include "nack-data-tag.h"
#include "packet-data-tag.h"
#include "netDevice-setup.h"
#include "ns3/mobility-module.h"
#define PACKET_SIZE 1000;
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
uint32_t nNodes = 2;
double simTime = 60; //4 seconds
double interval = 0.5;
bool enablePcap = false;
cmd.AddValue ("t","Simulation Time", simTime);
cmd.AddValue ("i", "Broadcast interval in seconds", interval);
cmd.AddValue ("n", "Number of nodes", nNodes);
cmd.AddValue ("pcap", "Enable PCAP", enablePcap);
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create(nNodes);
MobilityHelper mobility;
mobility.SetMobilityModel ("ns3::ConstantVelocityMobilityModel");
mobility.Install(nodes);
for (uint32_t i=0 ; i<nodes.GetN(); i++)
{
Ptr<ConstantVelocityMobilityModel> cvmm = DynamicCast<ConstantVelocityMobilityModel> (nodes.Get(i)->GetObject<MobilityModel>());
cvmm->SetPosition ( Vector (20+i*5, 20+(i%2)*5, 0));
cvmm->SetVelocity ( Vector (10+((i+1)%2)*5,0,0) );
}
NetDeviceSetup setup;
NetDeviceContainer devices = setup.ConfigureDevices (nodes);
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer ifaces;
ifaces = address.Assign (devices);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Packet::EnablePrinting ();
Ptr <SourceApplication> appSink = CreateObject <SourceApplication> ();
Ptr <SourceApplication> appSource = CreateObject <SourceApplication> ();
appSink->SetStartTime (Seconds(1));
appSink->SetStopTime (Seconds (simTime));
Ipv4Address dest_ip ("10.1.1.2");
Ipv4Address my_ip("10.1.1.1");
appSink->SetDestinationAddr (dest_ip);
appSink->SetMyAddr (my_ip);
appSource->SetStartTime (Seconds(2));
appSource->SetStopTime (Seconds (simTime));
Ipv4Address dest_ip2 ("10.1.1.1");
appSource->SetDestinationAddr (dest_ip2);
Ipv4Address my_addr2 ("10.1.1.2");
appSource->SetMyAddr (my_addr2);
nodes.Get(0)->AddApplication (appSink);
nodes.Get(1)->AddApplication (appSource);
LogComponentEnable ("SourceApplication", LOG_LEVEL_ALL);
Simulator::Stop (Seconds (simTime));
Simulator::Run ();
Simulator::Destroy ();
}
===================SourceApplication class==================
#include "ns3/log.h"
#include "source-application.h"
#include "ns3/udp-socket.h"
#include "ns3/simulator.h"
#include "ns3/csma-net-device.h"
#include "ns3/ethernet-header.h"
#include "ns3/arp-header.h"
#include "ns3/ipv4-header.h"
#include "ns3/udp-header.h"
#include "packet-data-tag.h"
#define PURPLE_CODE "\033[95m"
#define CYAN_CODE "\033[96m"
#define TEAL_CODE "\033[36m"
#define BLUE_CODE "\033[94m"
#define GREEN_CODE "\033[32m"
#define YELLOW_CODE "\033[33m"
#define LIGHT_YELLOW_CODE "\033[93m"
#define RED_CODE "\033[91m"
#define BOLD_CODE "\033[1m"
#define END_CODE "\033[0m"
namespace ns3
{
NS_LOG_COMPONENT_DEFINE("SourceApplication");
NS_OBJECT_ENSURE_REGISTERED(SourceApplication);
TypeId
SourceApplication::GetTypeId()
{
static TypeId tid = TypeId("ns3::SourceApplication")
.AddConstructor<SourceApplication>()
.SetParent<Application>();
return tid;
}
TypeId
SourceApplication::GetInstanceTypeId() const
{
return SourceApplication::GetTypeId();
}
//Constructor
SourceApplication::SourceApplication()
{
m_port1 = 7777;
m_port2 = 9999;
m_packet_size = 1000;
m_number_of_packets_to_send = 50;
}
//Destructor
SourceApplication::~SourceApplication()
{
}
void SourceApplication::SetupReceiveSocket(Ptr<Socket> socket, Ipv4Address myAddr, uint16_t port)
{
InetSocketAddress local = InetSocketAddress(myAddr, port);
if (socket->Bind(local) == -1)
{
NS_FATAL_ERROR("Failed to bind socket");
}
}
void SourceApplication::SetDestinationAddr (Ipv4Address dest_addr){
m_destination_addr = dest_addr;
}
Ipv4Address SourceApplication::GetDestinationAddr (){
return m_destination_addr;
}
void SourceApplication::SetMyAddr (Ipv4Address my_addr){
m_my_addr = my_addr;
}
Ipv4Address SourceApplication::GetMyAddr (){
return m_my_addr;
}
void SourceApplication::StartApplication()
{
Ptr<UniformRandomVariable> rand = CreateObject<UniformRandomVariable> ();
m_random_offset = MicroSeconds (rand->GetValue(2,10));
NS_LOG_FUNCTION("Start application ... " << m_my_addr);
TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
m_recv_socket1 = Socket::CreateSocket(GetNode(), tid);
SetupReceiveSocket(m_recv_socket1, m_my_addr, m_port1);
m_send_socket = Socket::CreateSocket(GetNode(), tid);
//If the application of node source, then sendPacket will be scheduled
if(this->m_my_addr == ("10.1.1.2")){
for(int i = 0; i < 100; i++){
Ptr <Packet> packet = Create <Packet> (m_packet_size);
PacketDataTag tag;
tag.SetSeqNumber (i);
tag.SetTimestamp (Now ());
packet->AddPacketTag (tag);
this->SendPacket (packet);
}
}
m_recv_socket1->SetRecvCallback(MakeCallback(&SourceApplication::HandleReadOne, this));
}
void SourceApplication::HandleReadOne(Ptr<Socket> socket)
{
//NS_LOG_FUNCTION(this << socket);
Ptr<Packet> packet;
Address from;
Address localAddress;
PacketDataTag tag;
while ((packet = socket->RecvFrom(from)))
{
if(packet->PeekPacketTag (tag)){
NS_LOG_INFO(TEAL_CODE << "HandleReadOne: node " << GetNode ()->GetId ()<< " Received " << packet->GetSize() << " bytes"
<< " at time " << Now().GetSeconds ()<< " from " <<InetSocketAddress::ConvertFrom (from).GetIpv4 ()
<< " port " <<InetSocketAddress::ConvertFrom (from).GetPort () << " timestimp "
<< tag.GetTimestamp () << " seq-number: " << tag.GetSeqNumber () << END_CODE);
}else {
NS_LOG_INFO(PURPLE_CODE << "HandleReadOne: node " << GetNode ()->GetId ()<< " Received a Packet of size: " << packet->GetSize()
<< " at time " << Now().GetSeconds() << " from " <<InetSocketAddress::ConvertFrom (from).GetIpv4 ()
<< " port " <<InetSocketAddress::ConvertFrom (from).GetPort ()
<< END_CODE);
}
}
}
void SourceApplication::SendPacket(Ptr<Packet> packet)
{
//NS_LOG_FUNCTION (this << m_my_addr << m_port1 );
m_send_socket->Connect(InetSocketAddress(m_destination_addr, m_port1));
m_send_socket->Send(packet);
//Simulator::Schedule(Seconds (3), &SourceApplication::SendPacket, this, packet); //, dest_ip, 7777);
}
} // namespace ns3

Related

Segmentation Error: Help on the correct allocation memory when saving & loading binary files containing a specific structure from a class

This is my first time asking a question, so apologies if it is not done 100%:
I have a class which saves and loads a binary file with a specific data structure.
If the program creates an instance of the class, save the binary file, and creates another instance of the class to load/read the binary file consequently, everything seems 100% correct.
However, if I run the program to save the binary file and then run it again to load/read that binary file, it gives me a segmentation fault at the end.
The program still does everything it needs to do before the segmentation fault, except deconstructing the class at the end (obviously).
It looks like my allocation of the memory is not correct, but I am not sure where I am going wrong.
A simplified version of the code follow (also here: https://github.com/LenteDreyer/Tests.git )
Can someone see where I am going wrong?
class header file that save/loads the file
#ifndef __TESTS_MAP_HH__
#define __TESTS_MAP_HH__
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
typedef struct test_struct{
bool test_bool;
double test_double;
std::vector<double> test_vector;
} test_struct_t;
class map
{
private:
std::string m_path, m_str;
double m_double;
test_struct m_struct;
public:
map(const std::string& a_id);
void set_str(std::string a_str);
void set_double(double a_double);
void set_struct(test_struct a_struct);
void load_file();
void save_file() const;
void show_file() const;
~map();
};
#endif //__TESTS_MAP_HH__
class source file that save/loads the binary file
#include "map.hh"
map::map(const std::string& a_id)
{
m_path = a_id + ".bin";
m_str = "none";
m_double = 0.0;
m_struct = {false, 0.0};
}
void map::set_str(std::string a_str){
m_str = a_str;
}
void map::set_double(double a_double){
m_double = a_double;
}
void map::set_struct(test_struct a_struct){
m_struct = a_struct;
}
void map::load_file(){
std::ifstream l_inF;
l_inF.open(m_path.c_str(), std::ios::binary | std::ios::in);
l_inF.read((char*)&m_double,sizeof(double));
l_inF.read((char*)&m_struct,sizeof(test_struct_t));
size_t str_size;
l_inF.read((char*)&str_size, sizeof(str_size));
m_str.resize(str_size);
l_inF.read((char*)&m_str[0], str_size);
l_inF.close();
}
void map::save_file() const{
std::ofstream l_outF;
l_outF.open(m_path.c_str(), std::ios::binary | std::ios::out);
l_outF.write((char*)&m_double,sizeof(double));
l_outF.write((char*)&m_struct,sizeof(test_struct_t));
size_t str_size = m_str.size();
l_outF.write((char*)&str_size, sizeof(str_size));
l_outF.write((char*)&m_str[0], str_size);
l_outF.close();
}
void map::show_file() const{
std::cout << ">>-----------------------------------------------" << std::endl;
std::cout << ">> double : " << m_double << std::endl;
std::cout << ">> double : " << m_double << std::endl;
std::cout << ">> struct.bool : " << m_struct.test_bool << std::endl;
std::cout << ">> struct.double : " << m_struct.test_double << std::endl;
std::cout << ">> struct.vector : " << "size = " << m_struct.test_vector.size() << std::endl;
std::cout << ">> string : " << m_str << std::endl;
std::cout << ">>-----------------------------------------------" << std::endl;
}
map::~map(){}
main function case 1 works, and case 2 gives the segmentation fault.
#include "map.hh"
int main(int argc, char const *argv[])
{
std::string id = "mapfile";
int action = 0;
if(argc > 1) action = std::stoi(argv[1]);
else {
std::string input;
std::cout << "Enter case (1 or 2): ";
std::cin >> input;
action = std::stoi(input);
}
switch (action)
{
case 1:
{
// This works 100% (no errors and it saves/reads class perfectly)
std::vector<double> l_vect = {0.1, 0.0, 0.6};
test_struct save_struct = {true, 5.0, l_vect};
map test_save(id);
test_save.show_file();
test_save.set_double(8.0);
test_save.set_str("save this string");
test_save.set_struct(save_struct);
test_save.show_file();
test_save.save_file();
map test_load(id);
test_load.load_file();
test_load.show_file();
}
break;
case 2:
{
// gives segmentation error at the end of the program
map test_load(id);
test_load.load_file();
test_load.show_file();
}
break;
default:
break;
}
return 0;
}

How to use Boost library 1.55.0 to create a shared memory using a matlab application

I'm trying to use boost library 1.55.0 to create a shared memory.I have a example.cpp file which creates boost shared memory. Mexing of this file is successful,but throws the following exception while debugging "MATLAB.exe has triggered a breakpoint".Is this exception because of the version of the boost being incompatible with the matlab version? How to resolve this
`/* File : sfun_counter_cpp.cpp
* Abstract:
*
* Example of an C++ S-function which stores an C++ object in
* the pointers vector PWork.
*
* Copyright 1990-2000 The MathWorks, Inc.
*
*/
#include "iostream"
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
typedef struct
{
int outGate;
unsigned int outPin;
int inGate;
unsigned int inPin;
} wire;
typedef struct
{
unsigned int gateType;
unsigned int inPins;
unsigned int outPins;
std::vector<wire> inWires;
std::vector<wire> outWires;
} gate;
std::vector<gate> gates;
wire wiredata;
gate gatedata;
class counter {
double x;
public:
counter() {
x = 0.0;
}
double output(void) {
x = x + 1.0;
return x;
}
};
#ifdef __cplusplus
extern "C" { // use the C fcn-call standard for all functions
#endif // defined within this scope
#define S_FUNCTION_LEVEL 2
#define S_FUNCTION_NAME sfun_counter_cpp
/*
* Need to include simstruc.h for the definition of the SimStruct and
* its associated macro definitions.
*/
#include "simstruc.h"
/*====================*
* S-function methods *
*====================*/
/* Function: mdlInitializeSizes ===============================================
* Abstract:
* The sizes information is used by Simulink to determine the S-function
* block's characteristics (number of inputs, outputs, states, etc.).
*/
static void mdlInitializeSizes(SimStruct *S)
{
/* See sfuntmpl_doc.c for more details on the macros below */
ssSetNumSFcnParams(S, 1); /* Number of expected parameters */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
/* Return if number of expected != number of actual parameters */
return;
}
ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);
if (!ssSetNumInputPorts(S, 0)) return;
if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortWidth(S, 0, 1);
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 1); // reserve element in the pointers vector
ssSetNumModes(S, 0); // to store a C++ object
ssSetNumNonsampledZCs(S, 0);
ssSetOptions(S, 0);
}
/* Function: mdlInitializeSampleTimes
=========================================
* Abstract:
* This function is used to specify the sample time(s) for your
* S-function. You must register the same number of sample times as
* specified in ssSetNumSampleTimes.
*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, mxGetScalar(ssGetSFcnParam(S, 0)));
ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
/* Function: mdlStart
=======================================================
* Abstract:
* This function is called once at start of model execution. If you
* have states that should be initialized once, this is the place
* to do it.
*/
static void mdlStart(SimStruct *S)
{
ssGetPWork(S)[0] = (void *) new counter; // store new C++ object in
the
} // pointers vector
#endif /* MDL_START */
/* Function: mdlOutputs =======================================================
* Abstract:
* In this function, you compute the outputs of your S-function
* block. Generally outputs are placed in the output vector, ssGetY(S).
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
using namespace boost::interprocess;
counter *c = (counter *) ssGetPWork(S)[0]; // retrieve C++ object from
shared_memory_object::remove("MySharedMemory");
//create the shared memory
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
//create the allocators for the struct elements to be accessed as
vectors
typedef allocator<gate,
managed_shared_memory::segment_manager>gate_alloc;
typedef allocator<wire,
managed_shared_memory::segment_manager>inwire_alloc;
typedef allocator<wire,
managed_shared_memory::segment_manager>outwire_alloc;
//create a boost vector with an associated allocator to it
typedef vector<gate, gate_alloc>gate_vec;
typedef vector<wire, inwire_alloc>inwire_vec;
typedef vector<wire, outwire_alloc>outwire_vec;
//Initialize shared memory STL-compatible allocator
const gate_alloc alloc_inst(segment.get_segment_manager());
const inwire_alloc alloc_inst1(segment.get_segment_manager());
const outwire_alloc alloc_inst2(segment.get_segment_manager());
//construct the segment for pushing the data into it
gate_vec *gate_data = segment.construct<gate_vec>("gatedata")
(alloc_inst);
inwire_vec *inwire_data = segment.construct<inwire_vec>("inwiredata")
(alloc_inst1);
outwire_vec *outwire_data = segment.construct<outwire_vec>
("outwiredata")
(alloc_inst2);
//push the data into the vectors
wiredata.inGate = 10;
wiredata.inPin = 2;
wiredata.outGate = 1;
wiredata.outPin = 3;
inwire_data->push_back(wiredata);
outwire_data->push_back(wiredata);
gatedata.gateType = 1;
gatedata.inPins = 2;
gatedata.outPins = 3;
gate_data->push_back(gatedata);
real_T *y = ssGetOutputPortRealSignal(S,0); // the pointers vector
and use
y[0] = c->output(); // member functions of
the
} // object
/* Function: mdlTerminate
=====================================================
* Abstract:
* In this function, you should perform any actions that are necessary
* at the termination of a simulation. For example, if memory was
* allocated in mdlStart, this is the place to free it.
*/
static void mdlTerminate(SimStruct *S)
{
counter *c = (counter *) ssGetPWork(S)[0]; // retrieve and destroy C++
delete c; // object in the
termination
} // function
/*======================================================*
* See sfuntmpl_doc.c for the optional S-function methods *
*======================================================*/
/*=============================*
* Required S-function trailer *
*=============================*/
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file?*/
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
#ifdef __cplusplus
} // end of extern "C" scope
#endif
`
this is the sfunction which I have to mex , debug and run .Though the mexing of the above code snippet is successful ,it throws an exception "MATLAB.exe has triggered a breakpoint" while debugging.
I can give you an example. Unfortunately I cannot test it with windows, but I have tested it on a UNIX system. The main idea is the same. In this case it is shared memory from an external binary to a Matlab mex function.
The external binary is:
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
using namespace boost::interprocess;
const std::string payload("SHARED MEMORY CONTENT");
int main(void) {
shared_memory_object shm(open_or_create, "memory4mat" ,read_write);
shm.truncate(payload.size());
mapped_region mem(shm, read_write);
std::memcpy(mem.get_address(), payload.c_str(), mem.get_size());
do {
std::cout << '\n' << "Press a key to continue...";
} while (std::cin.get() != '\n');
shared_memory_object::remove("memory4mat");
return 0;
}
while the mex function is:
#include "mex.hpp"
#include "mexAdapter.hpp"
#include "MatlabDataArray.hpp"
#include <string>
#include <cstdlib>
#include "boost/interprocess/shared_memory_object.hpp"
#include "boost/interprocess/mapped_region.hpp"
using namespace boost::interprocess;
class MexFunction : public matlab::mex::Function {
public:
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
matlab::data::ArrayFactory factory;
shared_memory_object shm(open_only, "memory4mat", read_only);
mapped_region mem(shm, read_only);
std::string payload(static_cast<const char *>(mem.get_address()), mem.get_size());
outputs[0] = factory.createCharArray(payload);
outputs[1] = factory.createScalar<int16_t>(mem.get_size());
}
};
it uses the C++ Interface and Data API for Matlab. To compile the two example you need to add the boost include directory as compiler options (shared memory is a header only feature in boost).
The external binary create a shared memory that contains the string "SHARED MEMORY CONTENT", and waits an enter from the user to remove the shared memory object.
The mex files opens the shared memory if exist (if the shared memory do not exist an error is reported and handled in Matlab, which is one of the reasons why I prefer C++ api) and copy its content in a Matlab char array. The function returns two values, the first is the content of the shared memory, the second is the length of the shared memory (the mapper uses all the memory, set with truncate).
This simple example uses only basic features and should work on Unix and Windows system, but again I cannot test on win.
A more complete example
Let's try with a more complete example about shared memories and Matlab Mex files. Let us write a very simple external binary that allows us to create/delete/read/write a shared memory. This binary has a lot of stuff hardcoded for simplicity, such as the name of the memory file ("shmem"):
// File: share_server.cpp
// g++ share_server.cpp -o share_server
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
using namespace boost::interprocess;
static const std::size_t size = 20;
static const std::size_t wsize = 15;
static const char name[6] = "shmem";
static const char input[wsize] = "write in share";
char output[size];
inline void printHelp() {
std::cout << "Options:" << std::endl;
std::cout << " n) open a new 'shmem' memory" << std::endl;
std::cout << " d) delete a 'shmem' memory" << std::endl;
std::cout << " r) read from 'shmem' memory" << std::endl;
std::cout << " w) write to 'shmem' memory" << std::endl;
std::cout << " x) Exit" << std::endl;
}
inline void cmd_createShare() {
try {
shared_memory_object sm(create_only, name, read_write);
sm.truncate(size);
std::cout << "Shared object created" << std::endl;
} catch(std::exception & e) {
std::cout << "Create Error :: " << e.what() << std::endl;
}
}
inline void cmd_deleteShare() {
try {
shared_memory_object::remove(name);
std::cout << "Shared object deletetd" << std::endl;
} catch(std::exception & e) {
std::cout << "Delete Error:: " << e.what() << std::endl;
}
}
inline void cmd_readShare() {
try {
shared_memory_object sm(open_only, name, read_only);
mapped_region sh_mem(sm, read_only);
std::string ret(static_cast<const char *>(sh_mem.get_address()), sh_mem.get_size());
std::cout << ret << std::endl;
} catch(std::exception & e) {
std::cout << "Read Error:: " << e.what() << std::endl;
}
}
inline void cmd_writeShare() {
try {
shared_memory_object sm(open_only, name, read_write);
mapped_region sh_mem(sm, read_write);
std::memcpy(sh_mem.get_address(), input, wsize);
std::cout << "Write completed" << std::endl;
} catch(std::exception & e) {
std::cout << "Read Error:: " << e.what() << std::endl;
}
}
we can write 3 mex files (using the C++ api) in order to interact with the shared memory. The first one, the simplest, reads the content of the shared memory as a string and returns it to the Matlab workspace. The interface in Matlab syntax is something like:
function [value, read_size] = read_share(share_name)
...
end
and the C++ implementation is the following:
// File: read_share.cpp
#include "mex.hpp"
#include "mexAdapter.hpp"
#include "MatlabDataArray.hpp"
#include <string>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <exception>
#include "boost/interprocess/shared_memory_object.hpp"
#include "boost/interprocess/mapped_region.hpp"
using namespace boost::interprocess;
using namespace matlab::data;
class MexFunction : public matlab::mex::Function {
private:
std::shared_ptr<matlab::engine::MATLABEngine> engine;
ArrayFactory factory;
void throwError(std::string errorMessage) {
engine->feval(matlab::engine::convertUTF8StringToUTF16String("error"),
0, std::vector<Array>({ factory.createScalar(errorMessage) }));
}
uint64_t read_shared_memory(const std::string & name, std::string & ret_value) {
try {
shared_memory_object sm(open_only, name.c_str(), read_only);
mapped_region sh_mem(sm, read_only);
ret_value += std::string(static_cast<const char *>(sh_mem.get_address()), sh_mem.get_size());
return ret_value.size();
} catch(std::exception & e) {
throwError(std::string("Reading error: ") + std::string(e.what()));
}
return 0;
}
void checkArguments(matlab::mex::ArgumentList inputs, matlab::mex::ArgumentList outputs) {
if (inputs.size() != 1)
throwError("Input must be of size 1");
if (inputs[0].getType() != ArrayType::CHAR)
throwError("First element must be a matlab char array");
if (outputs.size() > 2)
throwError("Too many outputs (required 1)");
}
public:
MexFunction() {
engine = getEngine();
}
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
checkArguments(inputs, outputs);
const CharArray name_array = std::move(inputs[0]);
std::string name = name_array.toAscii();
std::string ret_string("");
uint64_t ret_size = read_shared_memory(name, ret_string);
outputs[0] = factory.createScalar(ret_string);
outputs[1] = factory.createScalar<uint64_t>(ret_size);
}
};
The second mex file is the write operation. It takes two input: the name of the shared memory and the string to write inside the memory. The mex checks the maximum size of the shared memory and stores no more than the available space. The function returns the bytes written in the The interface for the write function is something like:
function written_size = write_share(share_name, string)
...
end
and the implementation is:
// File: write_share.cpp
#include "mex.hpp"
#include "mexAdapter.hpp"
#include "MatlabDataArray.hpp"
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <exception>
#include "boost/interprocess/shared_memory_object.hpp"
#include "boost/interprocess/mapped_region.hpp"
using namespace boost::interprocess;
using namespace matlab::data;
class MexFunction : public matlab::mex::Function {
private:
std::shared_ptr<matlab::engine::MATLABEngine> engine;
ArrayFactory factory;
void throwError(std::string errorMessage) {
engine->feval(matlab::engine::convertUTF8StringToUTF16String("error"),
0, std::vector<Array>({ factory.createScalar(errorMessage) }));
}
uint64_t write_shared_memory(const std::string & name, const std::string & value) {
try {
shared_memory_object sm(open_only, name.c_str(), read_write);
mapped_region sh_mem(sm, read_write);
uint64_t size = std::min(value.size(), sh_mem.get_size());
std::memcpy(sh_mem.get_address(), value.c_str(), size);
return size;
} catch(std::exception & e) {
throwError(std::string("Reading error: ") + std::string(e.what()));
}
return 0;
}
void checkArguments(matlab::mex::ArgumentList inputs, matlab::mex::ArgumentList outputs) {
if (inputs.size() != 2)
throwError("Input must be of size 2");
if (inputs[0].getType() != ArrayType::CHAR)
throwError("First element must be a matlab char array");
if (inputs[1].getType() != ArrayType::CHAR)
throwError("Second element must be a matlab char array to save");
if (outputs.size() > 1)
throwError("Too many outputs (required 1)");
}
public:
MexFunction() {
engine = getEngine();
}
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
checkArguments(inputs, outputs);
const CharArray name_array = std::move(inputs[0]);
std::string name = name_array.toAscii();
const CharArray value_array = std::move(inputs[1]);
std::string value = value_array.toAscii();
uint64_t written = write_shared_memory(name, value);
outputs[0] = factory.createScalar<uint64_t>(written);
}
};
The last mex is the most complex and handles the creation and deletion of the shared memory. You will notice the presence of a destructor that handles the removal of shared memory when the mex is unloaded from Matlab. The interface takes a command in the form of "create" or "delete", a string with the name of the share and the size of the shared memory for creation (it must be an unsigned int - uint16(...)). The function returns the size of the shared memory (it should be equal to size):
function size_shmem = menage_mex(command, share_name, uint16(size))
...
end
the implementation is the following:
// File: menage_share.cpp
#include "mex.hpp"
#include "mexAdapter.hpp"
#include "MatlabDataArray.hpp"
#include <string>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <exception>
#include "boost/interprocess/shared_memory_object.hpp"
#include "boost/interprocess/mapped_region.hpp"
using namespace boost::interprocess;
using namespace matlab::data;
class MexFunction : public matlab::mex::Function {
private:
std::shared_ptr<matlab::engine::MATLABEngine> engine;
ArrayFactory factory;
std::vector<std::string> pool;
void throwError(std::string errorMessage) {
engine->feval(matlab::engine::convertUTF8StringToUTF16String("error"),
0, std::vector<Array>({ factory.createScalar(errorMessage) }));
}
uint64_t run_command(const std::string & cmd, const std::string & name, uint64_t size) {
if (cmd == "create")
return create_shared_memory(name, size);
if (cmd == "delete")
return delete_shared_memory(name, size);
throwError("The command is unknown");
return 0;
}
uint64_t create_shared_memory(const std::string & name, uint64_t size) {
bool in_pool = false;
for (const auto & el : pool) {
if (el == name) {
in_pool = true;
break;
}
}
if (in_pool) {
try {
shared_memory_object sm(open_only, name.c_str(), read_only);
mapped_region sm_reg(sm, read_only);
if (sm_reg.get_size() != size)
throwError("Memory already exist and it is of different size");
return 0;
} catch (std::exception & e) {
throwError(std::string("Cannot open existing shared memory (maybe already open?) :: ") + std::string(e.what()));
}
} else {
try {
shared_memory_object sm(create_only, name.c_str(), read_write);
sm.truncate(size);
pool.push_back(name);
return size;
} catch (std::exception & e) {
throwError(std::string("Cannot create shared memory [" + name + "] (maybe already open?) :: ") + std::string(e.what()));
}
}
return 0;
}
uint64_t delete_shared_memory(const std::string & name, uint64_t size) {
std::size_t in_pool = 0;
for (const auto & el : pool) {
if (el == name)
break;
in_pool++;
}
if (in_pool < pool.size()) {
shared_memory_object::remove(name.c_str());
pool.erase(pool.begin() + in_pool);
} else {
throwError("Shared memory [" + name + "] is not handled by this mex");
}
return 0;
}
void checkArguments(matlab::mex::ArgumentList inputs, matlab::mex::ArgumentList outputs) {
if (inputs.size() != 3)
throwError("Input must be of size 3");
if (inputs[0].getType() != ArrayType::CHAR)
throwError("First element must be a matlab char array");
if (inputs[1].getType() != ArrayType::CHAR)
throwError("Second element must be amatlab char array");
if (inputs[2].getType() != ArrayType::UINT64)
throwError("Third element must be a single uint64 integer");
if (outputs.size() > 1)
throwError("Too many outputs (required 1)");
}
void inputArguments(std::string & cmd, std::string & name, uint64_t & size, matlab::mex::ArgumentList inputs) {
const CharArray cmd_array = std::move(inputs[0]);
const CharArray name_array = std::move(inputs[1]);
const TypedArray<uint64_t> size_array = std::move(inputs[2]);
cmd = cmd_array.toAscii();
name = name_array.toAscii();
size = size_array[0];
}
public:
MexFunction() {
pool.clear();
engine = getEngine();
}
~MexFunction() {
for (const auto & el : pool) {
shared_memory_object::remove(el.c_str());
}
}
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
checkArguments(inputs, outputs);
std::string cmd, name;
uint64_t size;
inputArguments(cmd, name, size, inputs);
uint64_t ret = run_command(cmd, name, size);
outputs[0] = factory.createScalar<uint64_t>(ret);
}
};
To compile the mex you can use the following script:
MEX_OPT = ['-I', '/path/to/boost'];
MEX_SRC = { ...
'menage_share.cpp', ...
'read_share.cpp', ...
'write_share.cpp' ...
};
for i = 1:length(MEX_SRC)
mex(MEX_OPT, MEX_SRC{i});
end
!g++ share_server.cpp -o share_server
and you can test them as follows:
(MATLAB) | (TERMINAL)
>> menage_share('create', 'shmem', uint64(20)) |
<< 20 |
>> write_share('shmem', 'Hello there') | $ ./share_server
<< 11 | ( ... help message ... )
| << r
| >> Hello there

How to return to menu in Socket Client-Server

I've writen this code but it doesn't work correctly.
This is menu interface:
*************ENCODE_DECODE_BASE64**************
******* 1. Encode ********
******* 2. Decode ********
******* 3. Exit ********
***********************************************
When i choose 1, "Encode". The function runs but the process exits.
I want when I choose 1, the function to run and then after that, the menu to display again.
Can you help me?
Here is Clientcode:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1024
// read filename
char *inputString(int size){
int test=0;
char *s=(char*)malloc(size);
do{
if(test!=0){
printf("File not found !!!!!!");
}
fgets(s,size,stdin);
test++;
}while(strlen(s)<=1);
return strtok(s,"\n");
}
int main(int argc, char *argv[]){
int sockfd;
struct sockaddr serverAddr; //server address
char buff[1024];
struct sockaddr_in inAddr;
long int sentBytes,revedBytes;
sockfd=socket(AF_INET,SOCK_STREAM,0); // create socket
if (sockfd == -1)
{
printf("ERROR opening socket\n");
return 1;
}
printf("Socket done\n");
inAddr.sin_family=AF_INET; //default
inAddr.sin_port=htons(5500); // service port
inet_aton("127.0.0.1",&inAddr.sin_addr);
//connectting
if(connect(sockfd,(struct sockaddr *)&inAddr,sizeof(struct sockaddr))<0){
printf("Connect failed.\n");
return 1;
}
printf("Connection accepted\n");
char *FileName; // file input
char *Result; // file return
int choice;
do{
printf("\n*************ENCODE_DECODE_BASE64**************");
printf("\n******* 1. Encode ********");
printf("\n******* 2. Decode ********");
printf("\n******* 3. Exit ********");
printf("\n***********************************************\n");
printf("Choice: ");
choice = getchar();
while(getchar() !='\n');
switch(choice)
{
case '1':
//demo encode////////////////////////////////////////
send(sockfd,"encode",5,0); // send to server when choose 1
printf("File Encode : ");
FileName = inputString(20);
FILE *fpt = fopen(FileName,"r");
if(fpt==NULL){
printf("File not found");
return -1;
}
printf("File Result: ");
Result = inputString(20);
FILE *ft = fopen(Result,"w");
while(!feof(fpt)){
if (fgets(buff,MAX,fpt) != NULL ){
sentBytes=send(sockfd,buff,1024,0);
revedBytes=recv(sockfd,buff,1024,0);
fprintf(ft,"%s\n",buff);
}
}
printf("Encode done!thanks you !\n");
//close(sockfd);
fclose(fpt);fclose(ft);
return 0;
break;
//decode ///////////////////////////////////////////////
case '2':
send(sockfd,"decode",6,0);
printf("File Decode : ");
FileName = inputString(20);
FILE *fpt1 = fopen(FileName,"r");
if(fpt1==NULL){
printf("File not found");
return -1;
}
printf("File Result : ");
Result = inputString(20);
FILE *ft1 = fopen(Result,"w");
while(!feof(fpt1)){
if (fgets(buff,MAX,fpt1) != NULL ){
sentBytes=send(sockfd,buff,1024,0);
revedBytes=recv(sockfd,buff,1024,0);
fprintf(ft1,"%s",buff);
}
}
printf("Decode done ! thanks you !\n");
//close(sockfd);
fclose(fpt1);fclose(ft1);
return 0;
break;
///////////////////////////////////////////////////////
case '3':
printf("Thanks!\n");
break;
default: printf("wrong number, please try again!\n"); break;
//end choice///////////////////////////////////////
}
}while(choice!='3');
//end menu
//close(sockfd);
//return 0;
}
and here is ServerCode:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/* decodeblock - decode 4 '6-bit' characters into 3 8-bit binary bytes */
void decodeblock(unsigned char in[], char *clrstr) {
unsigned char out[4];
out[0] = in[0] << 2 | in[1] >> 4;
out[1] = in[1] << 4 | in[2] >> 2;
out[2] = in[2] << 6 | in[3] >> 0;
out[3] = '\0';
strncat(clrstr, out, sizeof(out));
}
void b64_decode(char *b64src, char *clrdst) {
int c, phase, i;
unsigned char in[4];
char *p;
clrdst[0] = '\0';
phase = 0; i=0;
while(b64src[i]) {
c = (int) b64src[i];
if(c == '=') {
decodeblock(in, clrdst);
break;
}
p = strchr(b64, c);
if(p) {
in[phase] = p - b64;
phase = (phase + 1) % 4;
if(phase == 0) {
decodeblock(in, clrdst);
in[0]=in[1]=in[2]=in[3]=0;
}
}
i++;
}
}
/* encodeblock - encode 3 8-bit binary bytes as 4 '6-bit' characters */
void encodeblock( unsigned char in[], char b64str[], int len ) {
unsigned char out[5];
out[0] = b64[ in[0] >> 2 ];
out[1] = b64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
out[2] = (unsigned char) (len > 1 ? b64[ ((in[1] & 0x0f) << 2) |
((in[2] & 0xc0) >> 6) ] : '=');
out[3] = (unsigned char) (len > 2 ? b64[ in[2] & 0x3f ] : '=');
out[4] = '\0';
strncat(b64str, out, sizeof(out));
}
/* encode - base64 encode a stream, adding padding if needed */
void b64_encode(char *clrstr, char *b64dst) {
unsigned char in[3];
int i, len = 0;
int j = 0;
b64dst[0] = '\0';
while(clrstr[j]) {
len = 0;
for(i=0; i<3; i++) {
in[i] = (unsigned char) clrstr[j];
if(clrstr[j]) {
len++; j++;
}
else in[i] = 0;
}
if( len ) {
encodeblock( in, b64dst, len );
}
}
}
void sig_chld(int signo) //child
{
pid_t pid;
int stat;
while((pid = waitpid(-1, &stat, WNOHANG))>0)
printf("child %d terminated\n", pid);
return;
}
int main()
{
int listen_sock, conn_sock;
int server_len, client_len,choice;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
char myb64[1024] = ""; //encode
char mydst[1024] = ""; //decode
int sentBytes,revedBytes,bytes_readi;
char buff[1024]; //buffer to send data
listen_sock = socket(AF_INET, SOCK_STREAM, 0); //create socket
if (listen_sock == -1)
{
printf("ERROR opening socket\n");
return 0;
}
printf("Socket done\n");
//Thiet lap dia chi server
server_address.sin_family = AF_INET; //default
inet_aton("127.0.0.1",&server_address.sin_addr); //ip server
server_address.sin_port = htons(5500); // port server
server_len = sizeof(server_address);
if(bind(listen_sock, (struct sockaddr *)&server_address,server_len)<0)
{
printf("ERROR on binding\n");
return 0;
}
printf("Bind done\n");
int check = listen(listen_sock,10);
if (check == -1)
{
printf("Error connect");
return 0;
}
printf("Waiting connect..\n");
while(1) {
client_len = sizeof(client_address);
conn_sock = accept(listen_sock,(struct sockaddr *)&client_address, &client_len);
if(conn_sock==-1){
printf("Error connect\n");
return 1;
}else{
printf("Accept new connection\n");
}
if(fork() == 0){
close(listen_sock);
do{
revedBytes = recv(conn_sock,buff,1024,0);
buff[revedBytes]='\0';
if(strcmp(buff,"mahoa")==0) choice=1;
else if(strcmp(buff,"giaima")==0) choice=2; else choice = 3;
switch(choice)
{
case 1:
while((revedBytes = recv(conn_sock,buff,1024,0)) > 0){
buff[revedBytes]='\0';
//printf("string send by client encode : %s\n",buff);
b64_encode(buff, myb64); //ma hoa
sentBytes=send(conn_sock,myb64,1024,0); //gui lai string da ma hoa cho client
}
close(conn_sock);//Dong ket noi cua client
exit(0);
break;
case 2:
while((revedBytes = recv(conn_sock,buff,1024,0)) > 0){
buff[revedBytes]='\0';
//printf("string send by client decode: %s\n",buff);
b64_decode(buff,mydst); // giaima
sentBytes=send(conn_sock,mydst,1024,0);
}
close(conn_sock);
exit(0);
break;
case 3:break;
}
}while(choice!=3);
break;
}
signal(SIGCHLD,sig_chld);
close(conn_sock);
}
return 1;
}

sendmsg() doesn't function as is expected

I wrote a program with a simple pair of client and server,the first of which generate a bunch of strings and sends them via API provided by socket,namely sendmsg(),and the second of which receive the strings with facilitation of epoll.
According to what's captured by the tcpdump and the result of client execution,the client is able to connect to the server and send a few messages correctly,but only a few,the following strings failed to be recieved completely,and at the end of server print of the received codes there is infinite dupilcation of the same string segment.
Please contact me if any more detail is in need.
BTW,is there any recommendation of references which provide detailed description about the usage of sendmsg()?
This is main codes of client:
void MultithreadedLogAnalyzer::SendToServer(string Addr,uint16_t Port) {
int connfd = socket(AF_INET,SOCK_STREAM,0);
sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(Port);
inet_pton(AF_INET,Addr.c_str(),&servaddr.sin_addr);
/*connect failure process to be added*/
int st = connect(connfd,(struct sockaddr *)&servaddr,sizeof(sockaddr));
if(-1 == st){
perror("connection failed");
}
Handle(connfd,servaddr);
close(connfd);
}
void MultithreadedLogAnalyzer::Handle(int connfd,const sockaddr_in &servaddr){
int n= 0,counter =0;
for(vector<string>::iterator si = mFilterResult.begin();si != mFilterResult.end(); ++si)
{
msghdr msg;
iovec iov;
memset(&msg,0,sizeof(msg));
memset(&iov,0,sizeof(iov));
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_flags = 0;
char data[1024];
memset(data,0, 1024);
memcpy(data,si->c_str(),si->size());
iov.iov_base = data;
iov.iov_len = (*si).size() ;//check first to locate error
cout << "size:" << (*si).size() << endl;
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
//msg.msg_accrights = NULL;
//msg.msg_accrightslen = 0;
n = sendmsg(connfd,&msg,0);
cout << "n: " << n << endl;
++counter;
if (-1 == n)
perror("sendmsg error\n");
}
cout << "number of strings send: " << counter << endl;
The codes of server follows as below:
#include <sys/socket.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <strings.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#define POLLSIZE 100
#define STRINGSIZE 300
using namespace std;
void handle(int fd){
int c = 0;
do{
msghdr msg;
memset(&msg,0,sizeof(msghdr));
msg.msg_name = NULL;
msg.msg_namelen = 0;
char data[1024];
iovec iov;
iov.iov_base = data;
iov.iov_len = 1024;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_flags = 0;
c = recvmsg(fd,&msg,0);
if(msg.msg_iovlen==0
){
return;
}
cout << "msg length:" << msg.msg_iovlen<< endl;
cout << string((char *)(msg.msg_iov[0].iov_base)) << endl;
}while(c != -1);
}
int main() {
int listenfd;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in servaddr,cliaddr;
socklen_t socklen = 0;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
int serverPort = 2000;
servaddr.sin_port = htons(serverPort);
bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
int listennumnber = 20;
listen(listenfd,listennumnber);
//the rest to be commented
int epfd = epoll_create(POLLSIZE);
if (epfd < 0)
perror("epoll_create");
struct epoll_event ev;
ev.events = EPOLLIN|EPOLLET;
ev.data.fd = listenfd;
if(epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&ev) < 0)
perror("epoll_ctl");
int curfds = 1;
struct epoll_event *events = (struct epoll_event*)malloc(sizeof(struct epoll_event));
for (;;){
int n;
int nfds = epoll_wait(epfd,events,curfds,-1);
if (-1 == nfds)
continue;
for(n = 0;n < nfds; ++n){
if(events[n].data.fd == listenfd){
int connfd = accept(listenfd,(struct sockaddr *)&cliaddr ,&socklen);
cout << "connect established through connfd: " << connfd << endl;
if (connfd < 0)
continue;
// if(setnonblocking(connfd) < 0)
// perror("set setnonblocking error");
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = connfd;
if(epoll_ctl(epfd,EPOLL_CTL_ADD,connfd,&ev) < 0)
return -1;
curfds++;
continue;
}
handle(events[n].data.fd);
}
}
return 0;
}
Problem solved by adding send() and recv() functions in appropriate places of the message transferring loop.
cout << "msg length:" << msg.msg_iovlen<< endl;
cout << string((char *)(msg.msg_iov[0].iov_base)) << endl;
This message should only print msg.msg_iov[0].iov_len bytes.
}while(c != -1);
This is in the wrong place. It should be a test immediately after c = recvmsg(...); it should be if (c > 0), and if c == -1 you should either call perror() or use errno or strerror() in a message, immediately, without calling any other system calls.
Basically you're continuing to receive beyond end of stream; you're printing junk after end of stream or an error; and you're ignoring the actual message length when printing the message.

Random number generator sometimes fails to start in Eclipse

I have written some code that produces an object which returns a unique random number when queried. However, every once in about 6 or 7 runs the program fails to start, and there seems to be no apparent reason for this. Also, my memory seems to drop over time (using a vector in the class). Any ideas?
/*
* Urn.h
*
* Created on: Jan 17, 2014
* Author: edwinrietmeijer
*/
#ifndef URN_H_
#define URN_H_
#include <cstdlib>
#include <vector>
#include <iostream>
class Urn {
private:
int highRand;
int loRand;
int numsTotal;
int numsLeft;
std::vector<int>numsAvailable;
public:
Urn();
int getRand();
int getSize();
void reset();
virtual ~Urn();
};
#endif /* URN_H_ */
/*
* Urn.cpp
*
* Created on: Jan 17, 2014
* Author: edwinrietmeijer
*/
#include "Urn.h"
using namespace std;
Urn::Urn() : highRand( 9 ), loRand( 0 ) {
vector<int>::iterator pos;
int newRandom;
numsTotal = highRand - loRand;
bool newRandomExists = false;
while ( numsAvailable.size() < numsTotal + 1 )
{
do {
newRandomExists = false;
newRandom = ( rand() % ( numsTotal + 1 ) ) + loRand;
for ( pos = numsAvailable.begin(); pos != numsAvailable.end(); pos++ ) {
// cout << *pos << " ";
if ( *pos == newRandom ) {
newRandomExists = true;
}
}
// cout << " Random -> " << newRandom << " " << newRandomExists << endl;
} while ( newRandomExists == true );
newRandomExists = false;
numsAvailable.push_back( newRandom );
}
cout << endl;
// List the list
for (pos = numsAvailable.begin(); pos != numsAvailable.end(); pos++ ) {
// cout << *pos << " ";
}
// cout << endl;
// TODO Auto-generated constructor stub
}
int Urn::getRand() {
vector<int>::iterator pos = numsAvailable.begin();
int numToReturn = numsAvailable.back( );
numsAvailable.erase( pos );
return numToReturn;
}
int Urn::getSize() {
return numsAvailable.size();
}
void Urn::reset() {
}
Urn::~Urn() {
// TODO Auto-generated destructor stub
}
//============================================================================
// Name : UniqueRand.cpp
// Author : Edwin Rietmeijer
// Version :
// Copyright : This code is owned by Edwin Rietmeijer as of 2014
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <string>
#include "Urn.h"
using namespace std;
int main() {
cout << "Seeding..." << endl;
srand( time( NULL ) );
cout << "Getting object..." << endl;
Urn * urnObject = new Urn;
cout << urnObject -> getRand();
return 0;
}