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 <cstring>
31 #include <cstdlib>
32 #include <signal.h>
33 #include <iostream>
34 
35 using namespace fawkes;
36 
37 #define MAGIC_TOKEN "FawkesShmemQAApp"
38 
39 class QASharedMemoryHeader : public SharedMemoryHeader
40 {
41  private:
42  typedef struct {
43  unsigned int type;
44  } qashmem_header_t;
45 
46  public:
47  QASharedMemoryHeader(unsigned int type)
48  {
49  header.type = type;
50  }
51 
52  virtual SharedMemoryHeader *
53  clone() const
54  {
55  QASharedMemoryHeader *qs = new QASharedMemoryHeader(header.type);
56  return qs;
57  }
58 
59  virtual bool operator==(const SharedMemoryHeader &s) const
60  {
61  const QASharedMemoryHeader *qs = dynamic_cast<const QASharedMemoryHeader *>(&s);
62  return (qs && (header.type == qs->header.type));
63  }
64 
65  virtual bool matches(void *memptr)
66  {
67  return (memcmp(memptr, &header, sizeof(qashmem_header_t)) == 0);
68  }
69 
70  virtual size_t size()
71  {
72  return sizeof(qashmem_header_t);
73  }
74 
75  virtual bool create()
76  {
77  return true;
78  }
79 
80  virtual void initialize(void *memptr)
81  {
82  memcpy(memptr, (char *)&header, sizeof(qashmem_header_t));
83  }
84 
85  virtual void set(void *memptr)
86  {
87  memcpy((char *)&header, memptr, sizeof(qashmem_header_t));
88  }
89 
90 
91  virtual void reset()
92  {
93  }
94 
95  virtual size_t data_size()
96  {
97  return 1024;
98  }
99 
100  private:
101  qashmem_header_t header;
102 };
103 
104 
105 bool quit;
106 
107 void
108 signal_handler(int signum)
109 {
110  quit = true;
111 }
112 
113 
114 int
115 main(int argc, char **argv)
116 {
117  quit = false;
118  signal(SIGINT, signal_handler);
119 
120  QASharedMemoryHeader *h1 = new QASharedMemoryHeader(1);
121 
122  SharedMemory *s1, *s2;
123 
124  try {
125  // This will create the shared memory segment
126  s1 = new SharedMemory(MAGIC_TOKEN, h1,
127  /* read only */ false,
128  /* create */ true,
129  /* destroy */ true);
130 
131  // This will attach to the existing shmem segment,
132  // use ipcs to check
133  s2 = new SharedMemory(MAGIC_TOKEN, h1,
134  /* read only */ true,
135  /* create */ false,
136  /* destroy */ false);
137  } catch ( ShmCouldNotAttachException &e ) {
138  e.print_trace();
139  exit(1);
140  }
141 
142  int *m1 = (int *)s1->memptr();
143  int *m2 = (int *)s2->memptr();
144 
145  int i = 0;
146 
147  while ( ! quit ) {
148  *m1 = i;
149  std::cout << "Wrote " << *m1 << " (should be " << i
150  << ") to b1, afterwards b2 reads: " << *m2
151  << std::endl;
152  usleep(500000);
153  ++i;
154  };
155 
156  delete s2;
157  delete s1;
158  delete h1;
159 }
160 
161 /// @endcond
Fawkes library namespace.
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:682
Could not attach to shared memory segment.
Shared memory segment.
Definition: shm.h:49
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
Interface for shared memory header.
Definition: shm.h:33