/*
 *  include/grid.h
 *
 *  Grid APIs
 *
 *  Copyright (C) 2004  Phoenix Grid Computing Project
 *                      ( http://www.logos.ic.i.u-tokyo.ac.jp/phoenix/ )
 *
 *  2004-12-07  grid_select () and Grid_select () added 
 *              by Kaneda Kenji
 *  2004-11-26  Implemented basic functions 
 *              by Kenji Kaneda
 */


#ifndef _GRID_H
#define _GRID_H


#ifdef __cplusplus
#  define BEGIN_C_DECLS extern "C" {
#  define END_C_DECLS   }
#else /* !__cplusplus */
#  define BEGIN_C_DECLS 
#  define END_C_DECLS   
#endif /* __cplusplus */


#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "opt.h"


enum grid_error_kind {
	GRID_ERROR_KIND_SYS,
	GRID_ERROR_KIND_SSL
};
typedef enum grid_error_kind	grid_error_kind_t;


BEGIN_C_DECLS


void    grid_initialize ( const struct opt_t *opt );
void    grid_finalize ( void );
int     grid_connect( int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen );
int	grid_bind ( int sockfd, struct sockaddr *my_addr, socklen_t addrlen );
int	grid_listen ( int s, int backlog );
int     grid_accept ( int s, struct sockaddr *addr, socklen_t *addrlen );
ssize_t grid_send ( int s, const void *buf, size_t len, int flags );
ssize_t grid_recv ( int s, void *buf, size_t len, int flags );
int     grid_shutdown ( int s, int how );
int     grid_select ( int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
		      struct timeval *timeout );


/* Grid_xxx () functions are same as grid_xxx () functions except that
 * the Grid_xxx () functions abort when they fail. */
int     Grid_connect( int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen );
void    Grid_bind ( int sockfd, struct sockaddr *my_addr, socklen_t addrlen );
void    Grid_listen ( int s, int backlog );
int     Grid_accept( int s, struct sockaddr *addr , socklen_t *addrlen );
size_t  Grid_send( int s, const void *buf, size_t len, int flags );
size_t  Grid_recv( int s, void *buf, size_t len, int flags );
int     Grid_select ( int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
		      struct timeval *timeout );


/* conveninent functions for network programming */
int  Socket ( int domain, int type, int protocol );
void Setsockopt_reuseaddr ( int s );
in_addr_t in_addr_of_hostname ( const char *name );
struct sockaddr_in create_inet_sockaddr ( in_addr_t in_addr, int port );


END_C_DECLS


#endif /* _GRID_H */
