A Discrete-Event Network Simulator
API
unix-fd-reader.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 The Boeing Company
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tom Goff <thomas.goff@boeing.com>
19  */
20 
21 #ifndef UNIX_FD_READER_H
22 #define UNIX_FD_READER_H
23 
24 #include <stdint.h>
25 
26 #include "callback.h"
27 #include "system-thread.h"
28 #include "event-id.h"
29 
36 namespace ns3 {
37 
47 class FdReader : public SimpleRefCount<FdReader>
48 {
49 public:
51  FdReader ();
53  virtual ~FdReader ();
54 
63  void Start (int fd, Callback<void, uint8_t *, ssize_t> readCallback);
64 
69  void Stop (void);
70 
71 protected:
72 
76  struct Data
77  {
79  Data () : m_buf (0), m_len (0)
80  {}
87  Data (uint8_t *buf, ssize_t len) : m_buf (buf), m_len (len)
88  {}
90  uint8_t *m_buf;
92  ssize_t m_len;
93  };
94 
108  virtual FdReader::Data DoRead (void) = 0;
109 
113  int m_fd;
114 
115 private:
116 
118  void Run (void);
120  void DestroyEvent (void);
121 
124 
127 
129  int m_evpipe[2];
131  bool m_stop;
132 
138 };
139 
140 } // namespace ns3
141 
142 #endif /* UNIX_FD_READER_H */
event-id.h
ns3::EventId declarations.
ns3::FdReader::m_fd
int m_fd
The file descriptor to read from.
Definition: unix-fd-reader.h:113
ns3::EventId
An identifier for simulation events.
Definition: event-id.h:54
ns3::Callback< void, uint8_t *, ssize_t >
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::SimpleRefCount
A template-based reference counting class.
Definition: simple-ref-count.h:74
ns3::FdReader::Data::m_len
ssize_t m_len
The size of the read data buffer, in bytes.
Definition: unix-fd-reader.h:92
callback.h
Declaration of the various callback functions.
ns3::FdReader::Data::Data
Data()
Default constructor, with null buffer and zero length.
Definition: unix-fd-reader.h:79
ns3::FdReader::m_readCallback
Callback< void, uint8_t *, ssize_t > m_readCallback
The main thread callback function to invoke when we have data.
Definition: unix-fd-reader.h:123
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::FdReader::m_readThread
Ptr< SystemThread > m_readThread
The thread doing the read, created and launched by Start().
Definition: unix-fd-reader.h:126
ns3::FdReader::DestroyEvent
void DestroyEvent(void)
Event handler scheduled for destroy time to halt the thread.
Definition: unix-fd-reader.cc:114
ns3::FdReader::Data::m_buf
uint8_t * m_buf
The read data buffer.
Definition: unix-fd-reader.h:90
ns3::FdReader::Data::Data
Data(uint8_t *buf, ssize_t len)
Construct from a buffer of a given length.
Definition: unix-fd-reader.h:87
ns3::FdReader::m_destroyEvent
EventId m_destroyEvent
The event scheduled for destroy time which will invoke DestroyEvent and halt the thread.
Definition: unix-fd-reader.h:137
ns3::FdReader::Run
void Run(void)
The asynchronous function which performs the read.
Definition: unix-fd-reader.cc:165
system-thread.h
System-independent thread class ns3::SystemThread declaration.
ns3::FdReader::~FdReader
virtual ~FdReader()
Destructor.
Definition: unix-fd-reader.cc:56
ns3::FdReader::Start
void Start(int fd, Callback< void, uint8_t *, ssize_t > readCallback)
Start a new read thread.
Definition: unix-fd-reader.cc:62
ns3::FdReader
A class that asynchronously reads from a file descriptor.
Definition: unix-fd-reader.h:48
ns3::FdReader::m_stop
bool m_stop
Signal the read thread to stop.
Definition: unix-fd-reader.h:131
ns3::FdReader::Stop
void Stop(void)
Stop the read thread and reset internal state.
Definition: unix-fd-reader.cc:121
ns3::FdReader::m_evpipe
int m_evpipe[2]
Pipe used to signal events between threads.
Definition: unix-fd-reader.h:129
ns3::FdReader::DoRead
virtual FdReader::Data DoRead(void)=0
The read implementation.
ns3::FdReader::FdReader
FdReader()
Constructor.
Definition: unix-fd-reader.cc:47
ns3::FdReader::Data
A structure representing data read.
Definition: unix-fd-reader.h:77