CWB
Data Structures | Macros | Typedefs | Functions | Variables
server.c File Reference
#include "server.h"
#include "auth.h"
#include "cqi.h"
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "../cl/cl.h"
#include "../cl/macros.h"
#include "../cqp/options.h"
#include "../cqp/corpmanag.h"
#include "../cqp/parse_actions.h"
#include "../cqp/hash.h"

Data Structures

struct  att_bucket
 The AttBucket object is a holder for an attribute and its key string. More...
 
struct  att_hashtable
 Underlying structure for the AttHashTable object. More...
 

Macros

#define NETBUFSIZE   512
 
#define ATTHASHSIZE   16384
 
#define GENERAL_ERROR_SIZE   1024
 
#define MSG_WAITALL   0
 

Typedefs

typedef struct att_bucket AttBucket
 The AttBucket object is a holder for an attribute and its key string. More...
 
typedef struct att_hashtableAttHashTable
 An AttHashTable object contains space for a hash table of attribute-pointers. More...
 

Functions

void cqi_send_error (char *function)
 Prints an error message to cqpserver's STDERR and then exits. More...
 
void cqi_recv_error (char *function)
 Prints an error message to cqpserver's STDERR and then exits. More...
 
void cqi_internal_error (char *function, char *cause)
 Prints an error message to cqpserver's STDERR and then exits. More...
 
void cqi_general_error (char *errstring)
 General error reporting function. More...
 
int accept_connection (int port)
 Wait for, and then process, an attempt by a client to initiate a connection to cqpserver via TCP/IP. More...
 
int cqi_flush (void)
 Flushes the stream from the CQP server to the client program, emptying its buffer. More...
 
int cqi_send_byte (int n, int nosnoop)
 Sends a BYTE to the client. More...
 
int cqi_send_word (int n)
 Sends a WORD to the client. More...
 
int cqi_send_int (int n)
 Sends an INT to the client. More...
 
int cqi_send_string (char *str)
 Sends a STRING to the client. More...
 
int cqi_send_byte_list (cqi_byte *list, int l)
 Sends a BYTE[] (byte list) to the client. More...
 
int cqi_send_int_list (int *list, int l)
 Sends an INT[] (integer list) to the client. More...
 
int cqi_send_string_list (char **list, int l)
 Sends a STRING[] (string list) to the client. More...
 
void cqi_command (int command)
 Sends a general CQi command, without any arguments. More...
 
void cqi_data_byte (int n)
 Sends a byte of data to the client. More...
 
void cqi_data_bool (int n)
 Sends a boolean to the client. More...
 
void cqi_data_int (int n)
 Sends an integer to the client. More...
 
void cqi_data_string (char *str)
 Sends a string to the client. More...
 
void cqi_data_byte_list (cqi_byte *list, int l)
 Sends a byte list to the client. More...
 
void cqi_data_bool_list (cqi_byte *list, int l)
 Sends a list of booleans to the client. More...
 
void cqi_data_int_list (int *list, int l)
 Sends a list of integers to the client. More...
 
void cqi_data_string_list (char **list, int l)
 Sends a list of strings to the client. More...
 
void cqi_data_int_int (int n1, int n2)
 Sends a sequence of two integers to the client. More...
 
void cqi_data_int_int_int_int (int n1, int n2, int n3, int n4)
 Sends a sequence of four integers to the client. More...
 
int cqi_recv_bytes (cqi_byte *buf, int bytes)
 
int cqi_recv_byte (void)
 
int cqi_read_byte (void)
 
int cqi_read_bool (void)
 
int cqi_read_word (void)
 
int cqi_read_int (void)
 
char * cqi_read_string (void)
 
int cqi_read_command (void)
 
int cqi_read_byte_list (cqi_byte **list)
 
int cqi_read_bool_list (cqi_byte **list)
 
int cqi_read_int_list (int **list)
 
int cqi_read_string_list (char ***list)
 
int check_corpus_name (char *name)
 
int check_attribute_name (char *name)
 
int check_subcorpus_name (char *name)
 
char * strdupto (char *str, char *end)
 
int split_attribute_spec (char *spec, char **corpus, char **attribute)
 
int split_subcorpus_spec (char *spec, char **corpus, char **subcorpus)
 
char * combine_subcorpus_spec (char *corpus, char *subcorpus)
 
void make_attribute_hash (int size)
 This function has to be called once to initialise the global attribute hash. More...
 
void free_attribute_hash (void)
 Frees the global AttHash object and the space that it points to. More...
 
AttBucketatt_hash_lookup (char *str)
 Finds an AttBucket within the global AttHash that matches the argument string. More...
 
Attributecqi_lookup_attribute (char *name, int type)
 
int cqi_drop_attribute (char *name)
 Drops the named attribute from memory. More...
 
