Jpp
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Attributes | List of all members
JLOGGER::JControlHostLogger Class Reference

Message logging based on ControlHost. More...

#include <JControlHostLogger.hh>

Inheritance diagram for JLOGGER::JControlHostLogger:
JLOGGER::JLogger JNET::JControlHost JNET::JSocketBlocking JLANG::JThrow< JControlHost > JNET::JSocket JLANG::JFile JNET::JSocketAddress JLANG::JAbstractFile JLANG::JBinaryInput JLANG::JBinaryOutput JLANG::JComparable< JAbstractFile >

Public Member Functions

 JControlHostLogger (std::string const &host_name)
 Constructor. More...
 
virtual void typeout (const std::string &tag, const std::string &message)
 Report message. More...
 
JSYNCHRONIZATION::JMutexmutex ()
 Provides internal lock object. More...
 

Protected Member Functions

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...
 
template<class T >
void setOption (const int level, const int option, const T value)
 Set socket option. More...
 
template<class T >
getOption (const int level, const int option) const
 Get socket option. 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...
 

Static Protected 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...
 

Protected Attributes

int fileDescriptor
 

Static Protected Attributes

static const int FILE_CLOSED = -1
 
static bool do_throw
 throw option More...
 

Private Attributes

JSYNCHRONIZATION::JMutex m_mutex
 
JPrefix prefix
 
int result
 

Detailed Description

Message logging based on ControlHost.

This class implements the JLogger interface.

Definition at line 26 of file JControlHostLogger.hh.

Constructor & Destructor Documentation

◆ JControlHostLogger()

JLOGGER::JControlHostLogger::JControlHostLogger ( std::string const &  host_name)
inline

Constructor.

Parameters
host_namehost name

Definition at line 36 of file JControlHostLogger.hh.

36  :
37  JControlHost(host_name)
38  {}

Member Function Documentation

◆ typeout()

virtual void JLOGGER::JControlHostLogger::typeout ( const std::string &  tag,
const std::string &  message 
)
inlinevirtual

Report message.

Parameters
tagtag
messagemessage

Implements JLOGGER::JLogger.

Definition at line 47 of file JControlHostLogger.hh.

49  {
50  this->PutFullString(tag, message);
51  }

◆ mutex()

JSYNCHRONIZATION::JMutex& JLOGGER::JLogger::mutex ( )
inlineinherited

Provides internal lock object.

Returns
reference to the internal mutex object.

Definition at line 44 of file JLogger.hh.

45  {
46  return m_mutex;
47  }

◆ maybe_special()

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 269 of file JControlHost.hh.

270  {
271  return tag[0] == '_';
272  }

◆ is_valid()

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 281 of file JControlHost.hh.

282  {
283  return (c == SUBSCRIBE_ALL ||
284  c == SUBSCRIBE_ANY);// ||
285  //c == SUBSCRIBE_SHARED_MEMORY);
286  }

◆ Subscribe() [1/2]

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 363 of file JControlHost.hh.

364  {
365  return PutFullString(DISPTAG_Subscribe, subscription.toString());
366  }

◆ Subscribe() [2/2]

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 375 of file JControlHost.hh.

376  {
377  return PutFullString(DISPTAG_Subscribe, subscription.toString());
378  }

◆ MyId()

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 387 of file JControlHost.hh.

388  {
389  return PutFullString(DISPTAG_MyId, nick_name);
390  }

◆ SendMeNext()

int JNET::JControlHost::SendMeNext ( )
inlineinherited

Tell server to send next message.

Returns
0 if OK; -1 if socket error

Definition at line 398 of file JControlHost.hh.

399  {
400  return PutFullData(DISPTAG_Gime, NULL, 0);
401  }

◆ SendMeAlways()

int JNET::JControlHost::SendMeAlways ( )
inlineinherited

Tell server to send messages forever.

Returns
0 if OK; -1 if socket error

Definition at line 409 of file JControlHost.hh.

410  {
411  return PutFullData(DISPTAG_Always, NULL, 0);
412  }

◆ PutFullData() [1/2]

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 423 of file JControlHost.hh.

424  {
425  try {
426 
427  JPrefix __prefix__(tag, length);
428 
429  write((char*) &__prefix__, sizeof(JPrefix));
430 
431  if (length != 0) {
432  write((char*) buffer, (int) length);
433  }
434 
435  return 0;
436  }
437  catch (const JSocketException& error) {
438  return Throw(error, -1);
439  }
440  }

