48 while (victim != NULL) {
61 fprintf(stderr,
"vrpn_TextPrinter::~vrpn_TextPrinter: delete failed\n");
86 "vrpn_TextPrinter::add_object(): NULL pointer passed\n");
91 printf(
"vrpn_TextPrinter: adding object %s\n", o->
d_servicename);
98 while (victim != NULL) {
103 victim = victim->
next;
109 fprintf(stderr,
"vrpn_TextPrinter::add_object(): out of memory\n");
122 "vrpn_TextPrinter::add_object(): Can't register callback\n");
127 fprintf(stderr,
"vrpn_TextPrinter::add_object: delete failed\n");
147 printf(
"vrpn_TextPrinter: removing object %s\n", o->
d_servicename);
153 "vrpn_TextPrinter::remove_object(): NULL pointer passed\n");
163 while ((victim != NULL) &&
167 snitch = &((*snitch)->next);
168 victim = victim->
next;
172 if (victim != NULL) {
181 fprintf(stderr,
"vrpn_TextPrinter::remove_object(): Can't "
182 "unregister callback\n");
187 *snitch = victim->
next;
191 fprintf(stderr,
"vrpn_TextPrinter::remove_object: delete failed\n");
222 printf(
"vrpn_TextPrinter: text handler called\n");
232 message, &severity, &level, p.
buffer) != 0) {
235 "vrpn_TextPrinter::text_message_handler(): Can't decode message\n");
262 fprintf(me->
d_ostream,
"UNKNOWN SEVERITY\n");
266 fprintf(me->
d_ostream,
" (%d) from %s: %s\n", level,
318 if (firstTimeCalled) {
370 fprintf(stderr,
"vrpn_BaseClassUnique: Can't register IDs\n");
378 d_connection->register_message_type(
"vrpn_Base text_message");
381 "vrpn_BaseClassUnique: Can't register Text type ID\n");
386 d_connection->register_message_type(
"vrpn_Base ping_message");
389 "vrpn_BaseClassUnique: Can't register ping type ID\n");
394 d_connection->register_message_type(
"vrpn_Base pong_message");
397 "vrpn_BaseClassUnique: Can't register pong type ID\n");
442 , d_num_autodeletions(0)
443 , d_first_mainloop(1)
444 , d_unanswered_ping(0)
448 d_time_first_ping.tv_sec = d_time_first_ping.tv_usec = 0;
462 for (i = 0; i < d_num_autodeletions; i++) {
464 d_handler_autodeletion_record[i].
type,
465 d_handler_autodeletion_record[i].
handler,
466 d_handler_autodeletion_record[i].
userdata,
467 d_handler_autodeletion_record[i].
sender);
469 d_num_autodeletions = 0;
486 fprintf(stderr,
"vrpn_BaseClassUnique::~vrpn_BaseClassUnique: delete failed\n");
510 fprintf(stderr,
"vrpn_BaseClassUnique::register_autodeleted_handler: "
511 "No vrpn_Connection.\n");
518 "vrpn_BaseClassUnique::register_autodeleted_handler: "
519 "Too many handlers registered. Increase vrpn_MAX_BCADRS "
520 "and recompile VRPN. Please report to vrpn@cs.unc.edu.\n");
525 d_handler_autodeletion_record[d_num_autodeletions].handler =
handler;
526 d_handler_autodeletion_record[d_num_autodeletions].sender =
sender;
527 d_handler_autodeletion_record[d_num_autodeletions].type =
type;
528 d_handler_autodeletion_record[d_num_autodeletions].userdata =
userdata;
529 d_num_autodeletions++;
540 vrpn_uint32 severity_as_uint = severity;
554 const char *bufptr = buf;
555 vrpn_uint32 severity_as_uint;
570 struct timeval timestamp,
575 size_t len = strlen(msg) + 1;
578 fprintf(stderr,
"vrpn_BaseClassUnique::send_message: Attempt to encode "
579 "string that is too long\n");
612 d_first_mainloop = 0;
659 handle_connection_dropped,
this);
662 initiate_ping_cycle();
665 d_first_mainloop = 0;
676 if (d_unanswered_ping) {
682 if (diff.tv_sec >= 1) {
690 d_time_last_warned = now;
694 if (diff.tv_sec >= 10) {
696 "No response from server for >= 10 seconds", now,
700 else if (diff.tv_sec >= 3) {
702 "No response from server for >= 3 seconds", now,
710void vrpn_BaseClassUnique::initiate_ping_cycle(
void)
716 d_unanswered_ping = 1;
719 d_time_last_warned.tv_sec = d_time_last_warned.tv_usec = 0;
733 me->d_unanswered_ping = 0;
736 if (me->d_flatline) {
774int vrpn_BaseClassUnique::handle_connection_dropped(
void *userdata,
780 if (me->d_unanswered_ping != 0) {
786 me->initiate_ping_cycle();
An RAII lock/guard class for vrpn_Semaphore.
static int decode_text_message_from_buffer(char *msg, vrpn_TEXT_SEVERITY *severity, vrpn_uint32 *level, const char *buf)
Decodes the body of the text message from a buffer from the connection.
vrpn_Connection * connectionPtr()
Returns a pointer to the connection this object is using.
int register_autodeleted_handler(vrpn_int32 type, vrpn_MESSAGEHANDLER handler, void *userdata, vrpn_int32 sender=vrpn_ANY_SENDER)
Registers a handler with the connection, and remembers to delete at destruction.
vrpn_Connection * d_connection
Connection that this object talks to.
void client_mainloop(void)
Handles functions that all clients should provide in their mainloop() (warning of no server,...
vrpn_MESSAGEHANDLER handler
vrpn_int32 d_pong_message_id
Server telling that it is there.
vrpn_int32 d_sender_id
Sender ID registered with the connection.
void server_mainloop(void)
Handles functions that all servers should provide in their mainloop() (ping/pong, for example) Should...
vrpn_int32 d_text_message_id
ID for text messages.
virtual ~vrpn_BaseClassUnique()
Unregister all of the message handlers that were to be autodeleted.
int send_text_message(const char *msg, struct timeval timestamp, vrpn_TEXT_SEVERITY type=vrpn_TEXT_NORMAL, vrpn_uint32 level=0)
Sends a NULL-terminated text message from the device d_sender_id.
static int encode_text_message_to_buffer(char *buf, vrpn_TEXT_SEVERITY severity, vrpn_uint32 level, const char *msg)
Encodes the body of the text message into a buffer, preparing for sending.
vrpn_int32 d_ping_message_id
Ask the server if they are there.
char * d_servicename
Name of this device, not including the connection part.
Class from which all user-level (and other) classes that communicate with vrpn_Connections should der...
virtual int register_senders(void)
Register the sender for this device (by default, the name of the device). Return 0 on success,...
virtual int register_types(void)=0
Register the types of messages this device sends/receives. Return 0 on success, -1 on fail.
virtual ~vrpn_BaseClass()
vrpn_BaseClass(const char *name, vrpn_Connection *c=NULL)
Names the device and assigns or opens connection, calls registration methods.
virtual int init(void)
Initialize things that the constructor can't. Returns 0 on success, -1 on failure.
Generic connection class not specific to the transport mechanism.
virtual const char * sender_name(vrpn_int32 sender)
Returns the name of the specified sender/type, or NULL if the parameter is invalid....
virtual int pack_message(vrpn_uint32 len, struct timeval time, vrpn_int32 type, vrpn_int32 sender, const char *buffer, vrpn_uint32 class_of_service)
Pack a message that will be sent the next time mainloop() is called. Turn off the RELIABLE flag if yo...
virtual int unregister_handler(vrpn_int32 type, vrpn_MESSAGEHANDLER handler, void *userdata, vrpn_int32 sender=vrpn_ANY_SENDER)
virtual int register_handler(vrpn_int32 type, vrpn_MESSAGEHANDLER handler, void *userdata, vrpn_int32 sender=vrpn_ANY_SENDER)
Set up (or remove) a handler for a message of a given type. Optionally, specify which sender to handl...
Structure to hold the objects that are being watched.
vrpn_TextPrinter_Watch_Entry * next
Pointer to the next one in the list.
vrpn_BaseClass * obj
Object being watched.
vrpn_TextPrinter * me
Pointer to this, because used in a static function.
Class that handles text/warning/error printing for all objects in the system.
FILE * d_ostream
Output stream to use.
static int VRPN_CALLBACK text_message_handler(void *userdata, vrpn_HANDLERPARAM p)
Handles the text messages that come from the connections for objects we are watching.
vrpn_TEXT_SEVERITY d_severity_to_print
Minimum severity to print.
vrpn_Semaphore d_semaphore
Mutex to ensure thread safety;.
void set_ostream_to_use(FILE *o)
Change the ostream that will be used to print messages. Setting a NULL ostream results in no printing...
~vrpn_TextPrinter()
Deletes any callbacks that are still registered.
void remove_object(vrpn_BaseClass *o)
Remove an object from the list of watched objects (multiple deletions of the object will not cause an...
vrpn_TextPrinter_Watch_Entry * d_first_watched_object
Head of list of objects being watched.
int add_object(vrpn_BaseClass *o)
Adds an object to the list of watched objects (multiple registration of the same object will result i...
void set_min_level_to_print(vrpn_TEXT_SEVERITY severity, vrpn_uint32 level=0)
Change the level of printing for the object (sets the minimum level to print). Default is Warnings an...
vrpn_uint32 d_level_to_print
Minimum level to print.
This structure is what is passed to a vrpn_Connection message callback.
vrpn_TextPrinter & vrpn_System_TextPrinter
All types of client/server/peer objects in VRPN should be derived from the vrpn_BaseClass type descri...
vrpn_TEXT_SEVERITY
Since the sending of text messages has been pulled into the base class (so that every object can send...
const unsigned vrpn_MAX_TEXT_LEN
const int vrpn_MAX_BCADRS
Internal value for number of BaseClass addresses.
VRPN_API vrpn_TextPrinter & vrpn_System_TextPrinter
char * vrpn_copy_service_name(const char *fullname)
const char * vrpn_dropped_connection
vrpn_Connection * vrpn_get_connection_by_name(const char *cname, const char *local_in_logfile_name, const char *local_out_logfile_name, const char *remote_in_logfile_name, const char *remote_out_logfile_name, const char *NIC_IPaddress, bool force_connection)
Create a client connection of arbitrary type (VRPN UDP/TCP, TCP, File, Loopback, MPI).
const vrpn_uint32 vrpn_CONNECTION_RELIABLE
Classes of service for messages, specify multiple by ORing them together Priority of satisfying these...
int(VRPN_CALLBACK * vrpn_MESSAGEHANDLER)(void *userdata, vrpn_HANDLERPARAM p)
Type of a message handler for vrpn_Connection messages.
VRPN_API int vrpn_unbuffer(const char **buffer, timeval *t)
Utility routine for taking a struct timeval from a buffer that was sent as a message.
VRPN_API int vrpn_buffer(char **insertPt, vrpn_int32 *buflen, const timeval t)
Utility routine for placing a timeval struct into a buffer that is to be sent as a message.
timeval vrpn_TimevalDiff(const timeval &tv1, const timeval &tv2)
timeval vrpn_TimevalNormalize(const timeval &in_tv)
#define vrpn_gettimeofday