Jpp  17.1.1
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 | Static Protected Attributes | Private Attributes | List of all members
JNET::JControlHostObjectOutput< T > Class Template Reference

Implemenation of object output through ControlHost. More...

#include <JControlHostObjectOutput.hh>

Inheritance diagram for JNET::JControlHostObjectOutput< T >:
JNET::JControlHost JLANG::JObjectOutput< T > JNET::JTag JNET::JSocketBlocking JLANG::JThrow< JControlHost > JNET::JSocket JLANG::JFile JNET::JSocketAddress JLANG::JAbstractFile JLANG::JBinaryInput JLANG::JBinaryOutput JLANG::JComparable< JAbstractFile >

Public Member Functions

 JControlHostObjectOutput (const JHostname &server)
 Constructor. More...
 
 JControlHostObjectOutput (const JHostname &server, const JTag &tag)
 Constructor. More...
 
virtual bool put (const T &object) override
 Object output. More...
 
int Subscribe (const JSubscription &subscription)
 Subscribe to single tag. More...
 
int Subscribe (const JSubscriptionList &subscription)
 Subscribe to list of tags. More...
 
int MyId (const std::string &nick_name)
 Identify. More...
 
int SendMeNext ()
 Tell server to send next message. More...
 
int SendMeAlways ()
 Tell server to send messages forever. More...
 
int PutFullData (const JTag &tag, const void *buffer, const long long int length)
 Send data. More...
 
int PutFullData (const std::string &tag, const void *buffer, const long long int length)
 Send data. More...
 
int PutFullString (const JTag &tag, const std::string &buffer)
 Send string. More...
 
int PutFullString (const std::string &tag, const std::string &buffer)
 Send string. More...
 
int Connected ()
 Send version. More...
 
int WaitHead (JPrefix &prefix)
 Wait for header. More...
 
int WaitHead (std::string &tag, long long int &length)
 Wait for header. More...
 
int CheckHead (JPrefix &prefix, JTimeval timeout=JTimeval::min())
 Check for header, without waiting. More...
 
int CheckHead (std::string &tag, long long int &length, const int timeout_us=0)
 Check for header, without waiting. More...
 
int GetFullData (void *buffer, long long int length)
 Receive data. More...
 
int GetFullString (std::string &buffer)
 Receive string. More...
 
int configure ()
 Configure socket (factory reset). More...
 
int read (char *buffer, const int length)
 Read data from socket. More...
 
int write (const char *buffer, const int length)
 Write data to socket. More...
 
int shutdown ()
 Shut down socket. More...
 
void setNonBlocking (const bool on)
 Set non-blocking of I/O. More...
 
bool getNonBlocking () const
 Get non-blocking of I/O. More...
 
void setKeepAlive (const bool on)
 Set keep alive of socket. More...
 
bool getKeepAlive () const
 Get keep alive of socket. More...
 
void setKeepIdle (const int t_s)
 Set the TCP idle time. More...
 
void setKeepCnt (const int count)
 Set the TCP idle count. More...
 
void setKeepIntvl (const int t_s)
 Set the TCP interval time. More...
 
void setReuseAddress (const bool on)
 Set reuse address. More...
 
bool getReuseAddress () const
 Get reuse address. More...
 
void setTcpNoDelay (const bool on)
 Set TCP no-delay. More...
 
bool getTcpNoDelay () const
 Get TCP no-delay. 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...
 
void accept (const int server)
 Accept connection from a server. More...
 
void connect (const int port)
 Connect to port on local host. More...
 
void connect (const std::string &host_name, const int port)
 Connect to port on specified host. More...
 
void connect (const int ip_number, const int port)
 Connect to port on specified host. More...
 
void 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...
 
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...
 
const JTaggetTag () const
 Get tag. More...
 
JTaggetTag ()
 Get tag. More...
 
void setTag (const JTag &tag)
 Set tag. More...
 
void setTag (const std::string &tag)
 Set tag. More...
 
void setTag (const JTag_t id)
 Set tag. More...
 
JTag_t getID () const
 Get identifier. More...
 
std::string toString () const
 Convert tag to string. More...
 
 operator std::string () const
 Type conversion operators. More...
 
const char * c_str () const
 C-string. More...
 
const char operator[] (int i) const
 Get character. More...
 

Static Public Member Functions

static bool maybe_special (const JTag &tag)
 Check special ControlHost tags. More...
 
static bool is_valid (const char c)
 Check validity of subscription specifier. More...
 
static int WhereIs (const std::string &host_name, const std::string &nick_name, std::string &answer)
 Locate ControlHost client(s). More...
 
static int sizeOf ()
 Get size of object. More...
 
static void Throw (const bool option)
 Enable/disable throw option. More...
 
