1
0

replication.py 2.2 KB

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