Modifying main

You must move Open Server application code currently running in the main routine to one or more event handlers. Other routines, such as the srv_run routine are also removed. You must also remove routines that EAServer automatically initiates, and remove properties and handlers that are configured through EAServer Manager.

Traditional Open Server application

The following file is a traditional Open Server application that contains a main routine:

#include <ospublic.h>
#include <server.h>

/*
** File server.c containing a typical Open Server main() function. For
** simplicity, there is no error handling here.
**
** This builds into an executable, with the supporting code, such as
** event handlers, ending up as either static libraries that become part 
** of the executable, or as dynamic libraries loaded at run time.
*/
main(int argc, char *argv[])
{
		/*
		** Variables.
		*/
		CS_INT conns;
		CS_INT threads;
		CS_CHAR *name;
		CS_CONTEXT *context;
	
		/*
		** Process command line. Function get_params() is in file appl.c.
		*/
		get_params(argc, argv, &conns, &threads, &name);
		if (name == (CS_CHAR *) NULL)
		{
		    printf("Usage: %s [-os_conns=<conns>] [-os_threads=<threads>] "
		           "-os_name=<name>\n", argv[0]);
		    exit(1);
		}
		/*
		** Initialize server.
		*/
		cs_ctx_alloc(CS_VERSION_100, &context);
		srv_version(context, CS_VERSION_100);
		if (conns > 0)
		    srv_props(context, CS_SET, SRV_S_NUMCONNECTIONS,
		              (CS_VOID *) &conns, sizeof(conns), (CS_INT *) NULL);
		if (threads > 0)
		    srv_props(context, CS_SET, SRV_S_NUMTHREADS,
		              (CS_VOID *) &threads, sizeof(threads), (CS_INT *) NULL)
		srv_init((SRV_CONFIG *) NULL, name, CS_NULLTERM);
	
		/*
		** Register handlers. Files handler1.c and handler2.c contain these
		** functions.
		*/
		srv_handle((SRV_SERVER *) NULL, SRV_START, start_handler);
		srv_handle((SRV_SERVER *) NULL, SRV_ATTENTION, 	attn_handler);
		srv_handle((SRV_SERVER *) NULL, SRV_BULK, bulk_handler);
		srv_handle((SRV_SERVER *) NULL, SRV_CONNECT, conn_handler);
		srv_handle((SRV_SERVER *) NULL, SRV_CURSOR, cur_handler);
		srv_handle((SRV_SERVER *) NULL, SRV_DISCONNECT, disc_handler);
		srv_handle((SRV_SERVER *) NULL, SRV_DYNAMIC, dyn_handler);
		srv_handle((SRV_SERVER *) NULL, SRV_LANGUAGE, lang_handler);
		srv_handle((SRV_SERVER *) NULL, SRV_RPC, rpc_handler);
		srv_handle((SRV_SERVER *) NULL, SRV_OPTION, opt_handler);
	
		/*
		** Start server.
		*/
		srv_run((SRV_SERVER *) NULL);
		exit(0);
}

Moving main() to an event handler

The main code has been placed in an event handler, other routines and properties have been removed:

#include <ospublic.h>
#include <server.h>

/*
** File server.c. The original main() function becomes init_handler().
**
** Build this into a dynamic library, and register the
** init_handler() function in EAServer Manager. EAServer
** will call the function at runtime.
**
** You may build the supporting code into the same dynamic library, or into
** one or more different dynamic libraries. In either case, you'll need to
** register each handler separately with EAServer, using
** EAServer Manager.
*/CS_RETCODE CS_PUBLIC init_handler(CS_CONTEXT *context, 
		int argc, char *argv[])
{
		/*
		** Variables.
		**
		** EAServer initializes context and passes it to this function.
		*/
		CS_INT conns;
		CS_INT threads;
		CS_CHAR *name;
	
		/*
		** Process command line. Function get_params() is in file appl.c.
		**
		** Do not exit on error.
		*/
		get_params(argc, argv, &conns, &threads, &name);
	
		/*
		** Initialize server.
		**
		** Get rid of cs_ctx_alloc(), srv_version(), etc. Do not call srv_init().
		** Certain properties previously set using srv_props() are now set
		** in EAServer Manager. 
		*/
		if (conns > 0)
		    srv_props(context, CS_SET, SRV_S_NUMCONNECTIONS,
		              (CS_VOID *) &conns, sizeof(conns), (CS_INT *) NULL);
		if (threads > 0)
		    srv_props(context, CS_SET, SRV_S_NUMTHREADS,
		              (CS_VOID *) &threads, sizeof(threads), (CS_INT *) NULL);
	
		/*
		** Register handlers. Files handler1.c and handler2.c contain handler
		** functions.
		**
		** Register all the handlers using EAServer Manager.
		*/
	
		/*
		** Start server.
		**
		** EAServer calls srv_run(); Do not call it yourself.
		*/
	
		/*
		** Return rather than exit.
		*/
		return CS_SUCCEED;
}