static int Throw (const JException &error, const int value=-1)
 Throw exception or return error. More...
 

Static Public Attributes

static const int FILE_CLOSED = -1
 

Protected Member Functions

 JControlHostObjectOutput ()
 Constructor. More...
 
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
 
JTag_t id
 

Static Protected Attributes

static bool do_throw
 throw option More...
 

Private Attributes

JByteArrayWriter out
 

Detailed Description

template<class T>
class JNET::JControlHostObjectOutput< T >

Implemenation of object output through ControlHost.

Note that if the tag is not specified, it is obtained by the template method getTag(JLANG::JType).
This class implements the JLANG::JObjectOutput interface.

Definition at line 36 of file JControlHostObjectOutput.hh.

Constructor & Destructor Documentation

template<class T>
JNET::JControlHostObjectOutput< T >::JControlHostObjectOutput ( )
inlineprotected

Constructor.

Definition at line 45 of file JControlHostObjectOutput.hh.

45  :
46  JControlHost(),
47  JObjectOutput<T>(),
48  JTag(::getTag(JType<T>()))
49  {}
JTag()
Default constructor.
Definition: JTag.hh:43
const JTag & getTag() const
Get tag.
Definition: JTag.hh:86
JControlHost()
Default constructor.
template<class T>
JNET::JControlHostObjectOutput< T >::JControlHostObjectOutput ( const JHostname server)
inline

Constructor.

Parameters
serverhost name and optional port number

Definition at line 58 of file JControlHostObjectOutput.hh.

58  :
59  JControlHost(),
60  JObjectOutput<T>(),
61  JTag(::getTag(JType<T>()))
62  {
63  connect(server.hostname, server.port);
64  }
JTag()
Default constructor.
Definition: JTag.hh:43
std::string hostname
Definition: JHostname.hh:154
const JTag & getTag() const
Get tag.
Definition: JTag.hh:86
void connect(const int port)
Connect to port on local host.
Definition: JSocket.hh:385
JControlHost()
Default constructor.
template<class T>
JNET::JControlHostObjectOutput< T >::JControlHostObjectOutput ( const JHostname server,
const JTag tag 
)
inline

Constructor.

Parameters
serverhost name and optional port number
tagtag

Definition at line 73 of file JControlHostObjectOutput.hh.

74  :
75  JControlHost(),
76  JObjectOutput<T>(),
77  JTag(tag)
78  {
79  connect(server.hostname, server.port);
80  }
JTag()
Default constructor.
Definition: JTag.hh:43
std::string hostname
Definition: JHostname.hh:154
void connect(const int port)
Connect to port on local host.
Definition: JSocket.hh:385
JControlHost()
Default constructor.

Member Function Documentation

template<class T>
virtual bool JNET::JControlHostObjectOutput< T >::put ( const T object)
inlineoverridevirtual

Object output.

Parameters
objectobject
Returns
true if OK; else false

Implements JLANG::JObjectOutput< T >.

Definition at line 89 of file JControlHostObjectOutput.hh.

90  {
91  out.clear();
92 
93  out << object;
94 
95  return this->PutFullData(this->getTag(), out.data(), out.size()) == 0;
96  }
void clear()
Clear buffer.
const JTag & getTag() const
Get tag.
Definition: JTag.hh:86
int PutFullData(const JTag &tag, const void *buffer, const long long int length)
Send data.
static bool JNET::JControlHost::maybe_special ( const JTag tag)
inlinestaticinherited

Check special ControlHost tags.

Parameters
tagtag
Returns
true if possibly special tag; else false

Definition at line 270 of file JControlHost.hh.

271  {
272  return tag[0] == '_';
273  }
static bool JNET::JControlHost::is_valid ( const char  c)
inlinestaticinherited

Check validity of subscription specifier.

Parameters
csubscription specifier
Returns
true if valid; else false

Definition at line 282 of file JControlHost.hh.

283  {
284  return (c == SUBSCRIBE_ALL ||
285  c == SUBSCRIBE_ANY);// ||
286  //c == SUBSCRIBE_SHARED_MEMORY);
287  }
$WORKDIR ev_configure_domsimulator txt echo process $DOM_SIMULATOR $i $SOURCE_HOST[$index] csh c(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&($DOM_SIMULATOR\-u\$NAME\$\-H\$SERVER\$\-M\$LOGGER\$\-d $DEBUG</dev/null > &/dev/null &))'
int JNET::JControlHost::Subscribe ( const JSubscription subscription)
inlineinherited

Subscribe to single tag.

Parameters
subscriptionsubscription
Returns
0 if OK; -1 if socket error

Definition at line 364 of file JControlHost.hh.

