This code illustrates the basic framework of an Open Server application program:
/*
** This program demonstrates the minimum steps necessary
** to initialize and start up an Open Server application.
** No user-defined event handlers are installed, therefore
** the default handlers will execute instead.
*/
/*
** Include the required Open Server header files.
**
** ospublic.h: Public Open Server structures, typedefs,
** defines, and function prototypes.
**
** oserror.h: Open Server error number #defines. This header
** file is only required if the Open Server application wants
** to detect specific errors inside the Open Server error
** handler.
*/
#include <ospublic.h>
#include <oserror.h>
/*
** Include the operating system specific header files required
** by this Open Server application.
*/
#include <stdio.h>
/*
** Local defines.
**
** OS_ARGCOUNT Expected number of command line arguments
*/
#define OS_ARGCOUNT 2
/*
** This Open Server application expects the following
** command line arguments:
**
** servername: The name of the Open Server application.
**
** This name must exist in the interfaces file defined by
** the SYBASE environment variable.
**
** Returns:
** 0 Open Server exited successfully.
** 1 An error was detected during initialization.
*/
int main(argc, argv)
int argc;
char *argv[];
{
CS_CONTEXT *cp; /* Context structure */
CS_CHAR *servername; /* Open Server name */
CS_CHAR logfile[512]; /* Log file name */
CS_BOOL ok; /* Error control flag */
SRV_SERVER *ssp; /* Server control structure*/
/* Initialization. */
ok = CS_TRUE;
/*
** Read the command line options. There must be one
** argument specifying the server name.
*/
if(argc != OS_ARGCOUNT)
{
(CS_VOID)fprintf(stderr, "Invalid number of
arguments(%d)\n",argc);
(CS_VOID)fprintf(stderr, "Usage: <program>
<server name>\n");
exit(1);
}
/*
** Initialize ‘servername' to the command line argument
** provided.
*/
servername = (CS_CHAR *)argv[1];
/*
** Allocate a CS-Library context structure to define the
** default localization information. Open Server
** also stores global state information in this structure
** during initialization.
*/
if(cs_ctx_alloc(CS_VERSION_155, &cp) != CS_SUCCEED)
{
(CS_VOID)fprintf(stderr, "%s: cs_ctx_alloc failed",
servername);
exit(1);
}
/*
** Default Open Server localization information can be
** changed here before calling srv_version, using cs_config
** and cs_locale.
*/
/*
** Set the Open Server version and context information
*/
if(srv_version(cp, CS_VERSION_155) != CS_SUCCEED)
{
/*
** Release the context structure already allocated.
*/
(CS_VOID)cs_ctx_drop(cp);
(CS_VOID)fprintf(stderr, "%s: srv_version failed",
servername);
exit(1);
}
/*
** There is no error handler installed in this sample
** Open Server application. Any errors detected by Open
** Server are written to the Open Server log file
** configured below. A real Open Server application would
** install an error handler after calling srv_version, using
** srv_props(SRV_S_ERRHANDLE). Then, any subsequent errors
** will be detected by the Open Server application code.
*/
/*
** Default Open Server global properties can be changed here
** before calling srv_init. We choose just to change the
** default log file name to use the name of this Open
** Server application.
*/
/*
** Build a new Open Server log file name using ‘servername'
*/
(CS_VOID)sprintf(logfile, "%s.log", servername);
/*
** Set the new log file name using the global SRV_S_LOGFILE
** property.
*/
if(srv_props(cp, CS_SET, SRV_S_LOGFILE, logfile,
CS_NULLTERM,(CS_INT *)NULL) != CS_SUCCEED)
{
/*
** Release the context structure already allocated.
*/
(CS_VOID)cs_ctx_drop(cp);
(CS_VOID)fprintf(stderr,"%s: srv_props(SRV_S_LOGFILE)
failed\n",servername);
exit(1);
}
/*
** Initialize Open Server. This causes Open Server to
** allocate internal control structures based on the global
** properties set above. Open Server also looks up
** the application name in the interfaces file.
*/
if((ssp = srv_init((SRV_CONFIG *)NULL, servername,
CS_NULLTERM))== (SRV_SERVER *)NULL)
{
/*
** Release the context structure already allocated
*/
(CS_VOID)cs_ctx_drop(cp);
(CS_VOID)fprintf(stderr, "%s: srv_init failed\n",
servername);
exit(1);
}
/*
** Start the Open Server application running. We don't
** install any event handlers in this simple example. This
** causes Open Server to use the default event handlers.
**
** The call to srv_run does not return until a fatal error is
** detected by this Open Server application, or a SRV_STOP
** event is queued. Since we haven't installed any event
** handlers, the only way to stop this Open Server
** application is to kill the operating system process in
** which it is running.
*/
if(srv_run((SRV_SERVER *)NULL) == CS_FAIL)
{
(CS_VOID)fprintf(stderr, "%s: srv_run failed\n",
servername);
ok = CS_FALSE;
}
/*
** Release all allocated control structures and exit.
*/
(CS_VOID)srv_free(ssp);
(CS_VOID)cs_ctx_drop(cp);
exit(ok ? 0 : 1);
}