summaryrefslogtreecommitdiff
path: root/docs/tutorials/019/page05.html
blob: b04217a95511ac2b122c55d3780778bd3141ef44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="James CE Johnson">
   <TITLE>ACE Tutorial 019</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">

<CENTER><B><FONT SIZE=+2>ACE Tutorial 019</FONT></B></CENTER>

<CENTER><B><FONT SIZE=+2>Sharing your Memories</FONT></B></CENTER>

<P>
<HR WIDTH="100%">
      Here's the mysterious shmem.h.  I wanted to show it after the
      placement-new client and server so that the SharedData object
      would have some relevance.
    <HR>
<HR width=50%><P><center>shmem.h</center><HR width=50%>
<PRE>

<font color=blue>#ifndef</font> <font color=purple>SHMEM_H</font>
<font color=blue>#define</font> <font color=purple>SHMEM_H</font>

<font color=red>// This is where you'll find the ACE_Shared_Memory_SV object</font>
<font color=blue>#include</font> "<font color=green>ace/Shared_Memory_SV.h</font>"

<font color=red>// SHMSZ is just enough for the alphabet and a null terminator</font>
<font color=blue>#define</font> <font color=purple>SHMSZ</font> 27

<font color=red>// Play with this, pick a value you like that isn't used by something else.</font>
<font color=blue>#define</font> <font color=purple>SHM_KEY</font> 4200

<font color=red>/*
  This is what we stuff into shared memory via placement new in the
  second client/server pair.  Notice that it is a very basic object
  with no virtual methods and only concrete data.
 */</font>
class SharedData
{
public:
        <font color=red>// Construct the object and optionally initialize buf_.</font>
    SharedData(int _initialize = 1);

        <font color=red>// Put some data into buf_</font>
    void set(void);
        <font color=red>// Show the data in buf_</font>
    void show(void);
        <font color=red>// What is the value of available_</font>
    int available(void);
        <font color=red>// Set the value of available_</font>
    void available(int _available);
        
protected:
        <font color=red>// Big enough for a simple message</font>
    char buf_[128];
        <font color=red>// A cheap mutex</font>
    int available_;
};

<font color=blue>#endif</font> <font color=red>// SHMEM_H</font>
</PRE>
<HR width=50%><P><center>shmem.cpp</center><HR width=50%>
<PRE>

<font color=blue>#include</font> "<font color=green>shmem.h</font>"

<font color=red>/*
  Set the available_ flag to zero & optionally initialize the buf_
  area.
*/</font>
<font color=#008888>SharedData::SharedData</font>(int _initialize)
    : available_(0)
{
    if( _initialize )
    {
        <font color=#008888>ACE_OS::sprintf</font>(buf_,"<font color=green>UNSET\n</font>");
    }
}

<font color=red>/*
  Write the process ID into the buffer.  This will prove to us that
  the data really is shared between the client and server.
*/</font>
void <font color=#008888>SharedData::set</font>(void)
{
    <font color=#008888>ACE_OS::sprintf</font>(buf_,"<font color=green>My PID is (%d)\n</font>",ACE_OS::getpid());
}

<font color=red>/*
  Display the buffer to the user
*/</font>
void <font color=#008888>SharedData::show</font>(void)
{
    ACE_DEBUG ((LM_INFO, "<font color=green>(%P|%t) Shared Data text is (%s)\n</font>",
                buf_ ));
}

<font color=red>// Show flag</font>
int <font color=#008888>SharedData::available</font>(void)
{
    return available_;
}

<font color=red>// Set flag</font>
void <font color=#008888>SharedData::available</font>(int _available)
{
    available_ = _available;
}
</PRE>
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page06.html">Continue This Tutorial</A>]</CENTER>