◆ PutFullData() [2/2]

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 451 of file JControlHost.hh.

452  {
453  try {
454  return PutFullData(JTag(tag), buffer, length);
455  }
456  catch (const JSocketException& error) {
457  return Throw(error, -1);
458  }
459  catch (const JControlHostException& error) {
460  return Throw(error, -2);
461  }
462  }

◆ PutFullString() [1/2]

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 472 of file JControlHost.hh.

474  {
475  return PutFullData(tag, buffer.c_str(), buffer.size());
476  }

◆ PutFullString() [2/2]

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 486 of file JControlHost.hh.

488  {
489  return PutFullData(tag, buffer.c_str(), buffer.size());
490  }

◆ Connected()

int JNET::JControlHost::Connected ( )
inlineinherited

Send version.

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

Definition at line 498 of file JControlHost.hh.

499  {
501  }

◆ WaitHead() [1/2]

int JNET::JControlHost::WaitHead ( JPrefix prefix)
inlineinherited

Wait for header.

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

Definition at line 510 of file JControlHost.hh.

511  {
512  try {
513 
514  read((char*) &this->prefix, sizeof(JPrefix));
515 
516  prefix = this->prefix;
517 
518  return 0;
519  }
520  catch (const JSocketException& error) {
521  return Throw(error, -1);
522  }
523  }

◆ WaitHead() [2/2]

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 533 of file JControlHost.hh.

534  {
535  const int rvalue = WaitHead(this->prefix);
536 
537  if (rvalue == 0) {
538  tag = this->prefix.getTag();
539  length = this->prefix.getSize();
540  }
541 
542  return rvalue;
543  }

◆ CheckHead() [1/2]

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 553 of file JControlHost.hh.

554  {
555  try {
556 
557  if (in_avail(timeout)) {
558 
559  WaitHead(prefix);
560 
561  return 1;
562 
563  } else {
564 
565  return 0;
566  }
567  }
568  catch (const JSocketException& error) {
569  return Throw(error, -1);
570  }
571  }

◆ CheckHead() [2/2]

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 582 of file JControlHost.hh.

583  {
584  const int rvalue = CheckHead(this->prefix, timeout_us);
585 
586  if (rvalue == 1) {
587  tag = this->prefix.getTag();
588  length = this->prefix.getSize();
589  }
590 
591  return rvalue;
592  }

◆ GetFullData()

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 602 of file JControlHost.hh.

603  {
604  try {
605 
606  read((char*) buffer, (int) length);
607 
608  return 0;
609  }
610  catch (const JSocketException& error) {
611  return Throw(error, -1);
612  }
613  }

◆ GetFullString()

int JNET::JControlHost::GetFullString ( std::string &  buffer)
inlineinherited

Receive string.

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

Definition at line 622 of file JControlHost.hh.

623  {
624  buffer.resize(this->prefix.getSize());
625 
626  return GetFullData((char*) buffer.data(), buffer.size());
627  }

◆ WhereIs()

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 638 of file JControlHost.hh.

641  {
642  try {
643 
644  using namespace std;
645 
646  JControlHost socket(host_name);
647 
648  socket.PutFullString(DISPTAG_WhereIs, nick_name);
649 
650  string tag;
651  long long int length;
652 
653  socket.WaitHead(tag, length);
654  socket.GetFullString(answer);
655 
656  return 0;
657  }
658  catch (const JSocketException& error) {
659  return Throw(error, -1);
660  }
661  }

◆ configure()

int JNET::JControlHost::configure ( )
inlineinherited

Configure socket (factory reset).

Returns
0 if OK; -1 if socket error

Definition at line 669 of file JControlHost.hh.

670  {
671  try {
672 
673  setTcpNoDelay (true);
674  setReuseAddress(true);
675  setKeepAlive (true);
676  setSendBufferSize (128*1024);
677  setReceiveBufferSize(128*1024);
678  setNonBlocking (false);
679 
680  return 0;
681  }
682  catch (const JSocketException& error) {
683  return Throw(error, -1);
684  }
685  }

◆ read()

int JNET::JSocketBlocking::read ( char *  buffer,
const int  length 
)
inlinevirtualinherited

Read data from socket.

Parameters
bufferbuffer
lengthnumber of bytes to read

Reimplemented from JNET::JSocket.

Definition at line 51 of file JSocketBlocking.hh.

52  {
53  char* data = buffer;
54  int size = length;
55 
56  while (size != 0) {
57 
58  const int pos = JSocket::read(data, size);
59 
60  if (pos != 0) {
61  data += pos;
62  size -= pos;
63  } else {
64  in_avail(1);
65  }
66  }
67 
68  return length - size;
69  }

◆ write()

int JNET::JSocketBlocking::write ( const char *  buffer,
const int  length 
)
inlinevirtualinherited

Write data to socket.

Parameters
bufferbuffer
lengthnumber of bytes to write

Reimplemented from JNET::JSocket.

Definition at line 78 of file JSocketBlocking.hh.

79  {
80  const char* data = buffer;
81  int size = length;
82 
83  while (size != 0) {
84 
85  const int pos = JSocket::write(data, size);
86 
87  if (pos != 0) {
88  data += pos;
89  size -= pos;
90  } else {
91  out_avail(1);
92  }
93  }
94 
95  return length - size;
96  }

◆ shutdown()

int JNET::JSocket::shutdown ( )
inlineinherited

Shut down socket.

Definition at line 94 of file JSocket.hh.

95  {
96  const int value = ::shutdown(getFileDescriptor(), SHUT_RDWR);
97 
98  close();
99 
100  return value;
101  }

◆ setNonBlocking()

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 109 of file JSocket.hh.

110  {
111  const int flags = fcntl(getFileDescriptor(), F_GETFL, -1);
112  const int mask = FNDELAY;
113 
114  if (flags == -1) {
115  THROW(JSocketException, "Get socket option failed.");
116  }
117 
118  if (((flags & mask) != mask && on) ||
119  ((flags & mask) != 0 && !on) ) {
120 
121  if (fcntl(getFileDescriptor(), F_SETFL, flags ^ mask) < 0) {
122  THROW(JSocketException, "Set socket option failed.");
123  }
124  }
125  }

◆ getNonBlocking()

bool JNET::JSocket::getNonBlocking ( ) const
inlineinherited

Get non-blocking of I/O.

Returns
true if enabled non-blocking; else false

Definition at line 133 of file JSocket.hh.

134  {
135  const int flags = fcntl(getFileDescriptor(), F_GETFL, -1);
136  const int mask = FNDELAY;
137 
138  if (flags == -1) {
139  THROW(JSocketException, "Get socket option failed.");
140  }
141 
142  return ((flags & mask) != 0);
143  }

◆ setKeepAlive()

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 151 of file JSocket.hh.

152  {
153  setOption(SOL_SOCKET, SO_KEEPALIVE, int(on ? 1 : 0));
154  }

◆ getKeepAlive()

bool JNET::JSocket::getKeepAlive ( ) const
inlineinherited

Get keep alive of socket.

Returns
true if keep alive; else false

Definition at line 162 of file JSocket.hh.

163  {
164  return (getOption<int>(SOL_SOCKET, SO_KEEPALIVE) == 1);
165  }

◆ setKeepIdle()

void JNET::JSocket::setKeepIdle ( const int  t_s)
inlineinherited

Set the TCP idle time.

Parameters
t_stime [s]

Definition at line 173 of file JSocket.hh.

174  {
175  setOption(IPROTO_TCP, TCP_KEEPIDLE, t_s);
176  }

◆ setKeepCnt()

void JNET::JSocket::setKeepCnt ( const int  count)
inlineinherited

Set the TCP idle count.

Parameters
countcount

Definition at line 184 of file JSocket.hh.

185  {
186  setOption(IPROTO_TCP, TCP_KEEPCNT, count);
187  }

◆ setKeepIntvl()

void JNET::JSocket::setKeepIntvl ( const int  t_s)
inlineinherited

Set the TCP interval time.

Parameters
t_stime [s]

Definition at line 195 of file JSocket.hh.

196  {
197  setOption(IPROTO_TCP, TCP_KEEPINTVL, t_s);
198  }

◆ setReuseAddress()

void JNET::JSocket::setReuseAddress ( const bool  on)
inlineinherited

Set reuse address.

Parameters
ontrue to enable reuse address; false to disable

Definition at line 206 of file JSocket.hh.

207  {
208  setOption(SOL_SOCKET, SO_REUSEADDR, int(on ? 1 : 0));
209  }

◆ getReuseAddress()

