replication.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. """This layer is responsible for replicating with remote home servers using
  16. a given transport.
  17. """
  18. from .federation_client import FederationClient
  19. from .federation_server import FederationServer
  20. from .transaction_queue import TransactionQueue
  21. from .persistence import TransactionActions
  22. import logging
  23. logger = logging.getLogger(__name__)
  24. class ReplicationLayer(FederationClient, FederationServer):
  25. """This layer is responsible for replicating with remote home servers over
  26. the given transport. I.e., does the sending and receiving of PDUs to
  27. remote home servers.
  28. The layer communicates with the rest of the server via a registered
  29. ReplicationHandler.
  30. In more detail, the layer:
  31. * Receives incoming data and processes it into transactions and pdus.
  32. * Fetches any PDUs it thinks it might have missed.
  33. * Keeps the current state for contexts up to date by applying the
  34. suitable conflict resolution.
  35. * Sends outgoing pdus wrapped in transactions.
  36. * Fills out the references to previous pdus/transactions appropriately
  37. for outgoing data.
  38. """
  39. def __init__(self, hs, transport_layer):
  40. self.server_name = hs.hostname
  41. self.keyring = hs.get_keyring()
  42. self.transport_layer = transport_layer
  43. self.federation_client = self
  44. self.store = hs.get_datastore()
  45. self.handler = None
  46. self.edu_handlers = {}
  47. self.query_handlers = {}
  48. self._clock = hs.get_clock()
  49. self.transaction_actions = TransactionActions(self.store)
  50. self._transaction_queue = TransactionQueue(hs, transport_layer)
  51. self._order = 0
  52. self.hs = hs
  53. def __str__(self):
  54. return "<ReplicationLayer(%s)>" % self.server_name