client-run.c

Go to the documentation of this file.
00001 #include "client-run.h"
00002 
00003 #include "ac_app_context_private.h"
00004 #include "kr_controller.h"
00005 #include "er_errors.h"
00006 #include "up_uploader.h"
00007 #include "utils_cl2.h"
00008 
00009 #include "common/assertions.h"
00010 #include "common/logging-stack.h"
00011 
00012 //#include <glib-object.h> // g_type_init
00013 
00014 #if __HAVE_SIGNAL__
00015 #include <signal.h>
00016 #endif
00017 
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 
00021 #if !defined(__SYMBIAN32__)
00022 
00023 // Note that these "Run" methods are not required in a typical Symbian
00024 // application, since Symbian applications have a built in event loop,
00025 // and a blocking call to "run" the client to the finish in the main
00026 // thread makes little sense. More likely you will run it on the
00027 // background in the main thread.
00028 
00029 gboolean cl2RunOnce(GError** error)
00030 {
00031   assert_error_unset(error);
00032 
00033   gboolean res = TRUE;
00034 
00035   kr_Controller* client = kr_Controller_new(error);
00036   if (!client) {
00037     logt("error in client creation");
00038     return FALSE;
00039   }
00040 
00041   if (!kr_Controller_start(client, error)) {
00042     logt("error starting client");
00043     res = FALSE;
00044     goto cleanup;
00045   }
00046 
00047   if (!kr_Controller_run(client, error)) {
00048     logt("error running client");
00049     res = FALSE;
00050   }
00051   logt("event loop exited");
00052 
00053   kr_Controller_stop(client);
00054   logt("client stopped");
00055     
00056 cleanup:
00057   kr_Controller_destroy(client);
00058   logt("client destroyed");
00059   return res;
00060 }
00061 
00062 // public interface
00063 int cl2RunOnceGetExitCode()
00064 {
00065   GError* localError = NULL;
00066   GError** error = &localError;
00067   int exitCode = 0;
00068 
00069   if (!cl2RunOnce(error)) {
00070     gx_txtlog_error_clear(error);
00071     exitCode = 1;
00072   }
00073 
00074   logt("exiting");
00075   return exitCode;
00076 }
00077 
00078 #endif // run functions
00079 
00080 #if defined(__SYMBIAN32__)
00081 #include <pipsversion.h>
00082 #endif /* __SYMBIAN32__ */
00083 
00084 #define TRAP_OOM_ENOMEM(_x) TRAP_OOM_VALUE(ENOMEM, _x)
00085 
00086 // public interface
00087 // 
00088 // We shall return a POSIX error code if there is an error, or -1 if
00089 // there is no appropriate standard value. See
00090 // <asm-generic/errno-base.h>.
00091 int cl2GlobalInit()
00092 {
00093   log_clear(PRIMARY_LOG_FILENAME);
00094   log_text(PRIMARY_LOG_FILENAME, "initializing");
00095   logg("app '%s' v%s variant '%s'", 
00096        __APP_NAME__, __VERSION_STRING__, __VARIANT_NAME__);
00097 #if defined(__ABLD_VARIANT__)
00098   logg("Symbian ABLD build variant is '%s'", __ABLD_VARIANT__);
00099 #endif
00100 #if defined(__GNUC__)
00101   logg("compiled with %s version %d.%d.%d (%06d)",
00102        __COMPILER_NAME__,
00103        __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__,
00104        __GCC_VERSION__);
00105 #endif
00106 #if defined(__SYMBIAN32__)
00107   logg("compiled against PIPS version %03u", PIPS_VERSION);
00108 #endif /* __SYMBIAN32__ */
00109   logg("compiled against GLib %u.%u.%u", 
00110        GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, 
00111        GLIB_MICRO_VERSION);
00112 #if !defined(__SYMBIAN32__)
00113   // These are not available on Symbian, as variables cannot be exported.
00114   logg("running with GLib %u.%u.%u", 
00115        glib_major_version, glib_minor_version, 
00116        glib_micro_version);
00117 #endif /* __SYMBIAN32__ */
00118   logg("built on %s at %s", __DATE__, __TIME__);
00119 
00120   log_ctx(PRIMARY_LOG_FILENAME, "context test");
00121 #if __DO_LOGGING__
00122   TRAP_OOM_ENOMEM({
00123       gchar* eData = g_strescape("hello", NULL);
00124       logg("'hello' is '%s'", eData);
00125       g_free(eData);
00126     });
00127 #endif
00128   logst;
00129 
00130   // This ensures that there will be no errors allocating our standard
00131   // error domains at runtime.
00132   TRAP_OOM_ENOMEM(preallocate_all_quarks);
00133 
00134 #ifndef __EPOC32__
00135   // Do not want any console popping up if STDIOSERVER is installed.
00136   printf("console test\n");
00137 #endif
00138 
00139 #if __DO_LOGGING__ && 0
00140   {
00141     SET_TRAP_OOM_VALUE(ENOMEM);
00142     double td = 6.38000011;
00143     logg("printf %%f %f", td);
00144     logg("printf %%g %g", td);
00145     logg("printf %%.6f %.6f", td);
00146     char tdb[50];
00147     snprintf(tdb, 50, "snprintf %%f %f", td); logt(tdb);
00148     snprintf(tdb, 50, "snprintf %%g %g", td); logt(tdb);
00149     snprintf(tdb, 50, "snprintf %%.6f %.6f", td); logt(tdb);
00150     g_snprintf_fix(tdb, 50, "g_snprintf_fix %%f %f", td); logt(tdb);
00151     g_snprintf_fix(tdb, 50, "g_snprintf_fix %%g %g", td); logt(tdb);
00152     g_snprintf_fix(tdb, 50, "g_snprintf_fix %%.6f %.6f", td); logt(tdb);
00153     UNSET_TRAP_OOM();
00154   }
00155 #endif
00156 
00157 #if 0 // OOM testing
00158   {
00159     SET_TRAP_OOM_VALUE(ENOMEM);
00160     logt("invoking g_strnfill");
00161     gchar* s = g_strnfill(100000000 /*gsize length*/,
00162         'a' /*gchar fill_char*/);
00163     logg("s is %d", (int)s);
00164     s[5] = '\0';
00165     logg("s contains '%s'", s);
00166     g_free(s);
00167     UNSET_TRAP_OOM();
00168   }
00169 #endif
00170 
00171   // Symbian^3 does support signals, so we invoke this on Symbian as well.
00172 #if __HAVE_SIGNAL__
00173   signal(SIGPIPE, SIG_IGN); // return EPIPE instead
00174 #endif
00175 
00176   srand(time(NULL));
00177 
00178   // Required when using the GObject object system. We no longer are.
00179   //TRAP_OOM_ENOMEM(g_type_init());
00180 
00181   GError* error = NULL;
00182 #if __FEATURE_UPLOADER__
00183   if (!up_global_init(&error)) {
00184     logg("uploader global init failure");
00185     gx_txtlog_error_clear(&error);
00186     return -1;
00187   }
00188 #endif
00189 
00190   ac_AppContext* ac = ac_AppContext_new(&error);
00191   if (G_UNLIKELY(!ac)) {
00192     logt("failure creating app context");
00193     gx_txtlog_error_clear(&error);
00194     return -1;
00195   }
00196   ac_set_global_AppContext(ac);
00197   logt("app ctx sync init complete");
00198 
00199   return 0;
00200 }
00201 
00202 // It must be okay to invoke this even if cl2GlobalInit() failed.
00203 void cl2GlobalCleanup()
00204 {
00205   logt("doing global cleanup");
00206 
00207 #if __FEATURE_UPLOADER__
00208   up_global_cleanup();
00209 #endif
00210 
00211   ac_AppContext* ac = ac_get_global_AppContext();
00212   if (ac) {
00213     ac_set_global_AppContext(NULL);
00214     ac_AppContext_destroy(ac);
00215   }
00216 
00217   logt("global cleanup complete");
00218 }
00219 
00220 /**
00221 
00222 client-run.c
00223 
00224 Copyright 2009 Helsinki Institute for Information Technology (HIIT)
00225 and the authors. All rights reserved.
00226 
00227 Authors: Tero Hasu <tero.hasu@hut.fi>
00228 
00229 Permission is hereby granted, free of charge, to any person
00230 obtaining a copy of this software and associated documentation files
00231 (the "Software"), to deal in the Software without restriction,
00232 including without limitation the rights to use, copy, modify, merge,
00233 publish, distribute, sublicense, and/or sell copies of the Software,
00234 and to permit persons to whom the Software is furnished to do so,
00235 subject to the following conditions:
00236 
00237 The above copyright notice and this permission notice shall be
00238 included in all copies or substantial portions of the Software.
00239 
00240 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00241 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00242 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00243 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00244 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00245 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00246 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00247 SOFTWARE.
00248 
00249  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:52 2011 by Doxygen 1.6.1