Main Page   Data Structures   File List   Data Fields   Globals  

nsds_util.c File Reference

Utility / common routines for the NSDS driver and DAQ. More...

#include "nsds_util.h"
#include "flog.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>

Go to the source code of this file.

Functions

char * gen_timestamp (void)
 Generate an ISO 8601-compliant timestamp.

int tcp_socket_make (const uint16_t SRV_PORT, const int QUEUE_LENGTH)
 Create server socket, ready for accept().

int tcp_nl_write (const int socket, const char *buf)
 Send a string, appending a newline character.

int tcp_nl_read (const uint16_t socket, char *buf, const time_t timeout)
 Read until EOL or MAX_CMD_LEN bytes.

int tcp_connect (const char *server, const uint16_t port)
 Routine, after Comer, for opening a TCP connection.

socket_wait_ret tcp_socket_wait (const int socket, const time_t timeout)
 Function to wait on a socket, with or without timeout.

char * tcp_peername (const int socket)
 Function to return a string w/name of connected peer.

int tcp_connect_retry (const char *host, const uint16_t port, const time_t retry_delay, const time_t end_time, const char *description)
 Loop until can open client connection.

void tcp_close (const int socket_fd)
 Shutdown and close TCP socket.


Variables

bool control_break
 Global boolean set by signal handler to force exit.


Detailed Description

Utility / common routines for the NSDS driver and DAQ.

Author:
Paul Hubbard
Date:
9/5/02
Note:
Requires the flog message library as of 9/24/02

Definition in file nsds_util.c.


Function Documentation

char* gen_timestamp void   
 

Generate an ISO 8601-compliant timestamp.

Note:
Not threadsafe!
Requires gettimeofday call
Return values:
Ptr  to static buffer with timestamp string
Date:
9/19/02
This is, frankly, a hack. It generates a UTC-format ISO8601 timestamp, with 5-digit fractional seconds. Its for the fake_daq program, so that it can spew psuedo data. The format, however, should be correct.

Definition at line 46 of file nsds_util.c.

Referenced by daq_thread_main().

void tcp_close const int    socket_fd
 

Shutdown and close TCP socket.

Does shutdown (read/write) and then close if fd > 0

Parameters:
socket_fd  File descriptor of socket

Definition at line 620 of file nsds_util.c.

Referenced by daq_main_loop(), and main_loop().

int tcp_connect const char *    server,
const uint16_t    port
 

Routine, after Comer, for opening a TCP connection.

---------------------------------------------------------------------

Note:
hardwired to use TCP, stream mode
Assumes Server ready and able to answer connections TCP/IP available Not multithreaded - gethostbyname is NOT reentrant!
Parameters:
server  Ptr to string of server name
port  Port number on server
Return values:
0  if no errors, !=0 on any error.
Date:
4/00, modified 8/28/02

Definition at line 359 of file nsds_util.c.

Referenced by tcp_connect_retry().

int tcp_connect_retry const char *    host,
const uint16_t    port,
const time_t    retry_delay,
const time_t    end_time,
const char *    description
 

Loop until can open client connection.

Parameters:
host  Hostname
port  Port number
retry_delay  Time, in seconds, to sleep between attempts
end_time  If >0, when we give up
description  String to print to screen
Return values:
If  > 0, socket FD
<=  0, error or timeout
Retry, every N seconds, to open a connection. Used to provide robustness against network failures; non-blocking.

Definition at line 562 of file nsds_util.c.

References tcp_connect().

Referenced by main_loop().

int tcp_nl_read const uint16_t    socket,
char *    buf,
const time_t    timeout
 

Read until EOL or MAX_CMD_LEN bytes.

--------------------------------------------------------------------- Read from TCP into static buffer until

  • get newline
  • or exceed buffer length.
All messages are newline-delimted, so this is used all over the place to read commands and data.

Yeah, yeah, I know already. Needs dynamic buffers, or sizing, or code that Sucks Less.

Note:
Removes newline and terminates string with 0x00
Date:
8/28/02
Parameters:
socket  tcp socket
buf  Buffer, of >= MAX_CMD_LEN bytes
timeout  If > 0, uses this as timeout. 0 waits forever.
Return values:
Number  of bytes read, < 0 on an error, 0 if timed out

Definition at line 246 of file nsds_util.c.

References MAX_CMD_LEN, socket_wait_ret, and tcp_socket_wait().

Referenced by cmd_handler(), and daq_do_work().

int tcp_nl_write const int    socket,
const char *    buf
 

Send a string, appending a newline character.

---------------------------------------------------------------------

Date:
8/29/02
Return values:
Number  of bytes written, not counting newline!
Parameters:
socket  Socket/fd
buf  C string to send
Note:
Forces a sync to flush at the end of the send
Very simple, writes a buffer and appends a newline.

Definition at line 194 of file nsds_util.c.

Referenced by cmd_handler(), daq_do_work(), daq_thread_main(), and tcp_send_driverids().

char* tcp_peername const int    socket
 

Function to return a string w/name of connected peer.

Note:
Non-reentrant, uses static buffer
Does a DNS lookup on the peer, returns their name or (if DNS fails) their IP address as a dotted-decimal string. Quite handy.

Definition at line 510 of file nsds_util.c.

Referenced by daq_main_loop(), and main_loop().

int tcp_socket_make const uint16_t    SRV_PORT,
const int    QUEUE_LENGTH
 

Create server socket, ready for accept().

---------------------------------------------------------------------

Note:
Borrowed from FNAL server code
Date:
8/27/02
Return values:
>  0 (Socket, OK)
<=0  (Error)
Parameters:
SRV_PORT  Port to create
QUEUE_LENGTH  TCP queue length, passed to listen()
Does the low-level work of setting up a listen socket, very careful about possible errors and socket options. Produced socket is ready for accept() calls.

Definition at line 100 of file nsds_util.c.

References QUEUE_LENGTH, and SRV_PORT.

Referenced by daq_main_loop().

socket_wait_ret tcp_socket_wait const int    socket,
const time_t    timeout
 

Function to wait on a socket, with or without timeout.

Blocking or non-blocking wait until a socket is ready for reading. Used in functions that cannot wait forever, as well as ones that can.

Note:
Also copied from FNAL code
Negative or zero timeout means blocking wait; might wanna change this to a bool param?
Return values:
socket_wait_ret  enumerated type
Parameters:
socket  file descriptor
timeout  Timeout, in seconds

Definition at line 437 of file nsds_util.c.

References socket_wait_ret.

Referenced by tcp_nl_read().


Variable Documentation

bool control_break
 

Global boolean set by signal handler to force exit.

Definition at line 30 of file nsds_util.c.

Referenced by daq_main_loop(), daq_thread_main(), main(), and sighandler().


Generated on Fri Dec 6 14:33:16 2002 for NSDS Driver by doxygen1.2.18