cc-protocol.txt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. The CC protocol
  2. ===============
  3. We use our home-grown protocol for IPC between modules. There's a
  4. central daemon routing the messages.
  5. Addressing
  6. ----------
  7. Each connected client gets an unique address, called ``l-name''. A
  8. message can be sent directly to such l-name, if it is known to the
  9. sender.
  10. A client may subscribe to a group of communication. A message can be
  11. broadcasted to a whole group instead of a single client. There's also
  12. an instance parameter to addressing, but we didn't find any actual use
  13. for it and it is not used for anything. It is left in the default `*`
  14. for most of our code and should be done so in any new code. It wasn't
  15. priority to remove it yet.
  16. Wire format
  17. -----------
  18. Each message on the wire looks like this:
  19. <message length><header length><header><body>
  20. The message length is 4-byte unsigned integer in network byte order,
  21. specifying the number of bytes of the rest of the message (eg. header
  22. length, header and body put together).
  23. The header length is 2-byte unsigned integer in network byte order,
  24. specifying the length of the header.
  25. The header is a string representation of single JSON object. It
  26. specifies the type of message and routing information.
  27. The body is the payload of the message. It takes the whole rest of
  28. size of the message (so its length is message length - 2 - header
  29. length). The content is not examined by the routing daemon, but the
  30. clients expect it to be valid JSON object.
  31. The body may be empty in case the message is not to be routed to
  32. client, but it is instruction for the routing daemon. See message
  33. types below.
  34. The message is sent in this format to the routing daemon, the daemon
  35. optionally modifies the headers and delivers it in the same format to
  36. the recipient(s).
  37. The headers
  38. -----------
  39. The header object can contain following information:
  40. |====================================================================================================
  41. |Name |type |Description
  42. |====================================================================================================
  43. |from |string|Sender's l-name
  44. |type |string|Type of the message. The routed message is "send".
  45. |group |string|The group to deliver to.
  46. |instance |string|Instance in the group. Purpose lost in history. Defaults to "*".
  47. |to |string|Override recipient (group/instance ignored).
  48. |seq |int |Tracking number of the message.
  49. |reply |int |If present, contains a seq number of message this is a reply to.
  50. |want_answer|bool |If present and true, the daemon generates error if there's no matching recipient.
  51. |====================================================================================================
  52. Types of messages
  53. -----------------
  54. Get Local Name (type "getlname")
  55. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  56. Upon connection, this is the first message to be sent to the daemon.
  57. It will return the local name of this client. Each connection gets
  58. its own unique local name, and local names are never repeated. They
  59. should be considered opaque strings, in a format useful only to the
  60. message routing system. They are used in replies or to send to a
  61. specific destination.
  62. To request the local name, the only element included is the
  63. {"type": "getlname"}
  64. tuple. The response is also a simple, single tuple:
  65. {"lname" => "Opaque utf-8 string"}
  66. Until this message is sent, no other types of messages may be sent on
  67. this connection.
  68. Regular Group Messages (type "send")
  69. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  70. Message routed to other client. This one expects the body to be
  71. non-empty.
  72. Expected headers are:
  73. * from
  74. * group
  75. * instance (set to "*" if no specific instance desired)
  76. * seq (should be unique for the sender)
  77. * to (set to "*" if not directed to specific client)
  78. * reply (optional, only if it is reply)
  79. * want_answer (optional, only when not a reply)
  80. A client does not see its own transmissions.
  81. Group Subscriptions (type "subscribe")
  82. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. Indicates the sender wants to be included in the given group.
  84. Expected headers are:
  85. * group
  86. * instance (leave at "*" for default)
  87. There is no response to this message and the client is subscribed to
  88. the given group and instance.
  89. The group can be any utf-8 string and the group doesn't have to exist
  90. before (it is created when at least one client is in it). A client may
  91. be subscribed in multiple groups.
  92. Group Unsubscribe (type "unsubscribe")
  93. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  94. The headers to be included are "group" and "instance" and have the same
  95. meaning as a "subscribe" message. Only, the client is removed from the
  96. group.
  97. Transmitted messages
  98. --------------------
  99. These are the messages generally transmitted in the body of the
  100. message.
  101. Command
  102. ~~~~~~~
  103. It is a command from one process to another, to do something or send
  104. some information. It is identified by a name and can optionally have
  105. parameters. It'd look like this:
  106. {"command": ["name", <parameters>]}
  107. The parameters may be omitted (then the array is 1 element long). If
  108. present, it may be any JSON element. However, the most usual is an
  109. object with named parameter values.
  110. It is usually transmitted with the `want_answer` header turned on to
  111. cope with the situation the remote end doesn't exist, and sent to a
  112. group (eg. `to` with value of `*`).
  113. Success reply
  114. ~~~~~~~~~~~~~
  115. When the command is successful, the other side answers by a reply of
  116. the following format:
  117. {"result": [0, <result>]}
  118. The result is the return value of the command. It may be any JSON
  119. element and it may be omitted (for the case of ``void'' function).
  120. This is transmitted with the `reply` header set to the `seq` number of
  121. the original command. It is sent with the `to` header set.
  122. Error reply
  123. ~~~~~~~~~~~
  124. In case something goes wrong, an error reply is sent. This is similar
  125. as throwing an exception from local function. The format is similar:
  126. {"result": [ecode, "Error description"]}
  127. The `ecode` is non-zero error code. Most of the current code uses `1`
  128. for all errors. The string after that is mandatory and must contain a
  129. human-readable description of the error.
  130. The negative error codes are reserved for errors from the daemon.
  131. Currently, only `-1` is used and it is generated when a message with
  132. `reply` not included is sent, it has the `want_answer` header set to
  133. `true` and there's no recipient to deliver the message to. This
  134. usually means a command was sent to a non-existent recipient.