Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * qa_shmem.h - QA for IPC shared memory 00004 * 00005 * Generated: Sun Sep 17 16:32:25 2006 00006 * Copyright 2005-2006 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 // Do not include in api reference 00025 ///@cond QA 00026 00027 #include <utils/ipc/shm.h> 00028 #include <utils/ipc/shm_exceptions.h> 00029 00030 #include <cstring> 00031 #include <cstdlib> 00032 #include <signal.h> 00033 #include <iostream> 00034 00035 using namespace fawkes; 00036 00037 #define MAGIC_TOKEN "FawkesShmemQAApp" 00038 00039 class QASharedMemoryHeader : public SharedMemoryHeader 00040 { 00041 private: 00042 typedef struct { 00043 unsigned int type; 00044 } qashmem_header_t; 00045 00046 public: 00047 QASharedMemoryHeader(unsigned int type) 00048 { 00049 header.type = type; 00050 } 00051 00052 virtual SharedMemoryHeader * 00053 clone() const 00054 { 00055 QASharedMemoryHeader *qs = new QASharedMemoryHeader(header.type); 00056 return qs; 00057 } 00058 00059 virtual bool operator==(const SharedMemoryHeader &s) const 00060 { 00061 const QASharedMemoryHeader *qs = dynamic_cast<const QASharedMemoryHeader *>(&s); 00062 return (qs && (header.type == qs->header.type)); 00063 } 00064 00065 virtual bool matches(void *memptr) 00066 { 00067 return (memcmp(memptr, &header, sizeof(qashmem_header_t)) == 0); 00068 } 00069 00070 virtual size_t size() 00071 { 00072 return sizeof(qashmem_header_t); 00073 } 00074 00075 virtual bool create() 00076 { 00077 return true; 00078 } 00079 00080 virtual void initialize(void *memptr) 00081 { 00082 memcpy(memptr, (char *)&header, sizeof(qashmem_header_t)); 00083 } 00084 00085 virtual void set(void *memptr) 00086 { 00087 memcpy((char *)&header, memptr, sizeof(qashmem_header_t)); 00088 } 00089 00090 00091 virtual void reset() 00092 { 00093 } 00094 00095 virtual size_t data_size() 00096 { 00097 return 1024; 00098 } 00099 00100 private: 00101 qashmem_header_t header; 00102 }; 00103 00104 00105 bool quit; 00106 00107 void 00108 signal_handler(int signum) 00109 { 00110 quit = true; 00111 } 00112 00113 00114 int 00115 main(int argc, char **argv) 00116 { 00117 quit = false; 00118 signal(SIGINT, signal_handler); 00119 00120 QASharedMemoryHeader *h1 = new QASharedMemoryHeader(1); 00121 00122 SharedMemory *s1, *s2; 00123 00124 try { 00125 // This will create the shared memory segment 00126 s1 = new SharedMemory(MAGIC_TOKEN, h1, 00127 /* read only */ false, 00128 /* create */ true, 00129 /* destroy */ true); 00130 00131 // This will attach to the existing shmem segment, 00132 // use ipcs to check 00133 s2 = new SharedMemory(MAGIC_TOKEN, h1, 00134 /* read only */ true, 00135 /* create */ false, 00136 /* destroy */ false); 00137 } catch ( ShmCouldNotAttachException &e ) { 00138 e.print_trace(); 00139 exit(1); 00140 } 00141 00142 int *m1 = (int *)s1->memptr(); 00143 int *m2 = (int *)s2->memptr(); 00144 00145 int i = 0; 00146 00147 while ( ! quit ) { 00148 *m1 = i; 00149 std::cout << "Wrote " << *m1 << " (should be " << i 00150 << ") to b1, afterwards b2 reads: " << *m2 00151 << std::endl; 00152 usleep(500000); 00153 ++i; 00154 }; 00155 00156 delete s2; 00157 delete s1; 00158 delete h1; 00159 } 00160 00161 /// @endcond