Jpp  18.3.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
JNET::JUDPSocket Class Reference

UDP socket. More...

#include <JUDPSocket.hh>

Inheritance diagram for JNET::JUDPSocket:
JNET::JSocket JLANG::JFile JNET::JSocketAddress JLANG::JAbstractFile JLANG::JBinaryInput JLANG::JBinaryOutput JLANG::JComparable< JAbstractFile >

Public Member Functions

 JUDPSocket ()
 Default constructor. More...
 
 JUDPSocket (const JHostname &hostname)
 Constructor. More...
 
 JUDPSocket (const int port)
 Constructor. More...
 
int read (char *buffer, const int length) override
 Read data from socket. More...
 
int read (char *buffer, const int length, JUDPSocket &udp)
 Read data from socket. More...
 
virtual int write (const char *buffer, const int length) override
 Write data to socket. More...
 
int shutdown ()
 Shut down socket. More...
 
void setKeepAlive (const bool on)
 Set keep alive of socket. More...
 
bool getKeepAlive () const
 Get keep alive of socket. More...
 
void setReuseAddress (const bool on)
 Set reuse address. More...
 
bool getReuseAddress () const
 Get reuse address. More...
 
void setReceiveBufferSize (const int size)
 Set receive buffer size. More...
 
int getReceiveBufferSize () const
 Set receive buffer size. More...
 
void setSendBufferSize (const int size)
 Set send buffer size. More...
 
int getSendBufferSize () const
 Get send buffer size. More...
 
int close ()
 Close file. More...
 
bool in_avail (JTimeval timeout=JTimeval::min()) const
 Check availability of input. More...
 
bool out_avail (JTimeval timeout=JTimeval::min()) const
 Check availability of output. More...
 
virtual bool good () const
 Check status. More...
 
virtual bool fail () const
 Check status. More...
 
virtual bool bad () const
 Check status. More...
 
virtual bool eof () const
 Check end of file. More...
 
bool less (const JAbstractFile &file) const
 Less than operation. More...
 
int getFileDescriptor () const
 Get file descriptor. More...
 
void setFileDescriptor (const int file)
 Set file descriptor. More...
 
bool is_open () const
 Get open status. More...
 
const sockaddr * getSockaddr () const
 Get sockaddr. More...
 
sockaddr * getSockaddr ()
 Get sockaddr. More...
 
int getFamily () const
 Get family. More...
 
void setFamily (const int family)
 Set family. More...
 
std::string getHostname () const
 Get host name. More...
 
int getIPnumber () const
 Get IP number. More...
 
void setIPnumber (const int ip_number)
 Set IP number. More...
 
void setIPnumber ()
 Set any IP number. More...
 
int getPort () const
 Get port number. More...
 
void setPort (const int port)
 Set port number. More...
 

Static Public Member Functions

static int sizeOf ()
 Get size of object. More...
 

Static Public Attributes

static const int FILE_CLOSED = -1
 

Protected Member Functions

template<class T >
void setOption (const int level, const int option, const T value)
 Set socket option. More...
 
template<class T >
T getOption (const int level, const int option) const
 Get socket option. More...
 

Protected Attributes

int fileDescriptor
 

Detailed Description

UDP socket.

Definition at line 25 of file JUDPSocket.hh.

Constructor & Destructor Documentation

JNET::JUDPSocket::JUDPSocket ( )
inline

Default constructor.

Definition at line 35 of file JUDPSocket.hh.

35  :
36  JSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
37  {}
JSocket()
Default constructor.
Definition: JSocket.hh:44
JNET::JUDPSocket::JUDPSocket ( const JHostname hostname)
inline

Constructor.

UDP socket for client.

Parameters
hostnamehost name of destination

Definition at line 46 of file JUDPSocket.hh.

46  :
47  JSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
48  {
49  this->setReuseAddress(true);
50  this->setIPnumber(JSYSTEM::getIPnumber(hostname.hostname));
51  this->setPort(hostname.port);
52  }
void setReuseAddress(const bool on)
Set reuse address.
Definition: JSocket.hh:111
int getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:117
void setIPnumber()
Set any IP number.
std::string hostname
Definition: JHostname.hh:171
void setPort(const int port)
Set port number.
JSocket()
Default constructor.
Definition: JSocket.hh:44
JNET::JUDPSocket::JUDPSocket ( const int  port)
inline

Constructor.

UDP socket for server.

Parameters
portport number of receiver

Definition at line 61 of file JUDPSocket.hh.

