Fawkes API  Fawkes Development Version
qa_ipc_shmem.cpp
1 
2 /***************************************************************************
3  * qa_shmem.h - QA for IPC shared memory
4  *
5  * Generated: Sun Sep 17 16:32:25 2006
6  * Copyright 2005-2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 // Do not include in api reference
25 ///@cond QA
26 
27 #include <utils/ipc/shm.h>
28 #include <utils/ipc/shm_exceptions.h>
29 
30 #include <cstdlib>
31 #include <cstring>
32 #include <iostream>
33 #include <signal.h>
34 
35 using namespace fawkes;
36 
37 #define MAGIC_TOKEN "FawkesShmemQAApp"
38 
39 class QASharedMemoryHeader : public SharedMemoryHeader
40 {
41 private:
42  typedef struct
43  {
44  unsigned int type;
45  } qashmem_header_t;
46 
47 public:
48  QASharedMemoryHeader(unsigned int type)
49  {
50  header.type = type;
51  }
52 
53  virtual SharedMemoryHeader *
54  clone() const
55  {
56  QASharedMemoryHeader *qs = new QASharedMemoryHeader(header.type);
57  return qs;
58  }
59 
60  virtual bool
61  operator==(const SharedMemoryHeader &s) const
62  {
63  const QASharedMemoryHeader *qs = dynamic_cast<const QASharedMemoryHeader *>(&s);
64  return (qs && (header.type == qs->header.type));
65  }
66 
67  virtual bool
68  matches(void *memptr)
69  {
70  return (memcmp(memptr, &header, sizeof(qashmem_header_t)) == 0);
71  }
72 
73  virtual size_t
74  size()
75  {
76  return sizeof(qashmem_header_t);
77  }
78 
79  virtual bool
80  create()
81  {
82  return true;
83  }
84 
85  virtual void
86  initialize(void *memptr)
87  {
88  memcpy(memptr, (char *)&header, sizeof(qashmem_header_t));
89  }
90 
91  virtual void
92  set(void *memptr)
93  {
94  memcpy((char *)&header, memptr, sizeof(qashmem_header_t));
95  }
96 
97  virtual void
98  reset()
99  {
100  }
101 
102  virtual size_t
103  data_size()
104  {
105  return 1024;
106  }
107 
108 private:
109  qashmem_header_t header;
110 };
111 
112 bool quit;
113 
114 void
115 signal_handler(int signum)
116 {
117  quit = true;
118 }
119 
120 int
121 main(int argc, char **argv)
122 {
123  quit = false;
124  signal(SIGINT, signal_handler);
125 
126  QASharedMemoryHeader *h1 = new QASharedMemoryHeader(1);
127 
128  SharedMemory *s1, *s2;
129 
130  try {
131  // This will create the shared memory segment
132  s1 = new SharedMemory(MAGIC_TOKEN,
133  h1,
134  /* read only */ false,
135  /* create */ true,
136  /* destroy */ true);
137 
138  // This will attach to the existing shmem segment,
139  // use ipcs to check
140  s2 = new SharedMemory(MAGIC_TOKEN,
141  h1,
142  /* read only */ true,
143  /* create */ false,
144  /* destroy */ false);
145  } catch (ShmCouldNotAttachException &e) {
146  e.print_trace();
147  exit(1);
148  }
149 
150  int *m1 = (int *)s1->memptr();
151  int *m2 = (int *)s2->memptr();
152 
153  int i = 0;
154 
155  while (!quit) {
156  *m1 = i;
157  std::cout << "Wrote " << *m1 << " (should be " << i << ") to b1, afterwards b2 reads: " << *m2
158  << std::endl;
159  usleep(500000);
160  ++i;
161  };
162 
163  delete s2;
164  delete s1;
165  delete h1;
166 }
167 
168 /// @endcond
fawkes::SharedMemory
Shared memory segment.
Definition: shm.h:53
fawkes::SharedMemoryHeader
Interface for shared memory header.
Definition: shm.h:34
fawkes::SharedMemory::memptr
void * memptr() const
Get a pointer to the shared memory This method returns a pointer to the data-segment of the shared me...
Definition: shm.cpp:734
fawkes
Fawkes library namespace.
fawkes::Exception::print_trace
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:601
fawkes::ShmCouldNotAttachException
Could not attach to shared memory segment.
Definition: shm_exceptions.h:33