summary refs log tree commit diff
path: root/synapse/federation/replication.py
blob: 3e062a5eab3c67985f59c269eba1c07901768131 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
# Copyright 2014-2016 OpenMarket Ltd
#
# 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.

"""This layer is responsible for replicating with remote home servers using
a given transport.
"""

from .federation_client import FederationClient
from .federation_server import FederationServer

from .transaction_queue import TransactionQueue

from .persistence import TransactionActions

import logging


logger = logging.getLogger(__name__)


class ReplicationLayer(FederationClient, FederationServer):
    """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

        self.keyring = hs.get_keyring()

        self.transport_layer = transport_layer

        self.federation_client = self

        self.store = hs.get_datastore()

        self.handler = None
        self.edu_handlers = {}
        self.query_handlers = {}

        self._clock = hs.get_clock()

        self.transaction_actions = TransactionActions(self.store)
        self._transaction_queue = TransactionQueue(hs, transport_layer)

        self._order = 0

        self.hs = hs

    def __str__(self):
        return "<ReplicationLayer(%s)>" % self.server_name