startstop.cpp

This is an example of using <sm_vas.h>. It shows a minimal storage manager server, which does nothing but start up (recover) and shut down.

00001 /*<std-header orig-src='shore'>
00002 
00003  $Id: startstop.cpp,v 1.2 2010/09/21 14:26:28 nhall Exp $
00004 
00005 SHORE -- Scalable Heterogeneous Object REpository
00006 
00007 Copyright (c) 1994-99 Computer Sciences Department, University of
00008                       Wisconsin -- Madison
00009 All Rights Reserved.
00010 
00011 Permission to use, copy, modify and distribute this software and its
00012 documentation is hereby granted, provided that both the copyright
00013 notice and this permission notice appear in all copies of the
00014 software, derivative works or modified versions, and any portions
00015 thereof, and that both notices appear in supporting documentation.
00016 
00017 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
00018 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
00019 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
00020 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
00021 
00022 This software was developed with support by the Advanced Research
00023 Project Agency, ARPA order number 018 (formerly 8230), monitored by
00024 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
00025 Further funding for this work was provided by DARPA through
00026 Rome Research Laboratory Contract No. F30602-97-2-0247.
00027 
00028 */
00029 
00030 /*  -- do not edit anything above this line --   </std-header>*/
00031 
00032 // This include brings in all header files needed for writing a VAs 
00033 //
00034 #include "sm_vas.h"
00035 
00036 /* This is about the most minimal storage manager program there is.
00037  * This starts up and shuts down a storage manager, but doesn't
00038  * do any work.
00039  * The purpose of this example is to illustrate the 
00040  * program infrastructure needed to use the storage manager.
00041  *
00042  * (Other examples build on this.)
00043  */
00044 
00045 
00046 /* smthread-based class for all sm-related work 
00047  *
00048  * This class processes run-time options given in argc and argv.
00049  * It also starts up and shuts down a storage manager.
00050  */
00051 class smthread_user_t : public smthread_t {
00052     int        _argc;
00053     char **    _argv;
00054     int        _retval;
00055 public:
00056 
00057     smthread_user_t(int ac, char **av) 
00058             : smthread_t(t_regular, "smthread_user_t"),
00059             _argc(ac), _argv(av), _retval(0) { }
00060     ~smthread_user_t()  {}
00061     // run() is virtual in smthread_t.
00062     void run();  // below
00063     int  return_value() const { return _retval; }
00064 };
00065 
00066 void 
00067 smthread_user_t::run() 
00068 {
00069     /* the storage manager requires that certain options are set.
00070      * Get them from the file named EXAMPLE_SHORECONFIG
00071      */
00072     option_group_t options(1);// one levels.
00073     /*
00074      * <program name>: <value>
00075      */
00076     W_COERCE(options.add_class_level(_argv[0]));    // program name
00077 
00078     /* Add the storage manager options to this group. This  
00079      * is a static method in ss_m. It adds to the group all
00080      * the options the storage manager uses. All storage
00081      * manager options that have default values are given those defaults.
00082      * You can add your own run-time options to the group as well.
00083      * In this example, we do not have any to add.
00084      */
00085     W_COERCE(ss_m::setup_options(&options));
00086     
00087     /* Get option values from file EXAMPLE_SHORECONFIG */
00088     {
00089         /* Scan the file. 
00090          * We could scan the command line also or instead: you
00091          * can find an example of this is src/sm/tests/init_config_options.cpp
00092          */
00093         w_ostrstream      err_stream;
00094         const char* opt_file = "EXAMPLE_SHORECONFIG";     // option config file
00095         option_file_scan_t opt_scan(opt_file, &options);
00096 
00097         /* Scan the file and override any default option settings.
00098          * Option names must be spelled correctly and in full.
00099          */
00100         w_rc_t rc = opt_scan.scan(true /*override defaults*/, err_stream, 
00101                 true /* exact match: option names must match exactly */
00102                 );
00103         if (rc.is_error()) {
00104             cerr << "Error in reading option file: " << opt_file << endl;
00105             cerr << "\t" << err_stream.c_str() << endl;
00106             _retval = 1;
00107             return;
00108         }
00109     }
00110 
00111     /* Check that all required options have been set. 
00112      * If an option's attributes indicate that 
00113      * a value is required (not optional) and it hasn't
00114      * been given a value (either by default or through the
00115      * file processing above), this will detect the
00116      * missing value.
00117      */
00118     {
00119         w_ostrstream      err_stream;
00120         w_rc_t rc = options.check_required(&err_stream);
00121         if (rc.is_error()) {
00122             cerr << "These required options are not set:" << endl;
00123             cerr << err_stream.c_str() << endl;
00124             _retval = 1;
00125             return;
00126         }
00127     }
00128 
00129 
00130     /* Now we have done the minimal marshaling of resources to start up
00131      * and shut down a storage manager.
00132      */
00133 
00134     cout << "Starting SSM and performing recovery." << endl;
00135     ss_m* ssm = new ss_m();
00136     if (!ssm) {
00137         cerr << "Error: Out of memory for ss_m" << endl;
00138         _retval = 1;
00139         return;
00140     }
00141 
00142     /* 
00143      * Any other work with the storage manager should be
00144      * done here or in child threads forked by this one,
00145      * (passing this ss_m as an argument to those threads)
00146      */
00147     cout << "Shutting down SSM." << endl;
00148     delete ssm;
00149     ssm = NULL;
00150 }
00151 
00152 int
00153 main(int argc, char* argv[])
00154 {
00155 
00156     /* Create an smthread that reads options and starts/stops
00157      * a storage manager
00158      */
00159     smthread_user_t *smtu = new smthread_user_t(argc, argv);
00160     if (!smtu)
00161             W_FATAL(fcOUTOFMEMORY);
00162 
00163     /* cause the thread's run() method to be invoked */
00164     w_rc_t e = smtu->fork();
00165     if(e.is_error()) {
00166         cerr << "error forking thread: " << e <<endl;
00167         return 1;
00168     }
00169 
00170     /* wait for the thread's run() method to end */
00171     e = smtu->join();
00172     if(e.is_error()) {
00173         cerr << "error forking thread: " << e <<endl;
00174         return 1;
00175     }
00176 
00177     /* get the thread's result */
00178     int  return_value = smtu->return_value();
00179 
00180     /* clean up */
00181     delete smtu;
00182 
00183     /* return from main() */
00184     return return_value;
00185 }
00186 

Generated on Mon Nov 8 11:12:36 2010 for Shore Storage Manager by  doxygen 1.4.7