CorpusListcqi_find_corpus (char *name)
 Gets a pointer to the corpus with the given name. More...
 
int cqi_activate_corpus (char *name)
 Activates the named corpus. More...
 

Variables

int sockfd
 Connection in: file-descriptor integer. More...
 
int connfd
 Connection out: file-descriptor integer. More...
 
FILE * conn_out
 Connection out: stream for buffered output (don't forget to flush()) More...
 
struct sockaddr_in my_addr client_addr
 
struct hostent * remote_host
 
char * remote_address
 
cqi_byte netbuf [NETBUFSIZE]
 
int bytes
 
int cqi_errno = CQI_STATUS_OK
 CQi last error. More...
 
char cqi_error_string [GENERAL_ERROR_SIZE] = "No error."
 String describing the last CQi error. More...
 
char cqi_id_uc_first [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_"
 
char cqi_id_uc [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_-0123456789"
 
char cqi_id_lc_first [] = "abcdefghijklmnopqrstuvwxyz_"
 
char cqi_id_lc [] = "abcdefghijklmnopqrstuvwxyz_-0123456789"
 
char cqi_id_all [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-0123456789"
 
AttHashTable AttHash = NULL
 The CQi server's global attribute hash. More...
 

Macro Definition Documentation

#define ATTHASHSIZE   16384

Referenced by accept_connection().

#define GENERAL_ERROR_SIZE   1024

Referenced by cqi_general_error().

#define MSG_WAITALL   0
#define NETBUFSIZE   512

Typedef Documentation

typedef struct att_bucket AttBucket

The AttBucket object is a holder for an attribute and its key string.

typedef struct att_hashtable* AttHashTable

An AttHashTable object contains space for a hash table of attribute-pointers.

Function Documentation

int accept_connection ( int  port)

Wait for, and then process, an attempt by a client to initiate a connection to cqpserver via TCP/IP.

Note that this function may or may not fork the cqpserver process.

If forking happens, then the child handles the connection, whereas the parent carries on waiting for further connections.

On Windows, forking never happens (since Windows doesn't support it).

On *nix, forking happens UNLESS the global private_server is true. (Actually, if private_server is true, then forking still happens, but the parent process immeidately exits.)

TODO: a better name would be server_accept_connection or somesuch...

Parameters
portThe integer identifier of the port to listen on.
Returns
A > 0 value (actually the socket ID of the incoming connection) if all is OK; otherwise -1.

References ATTHASHSIZE, check_host(), client_addr, conn_out, connfd, CQI_PORT, localhost, make_attribute_hash(), private_server, remote_address, remote_host, server_debug, server_log, server_quit, and sockfd.

Referenced by main().

AttBucket* att_hash_lookup ( char *  str)

Finds an AttBucket within the global AttHash that matches the argument string.

References ATT_NONE, att_bucket::attribute, cl_strdup(), cqi_internal_error(), hash_string(), att_hashtable::size, att_hashtable::space, att_bucket::string, and att_bucket::type.

Referenced by cqi_drop_attribute(), and cqi_lookup_attribute().

int check_attribute_name ( char *  name)
int check_corpus_name ( char *  name)
int check_subcorpus_name ( char *  name)
char* combine_subcorpus_spec ( char *  corpus,
char *  subcorpus 
)

References cl_malloc(), and cl_strdup().

Referenced by do_cqi_cqp_query().

int cqi_activate_corpus ( char *  name)

Activates the named corpus.

References cqi_find_corpus(), server_debug, and set_current_corpus().

Referenced by do_cqi_cqp_query().

void cqi_command ( int  command)
void cqi_data_bool ( int  n)
void cqi_data_bool_list ( cqi_byte list,
int  l 
)

Sends a list of booleans to the client.

Parameters
listpointer to a block of booleans (each occupying one byte) to send.
lthe number of bytes to send.

References CQI_DATA_BOOL_LIST, cqi_flush(), cqi_send_byte_list(), cqi_send_error(), and cqi_send_word().

void cqi_data_byte ( int  n)

Sends a byte of data to the client.

References CQI_DATA_BYTE, cqi_flush(), cqi_send_byte(), cqi_send_error(), and cqi_send_word().

void cqi_data_byte_list ( cqi_byte list,
int  l 
)

Sends a byte list to the client.

Parameters
listpointer to a block of bytes to send.
lthe number of bytes to send.

References CQI_DATA_BYTE_LIST, cqi_flush(), cqi_send_byte_list(), cqi_send_error(), and cqi_send_word().

void cqi_data_int ( int  n)
void cqi_data_int_int ( int  n1,
int  n2 
)

Sends a sequence of two integers to the client.

Parameters
n1The first integer sent
n2The second integer sent

References CQI_DATA_INT_INT, cqi_flush(), cqi_send_error(), cqi_send_int(), and cqi_send_word().

Referenced by do_cqi_cl_struc2cpos().

void cqi_data_int_int_int_int ( int  n1,
int  n2,
int  n3,
int  n4 
)

Sends a sequence of four integers to the client.

Parameters
n1The first integer sent
n2The second integer sent
n3The third integer sent
n4The fourth integer sent

References CQI_DATA_INT_INT_INT_INT, cqi_flush(), cqi_send_error(), cqi_send_int(), and cqi_send_word().

Referenced by do_cqi_cl_alg2cpos().

void cqi_data_int_list ( int *  list,
int  l 
)

Sends a list of integers to the client.

Parameters
listpointer to a block of integers to send.
lthe number of integers to send.

References CQI_DATA_INT_LIST, cqi_flush(), cqi_send_error(), cqi_send_int_list(), and cqi_send_word().

Referenced by do_cqi_cl_id2cpos(), do_cqi_cl_idlist2cpos(), and do_cqi_cl_regex2id().

void cqi_data_string ( char *  str)
void cqi_data_string_list ( char **  list,
int  l 
)

Sends a list of strings to the client.

Parameters
listpointer to a block of pointers-to-strings; the strings will be sent.
lthe number of strings to send.

References CQI_DATA_STRING_LIST, cqi_flush(), cqi_send_error(), cqi_send_string_list(), and cqi_send_word().

Referenced by do_cqi_corpus_properties().

int cqi_drop_attribute ( char *  name)

Drops the named attribute from memory.

The attribute name is looked up in the global hash, and then cl_delete_attribute() is called on the result.

See also
cl_delete_attribute
Parameters
nameName of the attribute to be deleted
Returns
Boolean: true for all OK, otherwise false

References att_hash_lookup(), ATT_NONE, att_bucket::attribute, cl_delete_attribute(), and att_bucket::type.

CorpusList* cqi_find_corpus ( char *  name)
int cqi_flush ( void  )
void cqi_general_error ( char *  errstring)

General error reporting function.

Note that unlike other CQi error functions, this function sends an error message to the outgoing connection, rather than printing to the server's STDERR. Also note that the program doesn't exit!

The error message is placed into the global variable cqi_error_string, whence it can be accessed by the client if the CQI_CTRL_LAST_GENERAL_ERROR is sent to the server.

TODO a better name would be cqi_error_general

Parameters
errstringString containing the error message.

References cqi_command(), CQI_ERROR_GENERAL_ERROR, cqi_error_string, cqi_internal_error(), and GENERAL_ERROR_SIZE.

Referenced by do_cqi_cl_drop_attribute(), and do_cqi_corpus_attributes().

void cqi_internal_error ( char *  function,
char *  cause 
)

Prints an error message to cqpserver's STDERR and then exits.

This function reports a (miscellanrous) internal error in a cqpserver function.

Parameters
functionString containing the name of the function where the error was raised.
causeString containing a description of what caused the error.

Referenced by att_hash_lookup(), and cqi_general_error().

Attribute* cqi_lookup_attribute ( char *  name,
int  type 
)
int cqi_read_bool ( void  )
int cqi_read_bool_list ( cqi_byte **  list)
int cqi_read_byte ( void  )
int cqi_read_byte_list ( cqi_byte **  list)
int cqi_read_command ( void  )

References CQI_PAD, cqi_read_byte(), and server_debug.

Referenced by interpreter(), and main().

int cqi_read_int ( void  )
int cqi_read_int_list ( int **  list)
char* cqi_read_string ( void  )
int cqi_read_string_list ( char ***  list)
int cqi_read_word ( void  )

References cqi_read_byte(), and snoop.

Referenced by cqi_read_string().

int cqi_recv_byte ( void  )

References connfd, MSG_WAITALL, and snoop.

Referenced by cqi_read_bool(), and cqi_read_byte().

int cqi_recv_bytes ( cqi_byte buf,
int  bytes 
)

References connfd, MSG_WAITALL, and snoop.

Referenced by cqi_read_string().

void cqi_recv_error ( char *  function)

Prints an error message to cqpserver's STDERR and then exits.

This function reports an error in READING data from the incoming connection.

Parameters
functionString containing the name of the function where the error was raised.

Referenced by cqi_read_bool(), cqi_read_byte(), and cqi_read_string().

int cqi_send_byte ( int  n,
int  nosnoop 
)

Sends a BYTE to the client.

This function should be called via one of the cqi_data_* functions and not on its own.

This is the fundamental "sending" function, and the only one that calls the underlying OS-specific file stream/socket functions.

Parameters
nThe byte to send. NOTE that as the parameter is an int, numbers bigger than 0xff can be passed. BUT all content except the lowest-order 8-bits are discarded (0xff is used as a mask with bitwise-and).
nosnoopBoolean: if true, snoop functionality is overridden (to allow for non-repetition of messages when called from a function that has already printed a message)

References conn_out, connfd, MSG_WAITALL, and snoop.

Referenced by cqi_data_bool(), cqi_data_byte(), cqi_send_byte_list(), cqi_send_int(), cqi_send_string(), and cqi_send_word().

int cqi_send_byte_list ( cqi_byte list,
int  l 
)

Sends a BYTE[] (byte list) to the client.

This function should be called via one of the cqi_data_* functions and not on its own.

Parameters
listpointer to a block of bytes to send.
lthe number of bytes to send.
Returns
Boolean: true if everything OK, otherwise false.

References cqi_send_byte(), and cqi_send_int().

Referenced by cqi_data_bool_list(), and cqi_data_byte_list().

void cqi_send_error ( char *  function)

Prints an error message to cqpserver's STDERR and then exits.

This function reports an error in SENDING data to the outgoing connection.

Parameters
functionString containing the name of the function where the error was raised.

Referenced by cqi_command(), cqi_data_bool(), cqi_data_bool_list(), cqi_data_byte(), cqi_data_byte_list(), cqi_data_int(), cqi_data_int_int(), cqi_data_int_int_int_int(), cqi_data_int_list(), cqi_data_string(), and cqi_data_string_list().

int cqi_send_int ( int  n)
int cqi_send_int_list ( int *  list,
int  l 
)

Sends an INT[] (integer list) to the client.

This function should be called via one of the cqi_data_* functions and not on its own.

Parameters
listpointer to a block of integers to send.
lthe number of integers to send.
Returns
Boolean: true if everything OK, otherwise false.

References cqi_send_int().

Referenced by cqi_data_int_list().

int cqi_send_string ( char *  str)

Sends a STRING to the client.

CQi strings are NOT null-terminated – so while the argument to this function needs to be a null-terminated string, the string that actually gets sent across the network will not be.

This function should be called via one of the cqi_data_* functions and not on its own.

Returns
Boolean: true if everything OK, otherwise false.

References cqi_send_byte(), cqi_send_word(), and snoop.

Referenced by cqi_data_string(), cqi_send_string_list(), do_cqi_cl_cpos2str(), do_cqi_cl_id2str(), do_cqi_cl_struc2str(), do_cqi_corpus_list_corpora(), do_cqi_cqp_list_subcorpora(), and send_cqi_corpus_attributes().

int cqi_send_string_list ( char **  list,
int  l 
)

Sends a STRING[] (string list) to the client.

This function should be called via one of the cqi_data_* functions and not on its own.

Parameters
listpointer to a block of pointers-to-strings; the strings will be sent.
lthe number of strings to send.

References cqi_send_int(), and cqi_send_string().

Referenced by cqi_data_string_list().

int cqi_send_word ( int  n)
void free_attribute_hash ( void  )

Frees the global AttHash object and the space that it points to.

TODO better name: att_hash_free

See also
AttHash

References att_hashtable::space.

void make_attribute_hash ( int  size)

This function has to be called once to initialise the global attribute hash.

TODO better name: att_hash_make

See also
AttHash

References bytes, cl_malloc(), att_hashtable::code, find_prime(), att_hashtable::size, and att_hashtable::space.

Referenced by accept_connection().

int split_attribute_spec ( char *  spec,
char **  corpus,
char **  attribute 
)
int split_subcorpus_spec ( char *  spec,
char **  corpus,
char **  subcorpus 
)
char* strdupto ( char *  str,
char *  end 
)

Variable Documentation

AttHashTable AttHash = NULL

The CQi server's global attribute hash.

int bytes
struct sockaddr_in my_addr client_addr

Referenced by accept_connection().

FILE* conn_out

Connection out: stream for buffered output (don't forget to flush())

Referenced by accept_connection(), cqi_flush(), and cqi_send_byte().

int connfd

Connection out: file-descriptor integer.

Referenced by accept_connection(), cqi_recv_byte(), cqi_recv_bytes(), and cqi_send_byte().

int cqi_errno = CQI_STATUS_OK
char cqi_error_string[GENERAL_ERROR_SIZE] = "No error."

String describing the last CQi error.

This can be queried by the client.

Referenced by cqi_general_error(), and interpreter().

char cqi_id_all[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-0123456789"
char cqi_id_lc[] = "abcdefghijklmnopqrstuvwxyz_-0123456789"
char cqi_id_lc_first[] = "abcdefghijklmnopqrstuvwxyz_"
char cqi_id_uc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_-0123456789"
char cqi_id_uc_first[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_"
char* remote_address

Referenced by accept_connection().

struct hostent* remote_host

Referenced by accept_connection().

int sockfd

Connection in: file-descriptor integer.

Referenced by accept_connection().