365  {
366  return PutFullString(DISPTAG_Subscribe, subscription.toString());
367  }
static const JTag DISPTAG_Subscribe("_Subscri")
Special ControlHost tags.
std::string toString() const
Convert subscription to string.
Definition: JControlHost.hh:83
int PutFullString(const JTag &tag, const std::string &buffer)
Send string.
int JNET::JControlHost::Subscribe ( const JSubscriptionList subscription)
inlineinherited

Subscribe to list of tags.

Parameters
subscriptionsubscription
Returns
0 if OK; -1 if socket error

Definition at line 376 of file JControlHost.hh.

377  {
378  return PutFullString(DISPTAG_Subscribe, subscription.toString());
379  }
static const JTag DISPTAG_Subscribe("_Subscri")
Special ControlHost tags.
std::string toString() const
Convert subscription list to string.
int PutFullString(const JTag &tag, const std::string &buffer)
Send string.
int JNET::JControlHost::MyId ( const std::string &  nick_name)
inlineinherited

Identify.

Parameters
nick_namenick name
Returns
0 if OK; -1 if socket error

Definition at line 388 of file JControlHost.hh.

389  {
390  return PutFullString(DISPTAG_MyId, nick_name);
391  }
static const JTag DISPTAG_MyId("_MyId")
int PutFullString(const JTag &tag, const std::string &buffer)
Send string.
int JNET::JControlHost::SendMeNext ( )
inlineinherited

Tell server to send next message.

Returns
0 if OK; -1 if socket error

Definition at line 399 of file JControlHost.hh.

400  {
401  return PutFullData(DISPTAG_Gime, NULL, 0);
402  }
static const JTag DISPTAG_Gime("_Gime")
int PutFullData(const JTag &tag, const void *buffer, const long long int length)
Send data.
int JNET::JControlHost::SendMeAlways ( )
inlineinherited

Tell server to send messages forever.

Returns
0 if OK; -1 if socket error

Definition at line 410 of file JControlHost.hh.

411  {
412  return PutFullData(DISPTAG_Always, NULL, 0);
413  }
static const JTag DISPTAG_Always("_Always")
int PutFullData(const JTag &tag, const void *buffer, const long long int length)
Send data.
int JNET::JControlHost::PutFullData ( const JTag tag,
const void *  buffer,
const long long int  length 
)
inlineinherited

Send data.

Parameters
tagtag
bufferdata
lengthnumber of bytes
Returns
0 if OK; -1 if socket error; -2 if other error

Definition at line 424 of file JControlHost.hh.

425  {
426  try {
427 
428  JPrefix __prefix__(tag, length);
429 
430  write((char*) &__prefix__, sizeof(JPrefix));
431 
432  if (length != 0) {
433  write((char*) buffer, (int) length);
434  }
435 
436  return 0;
437  }
438  catch (const JSocketException& error) {
439  return Throw(error, -1);
440  }
441  }
ControlHost prefix.
Definition: JPrefix.hh:31
int write(const char *buffer, const int length)
Write data to socket.
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
int JNET::JControlHost::PutFullData ( const std::string &  tag,
const void *  buffer,
const long long int  length 
)
inlineinherited

Send data.

Parameters
tagtag
bufferdata
lengthnumber of bytes
Returns
0 if OK; -1 if socket error; -2 if other error

Definition at line 452 of file JControlHost.hh.

453  {
454  try {
455  return PutFullData(JTag(tag), buffer, length);
456  }
457  catch (const JSocketException& error) {
458  return Throw(error, -1);
459  }
460  catch (const JControlHostException& error) {
461  return Throw(error, -2);
462  }
463  }
ControlHost tag.
Definition: JTag.hh:38
int PutFullData(const JTag &tag, const void *buffer, const long long int length)
Send data.
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
int JNET::JControlHost::PutFullString ( const JTag tag,
const std::string &  buffer 
)
inlineinherited

Send string.

Parameters
tagtag
bufferdata
Returns
0 if OK; -1 if socket error; -2 if other error

Definition at line 473 of file JControlHost.hh.

475  {
476  return PutFullData(tag, buffer.c_str(), buffer.size());
477  }
const char * c_str() const
C-string.
Definition: JTag.hh:201
int PutFullData(const JTag &tag, const void *buffer, const long long int length)
Send data.
int JNET::JControlHost::PutFullString ( const std::string &  tag,
const std::string &  buffer 
)
inlineinherited

Send string.

Parameters
tagtag
bufferdata
Returns
0 if OK; -1 if socket error; -2 if other error

Definition at line 487 of file JControlHost.hh.

