summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_3/ex03.html
blob: 743f0b395d2148731e96c620aea6b4bad6cd04ac (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
113
114
115
<PRE>
<font color=red>// $Id$</font>

<font color=blue>#include</font> "<font color=green>ace/Memory_Pool.h</font>"
<font color=blue>#include</font> "<font color=green>ace/Shared_Memory_MM.h</font>"
<font color=blue>#include</font> "<font color=green>ace/Malloc.h</font>"
<font color=blue>#include</font> "<font color=green>ace/Malloc_T.h</font>"
<font color=blue>#include</font> "<font color=green>ace/Thread_Manager.h</font>"

<font color=blue>#define</font> <font color=purple>DATA_SIZE</font> 100
<font color=blue>#define</font> <font color=purple>MESSAGE1</font> "<font color=green>Hiya over there client process</font>"
<font color=blue>#define</font> <font color=purple>MESSAGE2</font>  "<font color=green>Did you hear me the first time?</font>"
LPCTSTR poolname="<font color=green>My_Pool</font>";

typedef ACE_Malloc&lt;ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex> Malloc_Allocator;

static void *
server (void * = 0)
{
  ACE_MMAP_Memory_Pool_Options opt;

  <font color=red>//Create the memory allocator passing it the shared memory</font>
  <font color=red>//pool that you want to use</font>
  Malloc_Allocator shm_allocator(poolname,poolname,&opt);

  <font color=red>//Create a message, allocate memory for it and bind it with</font>
  <font color=red>//a name so that the client can the find it in the memory</font>
  <font color=red>//pool</font>
  char* Message1=(char*)shm_allocator.malloc(strlen(MESSAGE1)+1);

  <font color=#008888>ACE_OS::strcpy</font>(Message1,MESSAGE1);
  shm_allocator.bind("<font color=green>FirstMessage</font>",Message1);
  ACE_DEBUG((LM_DEBUG,"<font color=green>&lt;&lt;%s\n</font>",Message1));

  <font color=red>//How about a second message</font>
  char* Message2=(char*)shm_allocator.malloc(strlen(MESSAGE2)+1);
  <font color=#008888>ACE_OS::strcpy</font>(Message2,MESSAGE2);
  shm_allocator.bind("<font color=green>SecondMessage</font>",Message2);
  ACE_DEBUG((LM_DEBUG,"<font color=green>&lt;&lt;%s\n</font>",Message2));

  <font color=red>//Set the Server to go to sleep for a while so that the client has</font>
  <font color=red>//a chance to do its stuff</font>
  ACE_DEBUG((LM_DEBUG, "<font color=green>Server done writing.. going to sleep zzz..\n\n\n</font>"));
  <font color=#008888>ACE_OS::sleep</font>(10); 

  ACE_DEBUG ((LM_DEBUG, "<font color=green>server exit\n</font>"));

  return 0;
}

static void *
client (void * = 0)
{
  ACE_MMAP_Memory_Pool_Options opt;

  <font color=red>//Create the memory allocator passing it the shared memory</font>
  <font color=red>//pool that you want to use</font>
  Malloc_Allocator shm_allocator(poolname,poolname,&opt);

  <font color=red>//Lets get that first message.  Notice that the find is looking up the</font>
  <font color=red>//memory based on the "<font color=green>name</font>" that was bound to it by the server.</font>
  void *Message1 = 0;
  if(shm_allocator.find("<font color=green>FirstMessage</font>") == -1 )
    {
      ACE_ERROR((LM_ERROR,
                 "<font color=green>Client ack\n</font>"));
      return 0;
    }
  if(shm_allocator.find("<font color=green>FirstMessage</font>",Message1)==-1)
    {
      ACE_ERROR((LM_ERROR,
                 "<font color=green>Client: Problem cant find data that server has sent\n</font>"));
      return 0;
    }

  <font color=#008888>ACE_OS::printf</font>("<font color=green>>>%s\n</font>",(char*) Message1);
  <font color=#008888>ACE_OS::fflush</font>(stdout);

  <font color=red>//Lets get that second message now.</font>
  void *Message2;
  if(shm_allocator.find("<font color=green>SecondMessage</font>",Message2)==-1)
    {
      ACE_ERROR((LM_ERROR,
                 "<font color=green>Client: Problem cant find data that server has sent\n</font>"));
      <font color=#008888>ACE_OS::exit</font>(1);
    }
  <font color=#008888>ACE_OS::printf</font>("<font color=green>>>%s\n</font>",(char*)Message2);
  <font color=#008888>ACE_OS::fflush</font>(stdout);

  ACE_DEBUG((LM_DEBUG,"<font color=green>Client done reading! BYE NOW\n</font>"));
  <font color=#008888>ACE_OS::fflush</font>(stdout);

  <font color=red>//Get rid of all resources allocated by the server. In other</font>
  <font color=red>//words get rid of the shared memory pool that had been</font>
  <font color=red>//previously allocated</font>
  shm_allocator.remove();

  return 0;
}

int main (int, char *argv[])
{
  switch (*argv[1])
    {
    case 's':
      server ();
      break;
    default:
      client ();
      break;
    }

  return 0;
}
</PRE>