The Pedigree Project  0.1
user/applications/ipc-test-server/main.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigree/native/ipc/Ipc.h"
21 #include <stdio.h>
22 
23 #include <sys/klog.h>
24 #include <unistd.h>
25 
26 using namespace PedigreeIpc;
27 
28 int main(int argc, char *argv[])
29 {
30  printf("IPC Test: Server, daemonising\n");
31 
32  pid_t id = fork();
33  if (id == -1)
34  {
35  printf("Forking failed.");
36  return 1;
37  }
38  else if (id == 0)
39  {
40  // Grab an endpoint to use.
41  createEndpoint("ipc-test");
42  IpcEndpoint *pEndpoint = getEndpoint("ipc-test");
43 
44  klog(LOG_NOTICE, "IPC Test: Server started and entering message loop.");
45 
46  while (true)
47  {
48  // Wait for an incoming message.
49  IpcMessage *pRecv = 0;
50  if (!recv(pEndpoint, &pRecv, false))
51  {
52  klog(
53  LOG_WARNING,
54  "IPC Test: Server failed to receive a message.");
55  continue;
56  }
57 
58  // Log it.
59  klog(
60  LOG_NOTICE, "IPC Test: Server got message '%s'.",
61  pRecv->getBuffer());
62  delete pRecv;
63 
64  // Send a response.
65  IpcMessage *pResponse = new IpcMessage();
66  pResponse->initialise();
67  char *pBuffer = reinterpret_cast<char *>(pResponse->getBuffer());
68  if (!pBuffer)
69  {
70  klog(LOG_WARNING, "IPC Test: Server message creation failed.");
71  continue;
72  }
73  else
74  klog(
75  LOG_NOTICE, "IPC Test: Server is writing into message %x",
76  pBuffer);
77 
78  sprintf(pBuffer, "Server received message successfully!\n");
79 
80  if (!send(pEndpoint, pResponse, false))
81  klog(
82  LOG_WARNING, "IPC Test: Server failed to send a response.");
83 
84  // Clean up.
85  delete pResponse;
86  }
87  }
88  else
89  printf("Successfully forked, child has PID %d\n", id);
90 
91  return 0;
92 }
A standard IPC message that is less than 4 KB in size.