bool JNET::JSocket::getReuseAddress ( ) const
inlineinherited

Get reuse address.

Returns
true if enable reuse address; else false

Definition at line 217 of file JSocket.hh.

218  {
219  return (getOption<int>(SOL_SOCKET, SO_REUSEADDR) == 1);
220  }

◆ setTcpNoDelay()

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 228 of file JSocket.hh.

229  {
230  setOption(IPPROTO_TCP, TCP_NODELAY, int(on ? 1 : 0));
231  }

◆ getTcpNoDelay()

bool JNET::JSocket::getTcpNoDelay ( ) const
inlineinherited

Get TCP no-delay.

Returns
true if TCP no-delay; else false

Definition at line 239 of file JSocket.hh.

240  {
241  return (getOption<int>(IPPROTO_TCP, TCP_NODELAY) == 1);
242  }

◆ setReceiveBufferSize()

void JNET::JSocket::setReceiveBufferSize ( const int  size)
inlineinherited

Set receive buffer size.

Parameters
sizenumber of bytes

Definition at line 250 of file JSocket.hh.

251  {
252  setOption(SOL_SOCKET, SO_RCVBUF, int(size));
253  }

◆ getReceiveBufferSize()

int JNET::JSocket::getReceiveBufferSize ( ) const
inlineinherited

Set receive buffer size.

Returns
number of bytes

Definition at line 261 of file JSocket.hh.

262  {
263  return getOption<int>(SOL_SOCKET, SO_RCVBUF);
264  }

◆ setSendBufferSize()

void JNET::JSocket::setSendBufferSize ( const int  size)
inlineinherited

Set send buffer size.

Parameters
sizenumber of bytes

Definition at line 272 of file JSocket.hh.

273  {
274  setOption(SOL_SOCKET, SO_SNDBUF, int(size));
275  }

◆ getSendBufferSize()

int JNET::JSocket::getSendBufferSize ( ) const
inlineinherited

Get send buffer size.

Returns
number of bytes

Definition at line 283 of file JSocket.hh.

284  {
285  return getOption<int>(SOL_SOCKET, SO_SNDBUF);
286  }

◆ accept()

void JNET::JSocket::accept ( const int  server)
inlineinherited

Accept connection from a server.

Parameters
serverfile descriptor of server socket

Definition at line 372 of file JSocket.hh.

373  {
374  socklen_t size = sizeof(sockaddr_in);
375  fileDescriptor = ::accept(server, getSockaddr(), &size);
376  }

◆ connect() [1/3]

void JNET::JSocket::connect ( const int  port)
inlineinherited

Connect to port on local host.

Parameters
portport number

Definition at line 384 of file JSocket.hh.

385  {
386  connect(INADDR_ANY, port);
387  }

◆ connect() [2/3]

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 396 of file JSocket.hh.

397  {
398  connect(JSYSTEM::getIPnumber(host_name), port);
399  }

◆ connect() [3/3]

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 408 of file JSocket.hh.

409  {
410  setIPnumber(ip_number);
411  setPort(port);
412 
413  if (::connect(getFileDescriptor(), getSockaddr(), sizeof(sockaddr_in)) < 0) {
414  THROW(JSocketException, "Socket connection failed.");
415  }
416  }

◆ setOption()

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 428 of file JSocket.hh.

429  {
430  socklen_t size = sizeof(T);
431 
432  if (setsockopt(getFileDescriptor(), level, option, &value, size) < 0) {
433  THROW(JSocketException, "Set socket option failed.");
434  }
435  }

◆ getOption()

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 446 of file JSocket.hh.

447  {
448  T value;
449  socklen_t size = sizeof(T);
450 
451  if (getsockopt(getFileDescriptor(), level, option, &value, &size) < 0) {
452  THROW(JSocketException, "Get socket option failed.");
453  }
454 
455  return value;
456  }

◆ close()

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  }

◆ in_avail()

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  }

◆ out_avail()

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  }

◆ good()

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  }

◆ fail()

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  }

◆ bad()

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  }

◆ eof()

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  }

◆ less()

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  }

◆ getFileDescriptor()

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  }

◆ is_open()

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  }

◆ getSockaddr() [1/2]

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  }

◆ getSockaddr() [2/2]

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  }

◆ getFamily()

int JNET::JSocketAddress::getFamily ( ) const
inlineinherited

Get family.

Returns
family

