units.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """ Defines the JSON structure of the protocol units used by the server to
  16. server protocol.
  17. """
  18. import logging
  19. from synapse.util.jsonobject import JsonEncodedObject
  20. logger = logging.getLogger(__name__)
  21. class Edu(JsonEncodedObject):
  22. """ An Edu represents a piece of data sent from one homeserver to another.
  23. In comparison to Pdus, Edus are not persisted for a long time on disk, are
  24. not meaningful beyond a given pair of homeservers, and don't have an
  25. internal ID or previous references graph.
  26. """
  27. valid_keys = [
  28. "origin",
  29. "destination",
  30. "edu_type",
  31. "content",
  32. ]
  33. required_keys = [
  34. "edu_type",
  35. ]
  36. internal_keys = [
  37. "origin",
  38. "destination",
  39. ]
  40. class Transaction(JsonEncodedObject):
  41. """ A transaction is a list of Pdus and Edus to be sent to a remote home
  42. server with some extra metadata.
  43. Example transaction::
  44. {
  45. "origin": "foo",
  46. "prev_ids": ["abc", "def"],
  47. "pdus": [
  48. ...
  49. ],
  50. }
  51. """
  52. valid_keys = [
  53. "transaction_id",
  54. "origin",
  55. "destination",
  56. "origin_server_ts",
  57. "previous_ids",
  58. "pdus",
  59. "edus",
  60. "pdu_failures",
  61. ]
  62. internal_keys = [
  63. "transaction_id",
  64. "destination",
  65. ]
  66. required_keys = [
  67. "transaction_id",
  68. "origin",
  69. "destination",
  70. "origin_server_ts",
  71. "pdus",
  72. ]
  73. def __init__(self, transaction_id=None, pdus=[], **kwargs):
  74. """ If we include a list of pdus then we decode then as PDU's
  75. automatically.
  76. """
  77. # If there's no EDUs then remove the arg
  78. if "edus" in kwargs and not kwargs["edus"]:
  79. del kwargs["edus"]
  80. super(Transaction, self).__init__(
  81. transaction_id=transaction_id,
  82. pdus=pdus,
  83. **kwargs
  84. )
  85. @staticmethod
  86. def create_new(pdus, **kwargs):
  87. """ Used to create a new transaction. Will auto fill out
  88. transaction_id and origin_server_ts keys.
  89. """
  90. if "origin_server_ts" not in kwargs:
  91. raise KeyError(
  92. "Require 'origin_server_ts' to construct a Transaction"
  93. )
  94. if "transaction_id" not in kwargs:
  95. raise KeyError(
  96. "Require 'transaction_id' to construct a Transaction"
  97. )
  98. for p in pdus:
  99. p.transaction_id = kwargs["transaction_id"]
  100. kwargs["pdus"] = [p.get_pdu_json() for p in pdus]
  101. return Transaction(**kwargs)