61  :
62  JSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
63  {
64  this->setReuseAddress(true);
65  this->setIPnumber();
66  this->setPort(port);
67 
68  if (::bind(getFileDescriptor(), getSockaddr(), sizeof(sockaddr)) < 0) {
69  THROW(JSocketException, "Bind socket failed at port " << port);
70  }
71  }
void setReuseAddress(const bool on)
Set reuse address.
Definition: JSocket.hh:111
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
void setIPnumber()
Set any IP number.
const sockaddr * getSockaddr() const
Get sockaddr.
int getFileDescriptor() const
Get file descriptor.
void setPort(const int port)
Set port number.
JSocket()
Default constructor.
Definition: JSocket.hh:44

Member Function Documentation

int JNET::JUDPSocket::read ( char *  buffer,
const int  length 
)
inlineoverridevirtual

Read data from socket.

Parameters
bufferbuffer
lengthnumber of bytes to read
Returns
number of bytes actually read

Reimplemented from JNET::JSocket.

Definition at line 81 of file JUDPSocket.hh.

82  {
83  // read also zero-length datagram, as opposed to method read.
84 
85  return ::recv(this->getFileDescriptor(),
86  buffer,
87  length,
88  0);
89  }
int getFileDescriptor() const
Get file descriptor.
int JNET::JUDPSocket::read ( char *  buffer,
const int  length,
JUDPSocket udp 
)
inline

Read data from socket.

Parameters
bufferbuffer
lengthnumber of bytes to read
udpUDP socket
Returns
number of bytes actually read

Definition at line 100 of file JUDPSocket.hh.

101  {
102  socklen_t size = sizeof(sockaddr);
103 
105 
106  return ::recvfrom(this->getFileDescriptor(),
107  buffer,
108  length,
109  0,
110  udp.getSockaddr(),
111  &size);
112  }
const sockaddr * getSockaddr() const
Get sockaddr.
int getFileDescriptor() const
Get file descriptor.
void setFileDescriptor(const int file)
Set file descriptor.
virtual int JNET::JUDPSocket::write ( const char *  buffer,
const int  length 
)
inlineoverridevirtual

Write data to socket.

Parameters
bufferbuffer
lengthnumber of bytes to write
Returns
number of bytes actually written

Reimplemented from JNET::JSocket.

Definition at line 122 of file JUDPSocket.hh.

123  {
124  return ::sendto(this->getFileDescriptor(),
125  buffer,
126  length,
127  0,
128  this->getSockaddr(),
129  sizeof(sockaddr));
130  }
const sockaddr * getSockaddr() const
Get sockaddr.
int getFileDescriptor() const
Get file descriptor.
int JNET::JSocket::shutdown ( )
inlineinherited

Shut down socket.

Returns
return value

Definition at line 74 of file JSocket.hh.

75  {
76  const int value = ::shutdown(getFileDescriptor(), SHUT_RDWR);
77 
78  close();
79 
80  return value;
81  }
int close()
Close file.
Definition: JFile.hh:57
int getFileDescriptor() const
Get file descriptor.
int shutdown()
Shut down socket.
Definition: JSocket.hh:74
void JNET::JSocket::setKeepAlive ( const bool  on)
inlineinherited

Set keep alive of socket.

Parameters
ontrue to enable keep alive; false to disable

Definition at line 89 of file JSocket.hh.

90  {
91  setOption(SOL_SOCKET, SO_KEEPALIVE, int(on ? 1 : 0));
92  }
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:259
bool JNET::JSocket::getKeepAlive ( ) const
inlineinherited

Get keep alive of socket.

Returns
true if keep alive; else false

Definition at line 100 of file JSocket.hh.

101  {
102  return (getOption<int>(SOL_SOCKET, SO_KEEPALIVE) == 1);
103  }
void JNET::JSocket::setReuseAddress ( const bool  on)
inlineinherited

Set reuse address.

Parameters
ontrue to enable reuse address; false to disable

Definition at line 111 of file JSocket.hh.

112  {
113  setOption(SOL_SOCKET, SO_REUSEADDR, int(on ? 1 : 0));
114  }
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:259
bool JNET::JSocket::getReuseAddress ( ) const
inlineinherited

Get reuse address.

Returns
true if enable reuse address; else false

Definition at line 122 of file JSocket.hh.

123  {
124  return (getOption<int>(SOL_SOCKET, SO_REUSEADDR) == 1);
125  }
void JNET::JSocket::setReceiveBufferSize ( const int  size)
inlineinherited

Set receive buffer size.

Parameters
sizenumber of bytes

Definition at line 133 of file JSocket.hh.

134  {
135  setOption(SOL_SOCKET, SO_RCVBUF, int(size));
136  }
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:259
int JNET::JSocket::getReceiveBufferSize ( ) const
inlineinherited

Set receive buffer size.