489  {
490  return PutFullData(tag, buffer.c_str(), buffer.size());
491  }
int PutFullData(const JTag &tag, const void *buffer, const long long int length)
Send data.
int JNET::JControlHost::Connected ( )
inlineinherited

Send version.

Returns
0 if OK; -1 if socket error; -2 if other error

Definition at line 499 of file JControlHost.hh.

500  {
502  }
static const std::string CHOO_VERSION
Definition: JControlHost.hh:34
static const JTag DISPTAG_Version("_Version")
int PutFullString(const JTag &tag, const std::string &buffer)
Send string.
int JNET::JControlHost::WaitHead ( JPrefix prefix)
inlineinherited

Wait for header.

Parameters
prefixprefix
Returns
0 if OK; -1 if socket error

Definition at line 511 of file JControlHost.hh.

512  {
513  try {
514 
515  read((char*) &this->prefix, sizeof(JPrefix));
516 
517  prefix = this->prefix;
518 
519  return 0;
520  }
521  catch (const JSocketException& error) {
522  return Throw(error, -1);
523  }
524  }
ControlHost prefix.
Definition: JPrefix.hh:31
int read(char *buffer, const int length)
Read data from socket.
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
int JNET::JControlHost::WaitHead ( std::string &  tag,
long long int &  length 
)
inlineinherited

Wait for header.

Parameters
tagtag
lengthnumber of bytes
Returns
0 if OK; -1 if socket error

Definition at line 534 of file JControlHost.hh.

535  {
536  const int rvalue = WaitHead(this->prefix);
537 
538  if (rvalue == 0) {
539  tag = this->prefix.getTag();
540  length = this->prefix.getSize();
541  }
542 
543  return rvalue;
544  }
int getSize() const
Get size.
Definition: JPrefix.hh:62
const JTag & getTag() const
Get tag.
Definition: JTag.hh:86
int WaitHead(JPrefix &prefix)
Wait for header.
int JNET::JControlHost::CheckHead ( JPrefix prefix,
JTimeval  timeout = JTimeval::min() 
)
inlineinherited

Check for header, without waiting.

Parameters
prefixprefix
timeouttimeout
Returns
1 if header; 0 if no header; -1 if socket error

Definition at line 554 of file JControlHost.hh.

555  {
556  try {
557 
558  if (in_avail(timeout)) {
559 
560  WaitHead(prefix);
561 
562  return 1;
563 
564  } else {
565 
566  return 0;
567  }
568  }
569  catch (const JSocketException& error) {
570  return Throw(error, -1);
571  }
572  }
bool in_avail(JTimeval timeout=JTimeval::min()) const
Check availability of input.
Definition: JFile.hh:100
int WaitHead(JPrefix &prefix)
Wait for header.
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
int JNET::JControlHost::CheckHead ( std::string &  tag,
long long int &  length,
const int  timeout_us = 0 
)
inlineinherited

Check for header, without waiting.

Parameters
tagtag
lengthnumber of bytes
timeout_ustimeout [us]
Returns
1 if header; 0 if no header; -1 if socket error

Definition at line 583 of file JControlHost.hh.

584  {
585  const int rvalue = CheckHead(this->prefix, timeout_us);
586 
587  if (rvalue == 1) {
588  tag = this->prefix.getTag();
589  length = this->prefix.getSize();
590  }
591 
592  return rvalue;
593  }
int getSize() const
Get size.
Definition: JPrefix.hh:62
int CheckHead(JPrefix &prefix, JTimeval timeout=JTimeval::min())
Check for header, without waiting.
const JTag & getTag() const
Get tag.
Definition: JTag.hh:86
int JNET::JControlHost::GetFullData ( void *  buffer,
long long int  length 
)
inlineinherited

Receive data.

Parameters
bufferdata
lengthnumber of bytes
Returns
0 if OK; -1 if socket error

Definition at line 603 of file JControlHost.hh.

604  {
605  try {
606 
607  read((char*) buffer, (int) length);
608 
609  return 0;
610  }
611  catch (const JSocketException& error) {
612  return Throw(error, -1);
613  }
614  }
int read(char *buffer, const int length)
Read data from socket.
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
int JNET::JControlHost::GetFullString ( std::string &  buffer)
inlineinherited

Receive string.

Parameters
bufferdata
Returns
0 if OK; -1 if socket error

Definition at line 623 of file JControlHost.hh.

624  {
625  buffer.resize(this->prefix.getSize());
626 
627  return GetFullData((char*) buffer.data(), buffer.size());
628  }
int getSize() const
Get size.
Definition: JPrefix.hh:62
int GetFullData(void *buffer, long long int length)
Receive data.
static int JNET::JControlHost::WhereIs ( const std::string &  host_name,
const std::string &  nick_name,
std::string &  answer 
)
inlinestaticinherited

