Advertisement
djk77

ipc.h

Apr 1st, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. /**
  2. * @file ipc.h
  3. * @Author Michael Kosyakov and Evgeniy Ivanov (ifmo.distributedclass@gmail.com)
  4. * @date March, 2014
  5. * @brief A simple IPC library for programming assignments
  6. *
  7. * Students must not modify this file!
  8. */
  9.  
  10. #ifndef __IFMO_DISTRIBUTED_CLASS_IPC__H
  11. #define __IFMO_DISTRIBUTED_CLASS_IPC__H
  12.  
  13. #include <stddef.h>
  14. #include <stdint.h>
  15.  
  16. //------------------------------------------------------------------------------
  17.  
  18. typedef int8_t local_id;
  19. typedef int16_t timestamp_t;
  20.  
  21. enum {
  22. MESSAGE_MAGIC = 0xAFAF,
  23. MAX_MESSAGE_LEN = 4096,
  24. PARENT_ID = 0,
  25. MAX_PROCESS_ID = 15
  26. };
  27.  
  28. typedef enum {
  29. STARTED = 0, ///< message with string (doesn't include trailing '\0')
  30. DONE, ///< message with string (doesn't include trailing '\0')
  31. ACK, ///< empty message
  32. STOP, ///< empty message
  33. TRANSFER, ///< message with TransferOrder
  34. BALANCE_HISTORY, ///< message with BalanceHistory
  35. CS_REQUEST, ///< empty message
  36. CS_REPLY, ///< empty message
  37. CS_RELEASE ///< empty message
  38. } MessageType;
  39.  
  40. typedef struct {
  41. uint16_t s_magic; ///< magic signature, must be MESSAGE_MAGIC
  42. uint16_t s_payload_len; ///< length of payload
  43. int16_t s_type; ///< type of the message
  44. timestamp_t s_local_time; ///< set by sender, depends on particular PA:
  45. ///< physical time in PA2 or Lamport's scalar
  46. ///< time in PA3
  47. } __attribute__((packed)) MessageHeader;
  48.  
  49. enum {
  50. MAX_PAYLOAD_LEN = MAX_MESSAGE_LEN - sizeof(MessageHeader)
  51. };
  52.  
  53. typedef struct {
  54. MessageHeader s_header;
  55. char s_payload[MAX_PAYLOAD_LEN]; ///< Must be used as a buffer, unused "tail"
  56. ///< shouldn't be transfered
  57. } __attribute__((packed)) Message;
  58.  
  59. //------------------------------------------------------------------------------
  60.  
  61. /** Send a message to the process specified by id.
  62. *
  63. * @param self Any data structure implemented by students to perform I/O
  64. * @param dst ID of recepient
  65. * @param msg Message to send
  66. *
  67. * @return 0 on success, any non-zero value on error
  68. */
  69. int send(void * self, local_id dst, const Message * msg);
  70.  
  71. //------------------------------------------------------------------------------
  72.  
  73. /** Send multicast message.
  74. *
  75. * Send msg to all other processes including parrent.
  76. * Should stop on the first error.
  77. *
  78. * @param self Any data structure implemented by students to perform I/O
  79. * @param msg Message to multicast.
  80. *
  81. * @return 0 on success, any non-zero value on error
  82. */
  83. int send_multicast(void * self, const Message * msg);
  84.  
  85. //------------------------------------------------------------------------------
  86.  
  87. /** Receive a message from the process specified by id.
  88. *
  89. * Might block depending on IPC settings.
  90. *
  91. * @param self Any data structure implemented by students to perform I/O
  92. * @param from ID of the process to receive message from
  93. * @param msg Message structure allocated by the caller
  94. *
  95. * @return 0 on success, any non-zero value on error
  96. */
  97. int receive(void * self, local_id from, Message * msg);
  98.  
  99. //------------------------------------------------------------------------------
  100.  
  101. /** Receive a message from any process.
  102. *
  103. * Receive a message from any process, in case of blocking I/O should be used
  104. * with extra care to avoid deadlocks.
  105. *
  106. * @param self Any data structure implemented by students to perform I/O
  107. * @param msg Message structure allocated by the caller
  108. *
  109. * @return 0 on success, any non-zero value on error
  110. */
  111. int receive_any(void * self, Message * msg);
  112.  
  113. //------------------------------------------------------------------------------
  114.  
  115. #endif // __IFMO_DISTRIBUTED_CLASS_IPC__H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement