Main Page   Data Structures   File List   Data Fields   Globals  

nsds_util.h File Reference

Header file for the NSDS driver util routines. More...

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <time.h>

Go to the source code of this file.

Defines

#define NSDS_UTIL_H_
 read-once define

#define MAX_CMD_LEN   8192
 Max length of a command on the control channel, see tcp_nl_read.

#define TSTAMP_LEN   32
 Length, in chars, of ISO 8601 timestamp.

#define DATUM_LEN   32
 Length, in chars, of a single data point + channel ID.

#define NUM_CHANNELS   128
 Max number of data channels, only really affects fake_daq.


Enumerations

enum  socket_wait_ret
 Possible returns on wait-on-socket, simple tristate. More...

enum  bool
 Local define of boolean, if not extant. More...


Functions

int pthread_setconcurrency (int new_level)
 Hack - prototype pthread_setconcurrency since gcc doesnt have same.

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.

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


Detailed Description

Header file for the NSDS driver util routines.

Date:
9/5/02 Types, prototypes and defines for common code used by both driver and fake_daq.

Definition in file nsds_util.h.


Define Documentation

#define DATUM_LEN   32
 

Length, in chars, of a single data point + channel ID.

Definition at line 57 of file nsds_util.h.

Referenced by daq_thread_main().

#define MAX_CMD_LEN   8192
 

Max length of a command on the control channel, see tcp_nl_read.

Definition at line 51 of file nsds_util.h.

Referenced by cmd_handler(), daq_do_work(), data_thread_main(), and tcp_nl_read().

#define NSDS_UTIL_H_
 

read-once define

Definition at line 14 of file nsds_util.h.

#define NUM_CHANNELS   128
 

Max number of data channels, only really affects fake_daq.

Definition at line 60 of file nsds_util.h.

Referenced by daq_do_work(), daq_thread_main(), data_channel_enabled(), data_channel_flag(), and main().

#define TSTAMP_LEN   32
 

Length, in chars, of ISO 8601 timestamp.

Definition at line 54 of file nsds_util.h.

Referenced by daq_thread_main().


Enumeration Type Documentation

enum bool
 

Local define of boolean, if not extant.

Definition at line 44 of file nsds_util.h.

enum socket_wait_ret
 

Possible returns on wait-on-socket, simple tristate.

Definition at line 34 of file nsds_util.h.

Referenced by tcp_nl_read(), and tcp_socket_wait().


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().

int pthread_setconcurrency int    new_level
 

Hack - prototype pthread_setconcurrency since gcc doesnt have same.

Referenced by daq_do_work(), and data_thread_startup().

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().


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