Locate ControlHost client(s).

Parameters
host_namehost name
nick_namenick name
answerlist of host names
Returns
0 if OK; -1 if socket error

Definition at line 639 of file JControlHost.hh.

642  {
643  try {
644 
645  using namespace std;
646 
647  JControlHost socket(host_name);
648 
649  socket.PutFullString(DISPTAG_WhereIs, nick_name);
650 
651  string tag;
652  long long int length;
653 
654  socket.WaitHead(tag, length);
655  socket.GetFullString(answer);
656 
657  return 0;
658  }
659  catch (const JSocketException& error) {
660  return Throw(error, -1);
661  }
662  }
ControlHost class.
static const JTag DISPTAG_WhereIs("_WhereIs")
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
int JNET::JControlHost::configure ( )
inlineinherited

Configure socket (factory reset).

Returns
0 if OK; -1 if socket error

Definition at line 670 of file JControlHost.hh.

671  {
672  try {
673 
674  setTcpNoDelay (true);
675  setReuseAddress(true);
676  setKeepAlive (true);
677  setSendBufferSize (128*1024);
678  setReceiveBufferSize(128*1024);
679  setNonBlocking (false);
680 
681  return 0;
682  }
683  catch (const JSocketException& error) {
684  return Throw(error, -1);
685  }
686  }
void setReuseAddress(const bool on)
Set reuse address.
Definition: JSocket.hh:207
void setSendBufferSize(const int size)
Set send buffer size.
Definition: JSocket.hh:273
void setReceiveBufferSize(const int size)
Set receive buffer size.
Definition: JSocket.hh:251
void setNonBlocking(const bool on)
Set non-blocking of I/O.
Definition: JSocket.hh:110
void setKeepAlive(const bool on)
Set keep alive of socket.
Definition: JSocket.hh:152
void setTcpNoDelay(const bool on)
Set TCP no-delay.
Definition: JSocket.hh:229
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
int JNET::JSocketBlocking::read ( char *  buffer,
const int  length 
)
inlinevirtualinherited

Read data from socket.

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

Reimplemented from JNET::JSocket.

Definition at line 52 of file JSocketBlocking.hh.

53  {
54  char* data = buffer;
55  int size = length;
56 
57  while (size != 0) {
58 
59  const int pos = JSocket::read(data, size);
60 
61  if (pos != 0) {
62  data += pos;
63  size -= pos;
64  } else {
65  in_avail(1);
66  }
67  }
68 
69  return length - size;
70  }
bool in_avail(JTimeval timeout=JTimeval::min()) const
Check availability of input.
Definition: JFile.hh:100
virtual int read(char *buffer, const int length) override
Read data from socket.
Definition: JSocket.hh:301
int JNET::JSocketBlocking::write ( const char *  buffer,
const int  length 
)
inlinevirtualinherited

Write data to socket.

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

Reimplemented from JNET::JSocket.

Definition at line 80 of file JSocketBlocking.hh.

81  {
82  const char* data = buffer;
83  int size = length;
84 
85  while (size != 0) {
86 
87  const int pos = JSocket::write(data, size);
88 
89  if (pos != 0) {
90  data += pos;
91  size -= pos;
92  } else {
93  out_avail(1);
94  }
95  }
96 
97  return length - size;
98  }
bool out_avail(JTimeval timeout=JTimeval::min()) const
Check availability of output.
Definition: JFile.hh:113
virtual int write(const char *buffer, const int length) override
Write data to socket.
Definition: JSocket.hh:340
int JNET::JSocket::shutdown ( )
inlineinherited

Shut down socket.

Definition at line 95 of file JSocket.hh.

96  {
97  const int value = ::shutdown(getFileDescriptor(), SHUT_RDWR);
98 
99  close();
100 
101  return value;
102  }
void close()
Close file.
Definition: JFile.hh:55
int getFileDescriptor() const
Get file descriptor.
int shutdown()
Shut down socket.
Definition: JSocket.hh:95
void JNET::JSocket::setNonBlocking ( const bool  on)
inlineinherited

Set non-blocking of I/O.

Parameters
ontrue to enable non-blocking; false to disable

Definition at line 110 of file JSocket.hh.

