Process_test.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "util/events/EventBase.h"
  17. #include "util/events/Pipe.h"
  18. #include "util/events/Timeout.h"
  19. #include "memory/Allocator.h"
  20. #include "memory/MallocAllocator.h"
  21. #include "util/events/Process.h"
  22. #include "util/log/Log.h"
  23. #include "util/log/FileWriterLog.h"
  24. #include "util/CString.h"
  25. #include "util/Assert.h"
  26. #include "wire/Message.h"
  27. #include "wire/Error.h"
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #define MESSAGE "IT WORKS!"
  32. #define MESSAGEB "INDEED"
  33. struct Context {
  34. struct Allocator* alloc;
  35. struct EventBase* base;
  36. struct Log* log;
  37. };
  38. static void onConnectionParent(struct Pipe* p, int status)
  39. {
  40. Assert_always(!status);
  41. struct Context* c = p->iface.receiverContext;
  42. struct Allocator* alloc = Allocator_child(c->alloc);
  43. uint8_t* bytes = Allocator_calloc(alloc, CString_strlen(MESSAGE) + 1, 1);
  44. Bits_memcpy(bytes, MESSAGE, CString_strlen(MESSAGE));
  45. struct Message* m = Allocator_clone(alloc, (&(struct Message) {
  46. .length = CString_strlen(MESSAGE),
  47. .padding = 0,
  48. .capacity = CString_strlen(MESSAGE),
  49. .alloc = alloc,
  50. .bytes = bytes
  51. }));
  52. printf("Parent sending message [%s]\n", bytes);
  53. Interface_sendMessage(&p->iface, m);
  54. Allocator_free(alloc);
  55. }
  56. static uint8_t receiveMessageParent(struct Message* msg, struct Interface* iface)
  57. {
  58. struct Context* c = iface->receiverContext;
  59. Assert_always(msg->length == (int)CString_strlen(MESSAGEB));
  60. Assert_always(!Bits_memcmp(msg->bytes, MESSAGEB, CString_strlen(MESSAGEB)));
  61. Allocator_free(c->alloc);
  62. return Error_NONE;
  63. }
  64. static void timeout(void* vNULL)
  65. {
  66. Assert_always(!"timed out.");
  67. }
  68. static void onConnectionChild(struct Pipe* p, int status)
  69. {
  70. Assert_always(!status);
  71. printf("Child connected\n");
  72. }
  73. static uint8_t receiveMessageChild(struct Message* m, struct Interface* iface)
  74. {
  75. printf("Child received message\n");
  76. Assert_always(m->length == (int)CString_strlen(MESSAGE));
  77. Assert_always(!Bits_memcmp(m->bytes, MESSAGE, CString_strlen(MESSAGE)));
  78. Message_shift(m, -((int)CString_strlen(MESSAGE)), NULL);
  79. Message_push(m, MESSAGEB, CString_strlen(MESSAGEB), NULL);
  80. Interface_sendMessage(iface, m);
  81. // shutdown
  82. struct Context* ctx = iface->receiverContext;
  83. Allocator_free(ctx->alloc);
  84. return Error_NONE;
  85. }
  86. static void child(char* name, struct Context* ctx)
  87. {
  88. struct Pipe* pipe = Pipe_named(name, ctx->base, NULL, ctx->alloc);
  89. pipe->logger = ctx->log;
  90. pipe->iface.receiveMessage = receiveMessageChild;
  91. pipe->iface.receiverContext = ctx;
  92. pipe->onConnection = onConnectionChild;
  93. Timeout_setTimeout(timeout, NULL, 2000, ctx->base, ctx->alloc);
  94. EventBase_beginLoop(ctx->base);
  95. }
  96. int main(int argc, char** argv)
  97. {
  98. struct Allocator* alloc = MallocAllocator_new(1<<20);
  99. struct EventBase* eb = EventBase_new(alloc);
  100. struct Log* log = FileWriterLog_new(stdout, alloc);
  101. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  102. ctx->alloc = alloc;
  103. ctx->base = eb;
  104. ctx->log = log;
  105. if (argc > 3 && !CString_strcmp("Process_test", argv[1]) && !CString_strcmp("child", argv[2])) {
  106. child(argv[3], ctx);
  107. return 0;
  108. }
  109. struct Random* rand = Random_new(alloc, log, NULL);
  110. char name[32] = {0};
  111. Random_base32(rand, (uint8_t*)name, 31);
  112. struct Pipe* pipe = Pipe_named(name, eb, NULL, alloc);
  113. pipe->logger = log;
  114. pipe->iface.receiveMessage = receiveMessageParent;
  115. pipe->iface.receiverContext = ctx;
  116. pipe->onConnection = onConnectionParent;
  117. char* path = Process_getPath(alloc);
  118. Assert_always(path != NULL);
  119. #ifdef win32
  120. Assert_always(strstr(path, ":\\") == path + 1); /* C:\ */
  121. Assert_always(strstr(path, ".exe"));
  122. #else
  123. Assert_always(path[0] == '/');
  124. #endif
  125. char* args[] = { "Process_test", "child", name, NULL };
  126. Assert_always(!Process_spawn(path, args, eb, alloc));
  127. Timeout_setTimeout(timeout, NULL, 2000, eb, alloc);
  128. EventBase_beginLoop(eb);
  129. return 0;
  130. }