00001 /*****************************************************************************\ 00002 * * 00003 * Name : test of rendezvous object * 00004 * Author : Chris Koeritz * 00005 * * 00006 ******************************************************************************* 00007 * Copyright (c) 2004-$now By Author. This program is free software; you can * 00008 * redistribute it and/or modify it under the terms of the GNU General Public * 00009 * License as published by the Free Software Foundation; either version 2 of * 00010 * the License or (at your option) any later version. This is online at: * 00011 * http://www.fsf.org/copyleft/gpl.html * 00012 * Please send any updates to: fred@gruntose.com * 00013 \*****************************************************************************/ 00014 00015 #include <basis/byte_array.h> 00016 #include <basis/function.h> 00017 #include <basis/guards.h> 00018 #include <opsystem/application_shell.h> 00019 #include <loggers/console_logger.h> 00020 #include <loggers/file_logger.h> 00021 #include <opsystem/rendezvous.h> 00022 #include <data_struct/static_memory_gremlin.h> 00023 00024 #include <stdio.h> 00025 00026 const int CHECKING_INTERVAL = 200; 00027 // this many milliseconds elapses between timer hits. 00028 00029 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s) 00030 00032 00033 class test_rendezvous : public application_shell 00034 { 00035 public: 00036 test_rendezvous() 00037 : application_shell(class_name()), 00038 _got_lock(false), 00039 _mutt("rendezvous_testers_rendezvous_lock") {} 00040 00041 ~test_rendezvous() { 00043 } 00044 00045 IMPLEMENT_CLASS_NAME("test_rendezvous"); 00046 00047 bool already_running(rendezvous::locking_methods how_to_check) { 00048 FUNCDEF("already_running"); 00049 LOG("checking whether the application is already running..."); 00050 _got_lock = _mutt.lock(how_to_check); 00051 return !_got_lock; 00052 } 00053 00054 int execute() { 00055 FUNCDEF("execute"); 00056 rendezvous::locking_methods check_mode = rendezvous::IMMEDIATE_RETURN; 00057 // LOG("note: you can pass -infinite if you want to wait for rendezvous."); 00058 if (__argc >= 2) { 00059 istring flag = __argv[1]; 00060 if (flag.contains("infinite")) { 00061 check_mode = rendezvous::ENDLESS_WAIT; 00062 } 00063 } 00064 if (already_running(check_mode)) { 00065 LOG("the application was already running."); 00066 return 1; 00067 } 00068 LOG("this is the only instance running. hit enter to leave."); 00069 getchar(); 00070 return 0; 00071 } 00072 00073 private: 00074 bool _got_lock; 00075 rendezvous _mutt; 00076 }; 00077 00079 00080 HOOPLE_MAIN(test_rendezvous, ) 00081
1.5.1