111  {
112  const int flags = fcntl(getFileDescriptor(), F_GETFL, -1);
113  const int mask = FNDELAY;
114 
115  if (flags == -1) {
116  THROW(JSocketException, "Get socket option failed.");
117  }
118 
119  if (((flags & mask) != mask && on) ||
120  ((flags & mask) != 0 && !on) ) {
121 
122  if (fcntl(getFileDescriptor(), F_SETFL, flags ^ mask) < 0) {
123  THROW(JSocketException, "Set socket option failed.");
124  }
125  }
126  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
int getFileDescriptor() const
Get file descriptor.
bool JNET::JSocket::getNonBlocking ( ) const
inlineinherited

Get non-blocking of I/O.

Returns
true if enabled non-blocking; else false

Definition at line 134 of file JSocket.hh.

135  {
136  const int flags = fcntl(getFileDescriptor(), F_GETFL, -1);
137  const int mask = FNDELAY;
138 
139  if (flags == -1) {
140  THROW(JSocketException, "Get socket option failed.");
141  }
142 
143  return ((flags & mask) != 0);
144  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
int getFileDescriptor() const
Get file descriptor.
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 152 of file JSocket.hh.

153  {
154  setOption(SOL_SOCKET, SO_KEEPALIVE, int(on ? 1 : 0));
155  }
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:429
bool JNET::JSocket::getKeepAlive ( ) const
inlineinherited

Get keep alive of socket.

Returns
true if keep alive; else false

Definition at line 163 of file JSocket.hh.

164  {
165  return (getOption<int>(SOL_SOCKET, SO_KEEPALIVE) == 1);
166  }
void JNET::JSocket::setKeepIdle ( const int  t_s)
inlineinherited

Set the TCP idle time.

Parameters
t_stime [s]

Definition at line 174 of file JSocket.hh.

175  {
176  setOption(IPROTO_TCP, TCP_KEEPIDLE, t_s);
177  }
#define IPROTO_TCP
Definition: JSocket.hh:18
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:429
void JNET::JSocket::setKeepCnt ( const int  count)
inlineinherited

Set the TCP idle count.

Parameters
countcount

Definition at line 185 of file JSocket.hh.

186  {
187  setOption(IPROTO_TCP, TCP_KEEPCNT, count);
188  }
#define IPROTO_TCP
Definition: JSocket.hh:18
std::vector< int > count
Definition: JAlgorithm.hh:180
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:429
void JNET::JSocket::setKeepIntvl ( const int  t_s)
inlineinherited

Set the TCP interval time.

Parameters
t_stime [s]

Definition at line 196 of file JSocket.hh.

197  {
198  setOption(IPROTO_TCP, TCP_KEEPINTVL, t_s);
199  }
#define IPROTO_TCP
Definition: JSocket.hh:18
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:429
void JNET::JSocket::setReuseAddress ( const bool  on)
inlineinherited

Set reuse address.

Parameters
ontrue to enable reuse address; false to disable

Definition at line 207 of file JSocket.hh.

208  {
209  setOption(SOL_SOCKET, SO_REUSEADDR, int(on ? 1 : 0));
210  }
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:429
bool JNET::JSocket::getReuseAddress ( ) const
inlineinherited

Get reuse address.

Returns
true if enable reuse address; else false

Definition at line 218 of file JSocket.hh.

219  {
220  return (getOption<int>(SOL_SOCKET, SO_REUSEADDR) == 1);
221  }
void JNET::JSocket::setTcpNoDelay ( const bool  on)
inlineinherited

Set TCP no-delay.

Parameters
ontrue to set TCP no-delay; false to disable

Definition at line 229 of file JSocket.hh.

230  {
231  setOption(IPPROTO_TCP, TCP_NODELAY, int(on ? 1 : 0));
232  }
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:429
bool JNET::JSocket::getTcpNoDelay ( ) const
inlineinherited

Get TCP no-delay.

Returns
true if TCP no-delay; else false

Definition at line 240 of file JSocket.hh.

241  {
242  return (getOption<int>(IPPROTO_TCP, TCP_NODELAY) == 1);
243  }
void JNET::JSocket::setReceiveBufferSize ( const int  size)
inlineinherited

Set receive buffer size.

Parameters
sizenumber of bytes

Definition at line 251 of file JSocket.hh.

252  {
253  setOption(SOL_SOCKET, SO_RCVBUF, int(size));
254  }
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:429
int JNET::JSocket::getReceiveBufferSize ( ) const
inlineinherited

Set receive buffer size.

Returns
number of bytes

Definition at line 262 of file JSocket.hh.

263  {
264  return getOption<int>(SOL_SOCKET, SO_RCVBUF);
265  }
void JNET::JSocket::setSendBufferSize ( const int  size)
inlineinherited

Set send buffer size.

Parameters
sizenumber of bytes

Definition at line 273 of file JSocket.hh.

274  {
275  setOption(SOL_SOCKET, SO_SNDBUF, int(size));
276  }
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:429
int JNET::JSocket::getSendBufferSize ( ) const
inlineinherited

Get send buffer size.

Returns
number of bytes

Definition at line 284 of file JSocket.hh.

285  {
286  return getOption<int>(SOL_SOCKET, SO_SNDBUF);
287  }
void JNET::JSocket::accept ( const int  server)
inlineinherited

Accept connection from a server.

Parameters
serverfile descriptor of server socket

Definition at line 373 of file JSocket.hh.

374  {
375  socklen_t size = sizeof(sockaddr_in);
376  fileDescriptor = ::accept(server, getSockaddr(), &size);
377  }
const sockaddr * getSockaddr() const
Get sockaddr.
void accept(const int server)
Accept connection from a server.
Definition: JSocket.hh:373
void JNET::JSocket::connect ( const int  port)
inlineinherited

Connect to port on local host.

Parameters
portport number

Definition at line 385 of file JSocket.hh.

386  {
387  connect(INADDR_ANY, port);
388  }
void connect(const int port)
Connect to port on local host.
Definition: JSocket.hh:385
void JNET::JSocket::connect ( const std::string &  host_name,
const int  port 
)
inlineinherited

Connect to port on specified host.

Parameters
host_namehost name
portport number

Definition at line 397 of file JSocket.hh.

398  {
399  connect(JSYSTEM::getIPnumber(host_name), port);
400  }
int getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:117
void connect(const int port)
Connect to port on local host.
Definition: JSocket.hh:385
void JNET::JSocket::connect ( const int  ip_number,
const int  port 
)
inlineinherited

Connect to port on specified host.

Parameters
ip_numberIP number
portport number

Definition at line 409 of file JSocket.hh.

410  {
411  setIPnumber(ip_number);
412  setPort(port);
413 
414  if (::connect(getFileDescriptor(), getSockaddr(), sizeof(sockaddr_in)) < 0) {
415  THROW(JSocketException, "Socket connection failed " << getIPaddress(ip_number) << ":" << port);
416  }
417  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
std::string getIPaddress(const int ip)
Get IP address (decimal-dot notation).
Definition: JNetwork.hh:154
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.
void connect(const int port)
Connect to port on local host.
Definition: JSocket.hh:385
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 429 of file JSocket.hh.

430  {
431  socklen_t size = sizeof(T);
432 
433  if (setsockopt(getFileDescriptor(), level, option, &value, size) < 0) {
434  THROW(JSocketException, "Set socket option failed.");
435  }
436  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
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 447 of file JSocket.hh.

448  {
449  T value;
450  socklen_t size = sizeof(T);
451 
452  if (getsockopt(getFileDescriptor(), level, option, &value, &size) < 0) {
453  THROW(JSocketException, "Get socket option failed.");
454  }
455 
456  return value;
457  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
int getFileDescriptor() const
Get file descriptor.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
void JLANG::JFile::close ( )
inlineinherited

Close file.

Definition at line 55 of file JFile.hh.

56  {
57  if (fileDescriptor != FILE_CLOSED) {
58 
60 
62  result = 0;
63  }
64  }
void close()
Close file.
Definition: JFile.hh:55
static const int FILE_CLOSED
int result
Definition: JFile.hh:164
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 100 of file JFile.hh.

101  {
102  return JFileDescriptorMask(*this).in_avail(timeout);
103  }
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 113 of file JFile.hh.

114  {
115  return JFileDescriptorMask(*this).out_avail(timeout);
116  }
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 124 of file JFile.hh.

125  {
126  return is_open() && !eof() && !bad();
127  }
virtual bool eof() const
Check end of file.
Definition: JFile.hh:157
bool is_open() const
Get open status.
virtual bool bad() const
Check status.
Definition: JFile.hh:146
virtual bool JLANG::JFile::fail ( ) const
inlinevirtualinherited

Check status.

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

Definition at line 135 of file JFile.hh.

136  {
137  return result == 0;
138  }
int result
Definition: JFile.hh:164
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 146 of file JFile.hh.

147  {
148  return fail();
149  }
virtual bool fail() const
Check status.
Definition: JFile.hh:135
virtual bool JLANG::JFile::eof ( ) const
inlinevirtualinherited

Check end of file.

Returns
true if end of file; else false

Definition at line 157 of file JFile.hh.

158  {
159  return result == EOF;
160  }
int result
Definition: JFile.hh:164
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  }
bool JLANG::JAbstractFile::is_open ( ) const
inlineinherited

Get open status.

Definition at line 84 of file JAbstractFile.hh.

85  {
86  return fileDescriptor != FILE_CLOSED;
87  }
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:696
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  }
static void JLANG::JThrow< JControlHost >::Throw ( const bool  option)
inlinestaticinherited

Enable/disable throw option.

Parameters
optiontrue enable; false disable

Definition at line 37 of file JThrow.hh.

38  {
39  do_throw = option;
40  }
static bool do_throw
throw option
Definition: JThrow.hh:28
static int JLANG::JThrow< JControlHost >::Throw ( const JException error,
const int  value = -1 
)
inlinestaticinherited

Throw exception or return error.

Parameters
errorexception
valuereturn code
Returns
return code

Definition at line 50 of file JThrow.hh.

51  {
52  using namespace std;
53 
54  if (do_throw) {
55  throw error;
56  }
57 
58  cerr << error.what() << endl;
59 
60  return value;
61  }
virtual const char * what() const override
Get error message.
Definition: JException.hh:48
static bool do_throw
throw option
Definition: JThrow.hh:28
const JTag& JNET::JTag::getTag ( ) const
inlineinherited

Get tag.

Returns
tag

Definition at line 86 of file JTag.hh.

87  {
88  return static_cast<const JTag&>(*this);
89  }
ControlHost tag.
Definition: JTag.hh:38
JTag& JNET::JTag::getTag ( )
inlineinherited

Get tag.

Returns
tag

Definition at line 97 of file JTag.hh.

98  {
99  return static_cast<JTag&>(*this);
100  }
ControlHost tag.
Definition: JTag.hh:38
void JNET::JTag::setTag ( const JTag tag)
inlineinherited

Set tag.

Parameters
tagtag

Definition at line 108 of file JTag.hh.

109  {
110  id = tag.getID();
111  }
JTag_t getID() const
Get identifier.
Definition: JTag.hh:160
void JNET::JTag::setTag ( const std::string &  tag)
inlineinherited

Set tag.

This method may throw an exception.

Parameters
tagtag

Definition at line 121 of file JTag.hh.

122  {
123  if (!tag.empty() && tag.size() <= TAGSIZE) {
124 
125  char* __p = (char*) &(this->id);
126 
127  size_t i = 0;
128 
129  for ( ; i != tag.size(); ++i) {
130  __p[i] = tag[i];
131  }
132 
133  for ( ; i != TAGSIZE; ++i) {
134  __p[i] = '\0';
135  }
136 
137  } else {
138 
139  THROW(JControlHostException, "Invalid tag length <" << tag << "> " << tag.size());
140  }
141  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
static const size_t TAGSIZE
Size of ControlHost tag.
Definition: JTag.hh:32
void JNET::JTag::setTag ( const JTag_t  id)
inlineinherited

Set tag.

Parameters
ididentifier

Definition at line 149 of file JTag.hh.

150  {
151  this->id = id;
152  }
JTag_t id
Definition: JTag.hh:254
JTag_t JNET::JTag::getID ( ) const
inlineinherited

Get identifier.

Returns
identifier

Definition at line 160 of file JTag.hh.

161  {
162  return id;
163  }
JTag_t id
Definition: JTag.hh:254
std::string JNET::JTag::toString ( ) const
inlineinherited

Convert tag to string.

Returns
tag

Definition at line 171 of file JTag.hh.

172  {
173  int pos = TAGSIZE;
174 
175  const char* __p = (const char*) &(this->id);
176 
177  while (pos != 0 && __p[pos-1] == '\0') {
178  --pos;
179  }
180 
181  return std::string(__p, pos);
182  }
static const size_t TAGSIZE
Size of ControlHost tag.
Definition: JTag.hh:32
JNET::JTag::operator std::string ( ) const
inlineinherited

Type conversion operators.

Returns
tag

Definition at line 190 of file JTag.hh.

191  {
192  return toString();
193  }
std::string toString() const
Convert tag to string.
Definition: JTag.hh:171
const char* JNET::JTag::c_str ( ) const
inlineinherited

C-string.

Returns
tag

Definition at line 201 of file JTag.hh.

202  {
203  return (const char*) &(this->id);
204  }
JTag_t id
Definition: JTag.hh:254
const char JNET::JTag::operator[] ( int  i) const
inlineinherited

Get character.

Parameters
iindex
Returns
character at index

Definition at line 213 of file JTag.hh.

214  {
215  const char* __p = (const char*) &(this->id);
216 
217  return __p[i];
218  }

Member Data Documentation

template<class T>
JByteArrayWriter JNET::JControlHostObjectOutput< T >::out
private

Definition at line 99 of file JControlHostObjectOutput.hh.

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

Definition at line 27 of file JAbstractFile.hh.

int JLANG::JAbstractFile::fileDescriptor
protectedinherited

Definition at line 91 of file JAbstractFile.hh.

bool JLANG::JThrow< JControlHost >::do_throw
staticprotectedinherited

throw option

Set default throw option to true.

Definition at line 28 of file JThrow.hh.

JTag_t JNET::JTag::id
protectedinherited

Definition at line 254 of file JTag.hh.


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