Returns
number of bytes

Definition at line 144 of file JSocket.hh.

145  {
146  return getOption<int>(SOL_SOCKET, SO_RCVBUF);
147  }
void JNET::JSocket::setSendBufferSize ( const int  size)
inlineinherited

Set send buffer size.

Parameters
sizenumber of bytes

Definition at line 155 of file JSocket.hh.

156  {
157  setOption(SOL_SOCKET, SO_SNDBUF, int(size));
158  }
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:259
int JNET::JSocket::getSendBufferSize ( ) const
inlineinherited

Get send buffer size.

Returns
number of bytes

Definition at line 166 of file JSocket.hh.

167  {
168  return getOption<int>(SOL_SOCKET, SO_SNDBUF);
169  }
template<class T >
void JNET::JSocket::setOption ( const int  level,
const int  option,
const T  value 
)
inlineprotectedinherited

Set socket option.

Parameters
levellevel
optionoption
valuevalue

Definition at line 259 of file JSocket.hh.

260  {
261  socklen_t size = sizeof(T);
262 
263  if (setsockopt(getFileDescriptor(), level, option, &value, size) < 0) {
264  THROW(JSocketException, "Set socket option failed " << errno);
265  }
266  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
int getFileDescriptor() const
Get file descriptor.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
template<class T >
T JNET::JSocket::getOption ( const int  level,
const int  option 
) const
inlineprotectedinherited

Get socket option.

Parameters
levellevel
optionoption
Returns
value

Definition at line 277 of file JSocket.hh.

278  {
279  T value;
280  socklen_t size = sizeof(T);
281 
282  if (getsockopt(getFileDescriptor(), level, option, &value, &size) < 0) {
283  THROW(JSocketException, "Get socket option failed " << errno);
284  }
285 
286  return value;
287  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
int getFileDescriptor() const
Get file descriptor.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
int JLANG::JFile::close ( )
inlineinherited

Close file.

Returns
return value

Definition at line 57 of file JFile.hh.

58  {
59  int value = -1;
60 
61  if (fileDescriptor != FILE_CLOSED) {
62 
63  value = ::close(fileDescriptor);
64 
66  result = 0;
67  }
68 
69  return value;
70  }
int close()
Close file.
Definition: JFile.hh:57
static const int FILE_CLOSED
int result
Definition: JFile.hh:170
bool JLANG::JFile::in_avail ( JTimeval  timeout = JTimeval::min()) const
inlineinherited

Check availability of input.

This method returns true if at least one byte can be read without blocking.

Parameters
timeouttimeout
Returns
true if ready to read; else false

Definition at line 106 of file JFile.hh.

107  {
108  return JFileDescriptorMask(*this).in_avail(timeout);
109  }
Auxiliary class for method select.
bool in_avail(JTimeval timeout=JTimeval::min())
Check availability of input.
bool JLANG::JFile::out_avail ( JTimeval  timeout = JTimeval::min()) const
inlineinherited

Check availability of output.

This method returns true if at least one byte can be written without blocking.

Parameters
timeouttimeout
Returns
true if ready to write; else false

Definition at line 119 of file JFile.hh.

120  {
121  return JFileDescriptorMask(*this).out_avail(timeout);
122  }
bool out_avail(JTimeval timeout=JTimeval::min())
Check availability of output.
Auxiliary class for method select.
virtual bool JLANG::JFile::good ( ) const
inlinevirtualinherited

Check status.

Returns
true if last I/O operation successful; else false

Definition at line 130 of file JFile.hh.

131  {
132  return is_open() && !eof() && !bad();
133  }
virtual bool eof() const
Check end of file.
Definition: JFile.hh:163
bool is_open() const
Get open status.
virtual bool bad() const
Check status.
Definition: JFile.hh:152
virtual bool JLANG::JFile::fail ( ) const
inlinevirtualinherited

Check status.

Returns
true if last I/O operation caused logical error; else false

Definition at line 141 of file JFile.hh.

142  {
143  return result == 0;
144  }
int result
Definition: JFile.hh:170
virtual bool JLANG::JFile::bad ( ) const
inlinevirtualinherited

Check status.

Returns
true if last I/O operation caused read/write error; else false

Definition at line 152 of file JFile.hh.

153  {
154  return fail();
155  }
virtual bool fail() const
Check status.
Definition: JFile.hh:141
virtual bool JLANG::JFile::eof ( ) const
inlinevirtualinherited

Check end of file.

Returns
true if end of file; else false

Definition at line 163 of file JFile.hh.

164  {
165  return result == EOF;
166  }
int result
Definition: JFile.hh:170
bool JLANG::JAbstractFile::less ( const JAbstractFile file) const
inlineinherited

Less than operation.

Parameters
fileJAbstractFile to be compared
Returns
true if this file descriptor is less; else false

Definition at line 64 of file JAbstractFile.hh.

65  {
66  return getFileDescriptor() < file.getFileDescriptor();
67  }
int getFileDescriptor() const
Get file descriptor.
int JLANG::JAbstractFile::getFileDescriptor ( ) const
inlineinherited

Get file descriptor.

Returns
file descriptor

Definition at line 75 of file JAbstractFile.hh.

76  {
77  return fileDescriptor;
78  }
void JLANG::JAbstractFile::setFileDescriptor ( const int  file)
inlineinherited

Set file descriptor.

Parameters
filefile descriptor

Definition at line 86 of file JAbstractFile.hh.

87  {
89  }
then usage $script[< detector identifier >< run range >]< QA/QCfile > nExample script to produce data quality plots nWhen a detector identifier and run range are data are downloaded from the database nand subsequently stored in the given QA QC file
Definition: JDataQuality.sh:19
bool JLANG::JAbstractFile::is_open ( ) const
inlineinherited

Get open status.

Definition at line 95 of file JAbstractFile.hh.

96  {
97  return fileDescriptor != FILE_CLOSED;
98  }
static const int FILE_CLOSED
const sockaddr* JNET::JSocketAddress::getSockaddr ( ) const
inlineinherited

Get sockaddr.

Returns
pointer to sockaddr structure

Definition at line 44 of file JSocketAddress.hh.

45  {
46  return (const sockaddr*) static_cast<const sockaddr_in*>(this);
47  }
sockaddr* JNET::JSocketAddress::getSockaddr ( )
inlineinherited

Get sockaddr.

Returns
pointer to sockaddr structure

Definition at line 55 of file JSocketAddress.hh.

56  {
57  return (sockaddr*) static_cast<sockaddr_in*>(this);
58  }
int JNET::JSocketAddress::getFamily ( ) const
inlineinherited

Get family.

Returns
family

Definition at line 66 of file JSocketAddress.hh.

67  {
68  return sin_family;
69  }
void JNET::JSocketAddress::setFamily ( const int  family)
inlineinherited

Set family.

Parameters
familyfamily

Definition at line 77 of file JSocketAddress.hh.

78  {
79  sin_family = family;
80  }
std::string JNET::JSocketAddress::getHostname ( ) const
inlineinherited

Get host name.

Returns
host name

Definition at line 88 of file JSocketAddress.hh.

89  {
90  return JSYSTEM::getHostname(this->getIPnumber());
91  }
int getIPnumber() const
Get IP number.
std::string getHostname()
Get host name.
Definition: JNetwork.hh:77
int JNET::JSocketAddress::getIPnumber ( ) const
inlineinherited

Get IP number.

Returns
IP number

Definition at line 99 of file JSocketAddress.hh.

100  {
101  return sin_addr.s_addr;
102  }
void JNET::JSocketAddress::setIPnumber ( const int  ip_number)
inlineinherited

Set IP number.

Parameters
ip_numberIP number

Definition at line 110 of file JSocketAddress.hh.

111  {
112  sin_addr.s_addr = ip_number;
113  }
void JNET::JSocketAddress::setIPnumber ( )
inlineinherited

Set any IP number.

Definition at line 119 of file JSocketAddress.hh.

120  {
121  sin_addr.s_addr = htonl(INADDR_ANY);
122  }
int JNET::JSocketAddress::getPort ( ) const
inlineinherited

Get port number.

Returns
port number

Definition at line 130 of file JSocketAddress.hh.

131  {
132  return ntohs(this->sin_port);
133  }
void JNET::JSocketAddress::setPort ( const int  port)
inlineinherited

Set port number.

Parameters
portport number

Definition at line 141 of file JSocketAddress.hh.

142  {
143  if (port >= 0 && port <= std::numeric_limits<u_short>::max())
144  sin_port = htons((u_short) port);
145  else
146  THROW(JCastException, "JSocketAddress::setPort() illegal value.");
147  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
static int JNET::JSocketAddress::sizeOf ( )
inlinestaticinherited

Get size of object.

Returns
number of bytes

Definition at line 155 of file JSocketAddress.hh.

156  {
157  return sizeof(sockaddr_in);
158  }

Member Data Documentation

const int JLANG::JAbstractFile::FILE_CLOSED = -1
staticinherited

Definition at line 27 of file JAbstractFile.hh.

int JLANG::JAbstractFile::fileDescriptor
protectedinherited

Definition at line 102 of file JAbstractFile.hh.


The documentation for this class was generated from the following file: