2014-08-12 10:10:52 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-01-06 08:21:39 -05:00
|
|
|
# Copyright 2014, 2015 OpenMarket Ltd
|
2014-08-12 10:10:52 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2014-08-12 22:14:34 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
"""This layer is responsible for replicating with remote home servers using
|
|
|
|
a given transport.
|
|
|
|
"""
|
|
|
|
|
2015-01-26 05:45:24 -05:00
|
|
|
from .federation_client import FederationClient
|
|
|
|
from .federation_server import FederationServer
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2015-01-22 11:37:08 -05:00
|
|
|
from .transaction_queue import TransactionQueue
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2015-01-26 05:45:24 -05:00
|
|
|
from .persistence import TransactionActions
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-01-26 05:45:24 -05:00
|
|
|
class ReplicationLayer(FederationClient, FederationServer):
|
2014-08-12 10:10:52 -04:00
|
|
|
"""This layer is responsible for replicating with remote home servers over
|
|
|
|
the given transport. I.e., does the sending and receiving of PDUs to
|
|
|
|
remote home servers.
|
|
|
|
|
|
|
|
The layer communicates with the rest of the server via a registered
|
|
|
|
ReplicationHandler.
|
|
|
|
|
|
|
|
In more detail, the layer:
|
|
|
|
* Receives incoming data and processes it into transactions and pdus.
|
|
|
|
* Fetches any PDUs it thinks it might have missed.
|
|
|
|
* Keeps the current state for contexts up to date by applying the
|
|
|
|
suitable conflict resolution.
|
|
|
|
* Sends outgoing pdus wrapped in transactions.
|
|
|
|
* Fills out the references to previous pdus/transactions appropriately
|
|
|
|
for outgoing data.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, hs, transport_layer):
|
|
|
|
self.server_name = hs.hostname
|
|
|
|
|
2015-01-26 09:33:11 -05:00
|
|
|
self.keyring = hs.get_keyring()
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
self.transport_layer = transport_layer
|
|
|
|
self.transport_layer.register_received_handler(self)
|
|
|
|
self.transport_layer.register_request_handler(self)
|
|
|
|
|
2015-01-26 05:45:24 -05:00
|
|
|
self.federation_client = self
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2015-01-26 05:45:24 -05:00
|
|
|
self.store = hs.get_datastore()
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
self.handler = None
|
|
|
|
self.edu_handlers = {}
|
2014-08-13 11:55:53 -04:00
|
|
|
self.query_handlers = {}
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
self._clock = hs.get_clock()
|
|
|
|
|
2015-01-26 05:45:24 -05:00
|
|
|
self.transaction_actions = TransactionActions(self.store)
|
|
|
|
self._transaction_queue = TransactionQueue(hs, transport_layer)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2015-01-26 05:45:24 -05:00
|
|
|
self._order = 0
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2015-02-11 11:48:05 -05:00
|
|
|
self.hs = hs
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
def __str__(self):
|
|
|
|
return "<ReplicationLayer(%s)>" % self.server_name
|