Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

tuple_server.c File Reference

The tuple server accepts incoming TCP connections and offers clients access to the database. More...

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <semaphore.h>
#include "tuple.h"

Go to the source code of this file.

Data Structures

struct  ttuple
 Doubly-linked list of tuples, for efficient insertions and deletions. More...


Defines

#define LOGPRINTF(fmt, a...)   if (logptr < 8000) { logptr += sprintf(logbuf+logptr, fmt, ##a); }
#define LOGTUPLE(s)   print_tuple(s);
#define MAXNUMTHREADS   100

Functions

void add_tuple_to_space (struct tuple *t)
void remove_message_from_space (struct ttuple *s)
void kill_this_thread (struct context *arg)
void handle_get_read (struct context *ctx, struct tuple *s, int remove, int blocking)
void handle_dump_space (struct context *ctx)
void * client_thread_func (void *varg)
int main (int argc, char *argv[])

Variables

sem_t log_sem
ttuplefirst_message = NULL
ttuplelast_message = NULL
context client_list [MAXNUMTHREADS]
sem_t tuple_space_access
unsigned char num_blocked = 0
sem_t blocked_sem
sem_t new_client_sem


Detailed Description

The tuple server accepts incoming TCP connections and offers clients access to the database.

The tuple server is responsible for remaining stable while clients misbehave, and making sure that client operations are atomic. Client misbehavior can include things like badly-formed requests and dropped network sockets.

Definition in file tuple_server.c.


Define Documentation

#define LOGPRINTF fmt,
a...       if (logptr < 8000) { logptr += sprintf(logbuf+logptr, fmt, ##a); }
 

Definition at line 64 of file tuple_server.c.

Referenced by client_thread_func, handle_get_read, and kill_this_thread.

#define LOGTUPLE      print_tuple(s);
 

Definition at line 67 of file tuple_server.c.

Referenced by client_thread_func.

#define MAXNUMTHREADS   100
 

Definition at line 94 of file tuple_server.c.


Function Documentation

void add_tuple_to_space struct tuple   t [static]
 

When we add a tuple to the space, we unblock all the clients who are waiting for tuples, so they can check and see if this is the one they want.

Definition at line 134 of file tuple_server.c.

References blocked_sem, ttuple::next, num_blocked, ttuple::previous, ttuple::tuple, and tuple_space_access.

Referenced by client_thread_func, and handle_get_read.

void* client_thread_func void *    varg [static]
 

When a client makes a connection, find out what it wants, and do that operation.

Definition at line 346 of file tuple_server.c.

References add_tuple_to_space, DUMP, GET, GET_NB, handle_dump_space, handle_get_read, kill_this_thread, LOG, log_sem, LOGPRINTF, LOGTUPLE, PUT, READ, READ_NB, recv_chunk, recv_tuple, context::sock, and context::thr.

Referenced by main.

void handle_dump_space struct context   ctx [static]
 

When a client requests a dump of the tuple space, handle that here.

Definition at line 272 of file tuple_server.c.

References tuple_list::next, ttuple::next, recv_chunk, recv_tuple, send_chunk, send_tuple, tuple_list::tup, ttuple::tuple, tuple_space_access, and tuples_match.

Referenced by client_thread_func.

void handle_get_read struct context   ctx,
struct tuple   s,
int    remove,
int    blocking
[static]
 

Handle GETs and READs, both blocking and non-blocking.

Definition at line 210 of file tuple_server.c.

References add_tuple_to_space, blocked_sem, DBGPRINTF, destroy_tuple, log_sem, LOGPRINTF, ttuple::next, num_blocked, remove_message_from_space, send_chunk, send_tuple, context::sock, ttuple::tuple, tuple_space_access, and tuples_match.

Referenced by client_thread_func.

void kill_this_thread struct context   arg [static]
 

Definition at line 196 of file tuple_server.c.

References LOGPRINTF, context::sock, and context::thr.

Referenced by client_thread_func.

int main int    argc,
char *    argv[]
 

Set up a socket to listen for incoming requests. When one arrives, start a pthread to handle it, and run client_thread_func().

Definition at line 451 of file tuple_server.c.

References blocked_sem, client_thread_func, EXIT, log_sem, new_client_sem, context::sock, context::thr, and tuple_space_access.

void remove_message_from_space struct ttuple   s [static]
 

Tuples are kept in a doubly-linked list, so there's a lot of pointer-hacking here.

Definition at line 167 of file tuple_server.c.

References ttuple::next, and ttuple::previous.

Referenced by handle_get_read.


Variable Documentation

sem_t blocked_sem
 

When threads/clients block, they wait for this semaphore.

Definition at line 119 of file tuple_server.c.

Referenced by add_tuple_to_space, handle_get_read, and main.

struct context client_list[MAXNUMTHREADS]
 

Maintain a list of the clients currently trying to access tuple space. Keep track of a pthread and a socket for each one.

Definition at line 100 of file tuple_server.c.

struct ttuple* first_message = NULL
 

Pointer to the first tuple in tuple space. When there is a GET or READ operation, the search begins with the first tuple.

Definition at line 84 of file tuple_server.c.

struct ttuple* last_message = NULL
 

Pointer to the last tuple in tuple space. When there is a PUT operation, the tuple is put at the end of the list.

Definition at line 90 of file tuple_server.c.

sem_t log_sem
 

Definition at line 62 of file tuple_server.c.

Referenced by client_thread_func, handle_get_read, and main.

sem_t new_client_sem
 

When a client makes a connection, this semaphore protects client_list.

Definition at line 125 of file tuple_server.c.

Referenced by main.

unsigned char num_blocked = 0
 

Keep track of how many threads/clients are currently blocked, so that we can increment blocked_sem by the proper amount.

Definition at line 114 of file tuple_server.c.

Referenced by add_tuple_to_space, and handle_get_read.

sem_t tuple_space_access
 

Semaphore to make sure that all tuple operations are atomic. Each client finishes his operation before the next guy is allowed to begin.

Definition at line 108 of file tuple_server.c.

Referenced by add_tuple_to_space, handle_dump_space, handle_get_read, and main.


Generated on Sun Mar 30 23:46:51 2003 by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002