Definition at line 66 of file JSocketAddress.hh.

67  {
68  return sin_family;
69  }

◆ setFamily()

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  }

◆ getHostname()

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  }

◆ getIPnumber()

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  }

◆ setIPnumber() [1/2]

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  }

◆ setIPnumber() [2/2]

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  }

◆ getPort()

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  }

◆ setPort()

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  }

◆ sizeOf()

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  }

◆ Throw() [1/2]

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  }

◆ Throw() [2/2]

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  }

Member Data Documentation

◆ m_mutex

JSYNCHRONIZATION::JMutex JLOGGER::JLogger::m_mutex
privateinherited

Definition at line 51 of file JLogger.hh.

◆ prefix

JPrefix JNET::JControlHost::prefix
mutableprivateinherited

Definition at line 689 of file JControlHost.hh.

◆ result

int JLANG::JFile::result
privateinherited

Definition at line 164 of file JFile.hh.

◆ FILE_CLOSED

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

Definition at line 27 of file JAbstractFile.hh.

◆ fileDescriptor

int JLANG::JAbstractFile::fileDescriptor
protectedinherited

Definition at line 91 of file JAbstractFile.hh.

◆ do_throw

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

throw option

Set default throw option to true.

Definition at line 28 of file JThrow.hh.


The documentation for this class was generated from the following file:
JLANG::JAbstractFile::getFileDescriptor
int getFileDescriptor() const
Get file descriptor.
Definition: JAbstractFile.hh:75
JNET::JControlHost::PutFullString
int PutFullString(const JTag &tag, const std::string &buffer)
Send string.
Definition: JControlHost.hh:472
JNET::JSocket::read
virtual int read(char *buffer, const int length)
Read data from socket.
Definition: JSocket.hh:300
JLOGGER::JLogger::m_mutex
JSYNCHRONIZATION::JMutex m_mutex
Definition: JLogger.hh:51
JNET::DISPTAG_Subscribe
static const JTag DISPTAG_Subscribe("_Subscri")
Special ControlHost tags.
JNET::DISPTAG_Version
static const JTag DISPTAG_Version("_Version")
JNET::JPrefix
ControlHost prefix.
Definition: JPrefix.hh:31
JNET::DISPTAG_WhereIs
static const JTag DISPTAG_WhereIs("_WhereIs")
JNET::JSocketBlocking::write
int write(const char *buffer, const int length)
Write data to socket.
Definition: JSocketBlocking.hh:78
JNET::SUBSCRIBE_ALL
Definition: JControlHost.hh:40
JNET::JSocketBlocking::read
int read(char *buffer, const int length)
Read data from socket.
Definition: JSocketBlocking.hh:51
JNET::JTag::getTag
const JTag & getTag() const
Get tag.
Definition: JTag.hh:82
JNET::JSocket::setSendBufferSize
void setSendBufferSize(const int size)
Set send buffer size.
Definition: JSocket.hh:272
JNET::JControlHost::GetFullData
int GetFullData(void *buffer, long long int length)
Receive data.
Definition: JControlHost.hh:602
JNET::JSocket::setReuseAddress
void setReuseAddress(const bool on)
Set reuse address.
Definition: JSocket.hh:206
JNET::JSocketAddress::getSockaddr
const sockaddr * getSockaddr() const
Get sockaddr.
Definition: JSocketAddress.hh:44
JNET::JSocket::setTcpNoDelay
void setTcpNoDelay(const bool on)
Set TCP no-delay.
Definition: JSocket.hh:228
JNET::JSocket::shutdown
int shutdown()
Shut down socket.
Definition: JSocket.hh:94
JNET::JSocket::setReceiveBufferSize
void setReceiveBufferSize(const int size)
Set receive buffer size.
Definition: JSocket.hh:250
JNET::SUBSCRIBE_ANY
Definition: JControlHost.hh:41
JNET::JControlHost::JControlHost
JControlHost()
Default constructor.
Definition: JControlHost.hh:255
JLANG::JFile::fail
virtual bool fail() const
Check status.
Definition: JFile.hh:135
JLANG::JFile::in_avail
bool in_avail(JTimeval timeout=JTimeval::min()) const
Check availability of input.
Definition: JFile.hh:100
JLANG::JAbstractFile::FILE_CLOSED
static const int FILE_CLOSED
Definition: JAbstractFile.hh:27
JNET::DISPTAG_Always
static const JTag DISPTAG_Always("_Always")
JNET::JTag
ControlHost tag.
Definition: JTag.hh:35
IPROTO_TCP
#define IPROTO_TCP
Definition: JSocket.hh:18
JLANG::JFile::close
void close()
Close file.
Definition: JFile.hh:55
JNET::JSocket::accept
void accept(const int server)
Accept connection from a server.
Definition: JSocket.hh:372
JNET::JSocket::write
virtual int write(const char *buffer, const int length)
Write data to socket.
Definition: JSocket.hh:339
JLANG::JFileDescriptorMask::in_avail
bool in_avail(JTimeval timeout=JTimeval::min())
Check availability of input.
Definition: JFileDescriptorMask.hh:264
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
JNET::JSocketAddress::setIPnumber
void setIPnumber()
Set any IP number.
Definition: JSocketAddress.hh:119
JLANG::JFileDescriptorMask
Auxiliary class for method select.
Definition: JFileDescriptorMask.hh:24
JNET::CHOO_VERSION
static const std::string CHOO_VERSION
Definition: JControlHost.hh:33
JNET::JControlHost::WaitHead
int WaitHead(JPrefix &prefix)
Wait for header.
Definition: JControlHost.hh:510
JLANG::JAbstractFile::fileDescriptor
int fileDescriptor
Definition: JAbstractFile.hh:91
JNET::JControlHost
ControlHost class.
Definition: JControlHost.hh:247
JNET::JSocket::setKeepAlive
void setKeepAlive(const bool on)
Set keep alive of socket.
Definition: JSocket.hh:151
JNET::JSocketAddress::getIPnumber
int getIPnumber() const
Get IP number.
Definition: JSocketAddress.hh:99
JNET::JSocket::setOption
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:428
JNET::JSubscription::toString
std::string toString() const
Convert subscription to string.
Definition: JControlHost.hh:82
JLANG::JThrow< JControlHost >::Throw
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
JNET::JControlHost::PutFullData
int PutFullData(const JTag &tag, const void *buffer, const long long int length)
Send data.
Definition: JControlHost.hh:423
JNET::JSocket::connect
void connect(const int port)
Connect to port on local host.
Definition: JSocket.hh:384
JLANG::JFile::eof
virtual bool eof() const
Check end of file.
Definition: JFile.hh:157
JSYSTEM::getHostname
std::string getHostname()
Get host name.
Definition: JNetwork.hh:77
JNET::JSocket::setNonBlocking
void setNonBlocking(const bool on)
Set non-blocking of I/O.
Definition: JSocket.hh:109
std
Definition: jaanetDictionary.h:36
JNET::JControlHost::prefix
JPrefix prefix
Definition: JControlHost.hh:689
JNET::JSocketAddress::setPort
void setPort(const int port)
Set port number.
Definition: JSocketAddress.hh:141
JLANG::JThrow< JControlHost >::do_throw
static bool do_throw
throw option
Definition: JThrow.hh:28
JLANG::JException::what
virtual const char * what() const
Get error message.
Definition: JException.hh:48
JNET::JControlHost::CheckHead
int CheckHead(JPrefix &prefix, JTimeval timeout=JTimeval::min())
Check for header, without waiting.
Definition: JControlHost.hh:553
JSYSTEM::getIPnumber
int getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:117
JNET::DISPTAG_MyId
static const JTag DISPTAG_MyId("_MyId")
JNET::JPrefix::getSize
int getSize() const
Get size.
Definition: JPrefix.hh:62
JLANG::JFile::out_avail
bool out_avail(JTimeval timeout=JTimeval::min()) const
Check availability of output.
Definition: JFile.hh:113
JLANG::JFileDescriptorMask::out_avail
bool out_avail(JTimeval timeout=JTimeval::min())
Check availability of output.
Definition: JFileDescriptorMask.hh:278
JLANG::JAbstractFile::is_open
bool is_open() const
Get open status.
Definition: JAbstractFile.hh:84
JNET::JSubscriptionList::toString
std::string toString() const
Convert subscription list to string.
Definition: JControlHost.hh:214
JLANG::JFile::bad
virtual bool bad() const
Check status.
Definition: JFile.hh:146
JNET::DISPTAG_Gime
static const JTag DISPTAG_Gime("_Gime")
JLANG::JFile::result
int result
Definition: JFile.hh:164