2017-03-27 10:40:37 -04:00
|
|
|
# Copyright 2017 Vector Creations 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.
|
|
|
|
"""Defines the various valid commands
|
|
|
|
|
|
|
|
The VALID_SERVER_COMMANDS and VALID_CLIENT_COMMANDS define which commands are
|
|
|
|
allowed to be sent by which side.
|
|
|
|
"""
|
2020-04-07 12:40:22 -04:00
|
|
|
import abc
|
2017-03-27 10:40:37 -04:00
|
|
|
import logging
|
2022-02-08 11:03:08 -05:00
|
|
|
from typing import Optional, Tuple, Type, TypeVar
|
2017-03-27 10:40:37 -04:00
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
from synapse.replication.tcp.streams._base import StreamRow
|
2020-08-19 07:26:03 -04:00
|
|
|
from synapse.util import json_decoder, json_encoder
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
T = TypeVar("T", bound="Command")
|
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
|
2020-04-07 12:40:22 -04:00
|
|
|
class Command(metaclass=abc.ABCMeta):
|
2017-03-27 10:40:37 -04:00
|
|
|
"""The base command class.
|
|
|
|
|
|
|
|
All subclasses must set the NAME variable which equates to the name of the
|
|
|
|
command on the wire.
|
|
|
|
|
|
|
|
A full command line on the wire is constructed from `NAME + " " + to_line()`
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2021-07-15 06:02:43 -04:00
|
|
|
NAME: str
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
@classmethod
|
2020-04-07 12:40:22 -04:00
|
|
|
@abc.abstractmethod
|
2022-02-08 11:03:08 -05:00
|
|
|
def from_line(cls: Type[T], line: str) -> T:
|
2017-03-27 10:40:37 -04:00
|
|
|
"""Deserialises a line from the wire into this command. `line` does not
|
|
|
|
include the command.
|
|
|
|
"""
|
|
|
|
|
2020-04-07 12:40:22 -04:00
|
|
|
@abc.abstractmethod
|
|
|
|
def to_line(self) -> str:
|
2020-07-09 09:52:58 -04:00
|
|
|
"""Serialises the command for the wire. Does not include the command
|
2017-03-27 10:40:37 -04:00
|
|
|
prefix.
|
|
|
|
"""
|
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def get_logcontext_id(self) -> str:
|
2018-08-16 19:43:43 -04:00
|
|
|
"""Get a suitable string for the logcontext when processing this command"""
|
|
|
|
|
|
|
|
# by default, we just use the command name.
|
|
|
|
return self.NAME
|
|
|
|
|
2022-05-20 10:28:23 -04:00
|
|
|
def redis_channel_name(self, prefix: str) -> str:
|
|
|
|
"""
|
|
|
|
Returns the Redis channel name upon which to publish this command.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
prefix: The prefix for the channel.
|
|
|
|
"""
|
|
|
|
return prefix
|
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
SC = TypeVar("SC", bound="_SimpleCommand")
|
|
|
|
|
|
|
|
|
2020-04-07 12:40:22 -04:00
|
|
|
class _SimpleCommand(Command):
|
|
|
|
"""An implementation of Command whose argument is just a 'data' string."""
|
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def __init__(self, data: str):
|
2020-04-07 12:40:22 -04:00
|
|
|
self.data = data
|
|
|
|
|
|
|
|
@classmethod
|
2022-02-08 11:03:08 -05:00
|
|
|
def from_line(cls: Type[SC], line: str) -> SC:
|
2020-04-07 12:40:22 -04:00
|
|
|
return cls(line)
|
|
|
|
|
|
|
|
def to_line(self) -> str:
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
|
|
|
|
class ServerCommand(_SimpleCommand):
|
2017-03-27 10:40:37 -04:00
|
|
|
"""Sent by the server on new connection and includes the server_name.
|
|
|
|
|
|
|
|
Format::
|
|
|
|
|
|
|
|
SERVER <server_name>
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
NAME = "SERVER"
|
|
|
|
|
|
|
|
|
|
|
|
class RdataCommand(Command):
|
|
|
|
"""Sent by server when a subscribed stream has an update.
|
|
|
|
|
|
|
|
Format::
|
|
|
|
|
2020-04-29 11:23:08 -04:00
|
|
|
RDATA <stream_name> <instance_name> <token> <row_json>
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
The `<token>` may either be a numeric stream id OR "batch". The latter case
|
|
|
|
is used to support sending multiple updates with the same stream ID. This
|
|
|
|
is done by sending an RDATA for each row, with all but the last RDATA having
|
|
|
|
a token of "batch" and the last having the final stream ID.
|
|
|
|
|
|
|
|
The client should batch all incoming RDATA with a token of "batch" (per
|
|
|
|
stream_name) until it sees an RDATA with a numeric stream ID.
|
|
|
|
|
2020-04-29 11:23:08 -04:00
|
|
|
The `<instance_name>` is the source of the new data (usually "master").
|
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
`<token>` of "batch" maps to the instance variable `token` being None.
|
|
|
|
|
|
|
|
An example of a batched series of RDATA::
|
|
|
|
|
2020-04-29 11:23:08 -04:00
|
|
|
RDATA presence master batch ["@foo:example.com", "online", ...]
|
|
|
|
RDATA presence master batch ["@bar:example.com", "online", ...]
|
|
|
|
RDATA presence master 59 ["@baz:example.com", "online", ...]
|
2017-03-27 10:40:37 -04:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
NAME = "RDATA"
|
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def __init__(
|
|
|
|
self, stream_name: str, instance_name: str, token: Optional[int], row: StreamRow
|
|
|
|
):
|
2017-03-27 10:40:37 -04:00
|
|
|
self.stream_name = stream_name
|
2020-04-29 11:23:08 -04:00
|
|
|
self.instance_name = instance_name
|
2017-03-27 10:40:37 -04:00
|
|
|
self.token = token
|
|
|
|
self.row = row
|
|
|
|
|
|
|
|
@classmethod
|
2022-02-08 11:03:08 -05:00
|
|
|
def from_line(cls: Type["RdataCommand"], line: str) -> "RdataCommand":
|
2020-04-29 11:23:08 -04:00
|
|
|
stream_name, instance_name, token, row_json = line.split(" ", 3)
|
2017-03-27 10:40:37 -04:00
|
|
|
return cls(
|
2020-04-29 11:23:08 -04:00
|
|
|
stream_name,
|
|
|
|
instance_name,
|
|
|
|
None if token == "batch" else int(token),
|
2020-08-19 07:26:03 -04:00
|
|
|
json_decoder.decode(row_json),
|
2017-03-27 10:40:37 -04:00
|
|
|
)
|
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def to_line(self) -> str:
|
2017-03-27 10:40:37 -04:00
|
|
|
return " ".join(
|
2019-06-20 05:32:02 -04:00
|
|
|
(
|
2017-03-27 10:40:37 -04:00
|
|
|
self.stream_name,
|
2020-04-29 11:23:08 -04:00
|
|
|
self.instance_name,
|
2017-03-27 10:40:37 -04:00
|
|
|
str(self.token) if self.token is not None else "batch",
|
2020-08-19 07:26:03 -04:00
|
|
|
json_encoder.encode(self.row),
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
2017-03-27 10:40:37 -04:00
|
|
|
)
|
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def get_logcontext_id(self) -> str:
|
2018-08-16 19:43:43 -04:00
|
|
|
return "RDATA-" + self.stream_name
|
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
class PositionCommand(Command):
|
2020-10-12 10:51:41 -04:00
|
|
|
"""Sent by an instance to tell others the stream position without needing to
|
|
|
|
send an RDATA.
|
|
|
|
|
|
|
|
Two tokens are sent, the new position and the last position sent by the
|
|
|
|
instance (in an RDATA or other POSITION). The tokens are chosen so that *no*
|
|
|
|
rows were written by the instance between the `prev_token` and `new_token`.
|
|
|
|
(If an instance hasn't sent a position before then the new position can be
|
|
|
|
used for both.)
|
2019-02-26 10:04:34 -05:00
|
|
|
|
2020-04-29 11:23:08 -04:00
|
|
|
Format::
|
|
|
|
|
2020-10-12 10:51:41 -04:00
|
|
|
POSITION <stream_name> <instance_name> <prev_token> <new_token>
|
2020-04-29 11:23:08 -04:00
|
|
|
|
2020-10-12 10:51:41 -04:00
|
|
|
On receipt of a POSITION command instances should check if they have missed
|
|
|
|
any updates, and if so then fetch them out of band. Instances can check this
|
|
|
|
by comparing their view of the current token for the sending instance with
|
|
|
|
the included `prev_token`.
|
2020-04-29 11:23:08 -04:00
|
|
|
|
|
|
|
The `<instance_name>` is the process that sent the command and is the source
|
|
|
|
of the stream.
|
2017-03-27 10:40:37 -04:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
NAME = "POSITION"
|
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def __init__(
|
|
|
|
self, stream_name: str, instance_name: str, prev_token: int, new_token: int
|
|
|
|
):
|
2017-03-27 10:40:37 -04:00
|
|
|
self.stream_name = stream_name
|
2020-04-29 11:23:08 -04:00
|
|
|
self.instance_name = instance_name
|
2020-10-12 10:51:41 -04:00
|
|
|
self.prev_token = prev_token
|
|
|
|
self.new_token = new_token
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
@classmethod
|
2022-02-08 11:03:08 -05:00
|
|
|
def from_line(cls: Type["PositionCommand"], line: str) -> "PositionCommand":
|
2020-10-12 10:51:41 -04:00
|
|
|
stream_name, instance_name, prev_token, new_token = line.split(" ", 3)
|
|
|
|
return cls(stream_name, instance_name, int(prev_token), int(new_token))
|
2017-03-27 10:40:37 -04:00
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def to_line(self) -> str:
|
2020-10-12 10:51:41 -04:00
|
|
|
return " ".join(
|
|
|
|
(
|
|
|
|
self.stream_name,
|
|
|
|
self.instance_name,
|
|
|
|
str(self.prev_token),
|
|
|
|
str(self.new_token),
|
|
|
|
)
|
|
|
|
)
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
|
2020-04-07 12:40:22 -04:00
|
|
|
class ErrorCommand(_SimpleCommand):
|
2017-03-27 10:40:37 -04:00
|
|
|
"""Sent by either side if there was an ERROR. The data is a string describing
|
|
|
|
the error.
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
NAME = "ERROR"
|
|
|
|
|
|
|
|
|
2020-04-07 12:40:22 -04:00
|
|
|
class PingCommand(_SimpleCommand):
|
2020-06-15 08:44:54 -04:00
|
|
|
"""Sent by either side as a keep alive. The data is arbitrary (often timestamp)"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
NAME = "PING"
|
|
|
|
|
|
|
|
|
2020-04-07 12:40:22 -04:00
|
|
|
class NameCommand(_SimpleCommand):
|
2017-03-27 10:40:37 -04:00
|
|
|
"""Sent by client to inform the server of the client's identity. The data
|
|
|
|
is the name
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
NAME = "NAME"
|
|
|
|
|
|
|
|
|
|
|
|
class ReplicateCommand(Command):
|
2020-03-25 10:54:01 -04:00
|
|
|
"""Sent by the client to subscribe to streams.
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
Format::
|
|
|
|
|
2020-03-25 10:54:01 -04:00
|
|
|
REPLICATE
|
2017-03-27 10:40:37 -04:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
NAME = "REPLICATE"
|
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def __init__(self) -> None:
|
2020-03-25 10:54:01 -04:00
|
|
|
pass
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
@classmethod
|
2022-02-08 11:03:08 -05:00
|
|
|
def from_line(cls: Type[T], line: str) -> T:
|
2020-03-25 10:54:01 -04:00
|
|
|
return cls()
|
2017-03-27 10:40:37 -04:00
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def to_line(self) -> str:
|
2020-03-25 10:54:01 -04:00
|
|
|
return ""
|
2018-08-16 19:43:43 -04:00
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
class UserSyncCommand(Command):
|
|
|
|
"""Sent by the client to inform the server that a user has started or
|
2020-04-22 17:39:04 -04:00
|
|
|
stopped syncing on this process.
|
|
|
|
|
|
|
|
This is used by the process handling presence (typically the master) to
|
|
|
|
calculate who is online and who is not.
|
2017-03-27 10:40:37 -04:00
|
|
|
|
2017-03-31 06:46:20 -04:00
|
|
|
Includes a timestamp of when the last user sync was.
|
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
Format::
|
|
|
|
|
2020-03-30 11:37:24 -04:00
|
|
|
USER_SYNC <instance_id> <user_id> <state> <last_sync_ms>
|
2017-03-27 10:40:37 -04:00
|
|
|
|
2020-04-22 17:39:04 -04:00
|
|
|
Where <state> is either "start" or "end"
|
2017-03-27 10:40:37 -04:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
NAME = "USER_SYNC"
|
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def __init__(
|
|
|
|
self, instance_id: str, user_id: str, is_syncing: bool, last_sync_ms: int
|
|
|
|
):
|
2020-03-30 11:37:24 -04:00
|
|
|
self.instance_id = instance_id
|
2017-03-27 10:40:37 -04:00
|
|
|
self.user_id = user_id
|
|
|
|
self.is_syncing = is_syncing
|
2017-03-31 06:46:20 -04:00
|
|
|
self.last_sync_ms = last_sync_ms
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
@classmethod
|
2022-02-08 11:03:08 -05:00
|
|
|
def from_line(cls: Type["UserSyncCommand"], line: str) -> "UserSyncCommand":
|
2020-03-30 11:37:24 -04:00
|
|
|
instance_id, user_id, state, last_sync_ms = line.split(" ", 3)
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
if state not in ("start", "end"):
|
|
|
|
raise Exception("Invalid USER_SYNC state %r" % (state,))
|
|
|
|
|
2020-03-30 11:37:24 -04:00
|
|
|
return cls(instance_id, user_id, state == "start", int(last_sync_ms))
|
2017-03-27 10:40:37 -04:00
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def to_line(self) -> str:
|
2017-03-31 06:46:20 -04:00
|
|
|
return " ".join(
|
2019-06-20 05:32:02 -04:00
|
|
|
(
|
2020-03-30 11:37:24 -04:00
|
|
|
self.instance_id,
|
2017-03-31 06:46:20 -04:00
|
|
|
self.user_id,
|
|
|
|
"start" if self.is_syncing else "end",
|
|
|
|
str(self.last_sync_ms),
|
|
|
|
)
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
|
2020-03-30 11:37:24 -04:00
|
|
|
class ClearUserSyncsCommand(Command):
|
|
|
|
"""Sent by the client to inform the server that it should drop all
|
|
|
|
information about syncing users sent by the client.
|
|
|
|
|
|
|
|
Mainly used when client is about to shut down.
|
|
|
|
|
|
|
|
Format::
|
|
|
|
|
|
|
|
CLEAR_USER_SYNC <instance_id>
|
|
|
|
"""
|
|
|
|
|
|
|
|
NAME = "CLEAR_USER_SYNC"
|
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def __init__(self, instance_id: str):
|
2020-03-30 11:37:24 -04:00
|
|
|
self.instance_id = instance_id
|
|
|
|
|
|
|
|
@classmethod
|
2022-02-08 11:03:08 -05:00
|
|
|
def from_line(
|
|
|
|
cls: Type["ClearUserSyncsCommand"], line: str
|
|
|
|
) -> "ClearUserSyncsCommand":
|
2020-03-30 11:37:24 -04:00
|
|
|
return cls(line)
|
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def to_line(self) -> str:
|
2020-03-30 11:37:24 -04:00
|
|
|
return self.instance_id
|
|
|
|
|
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
class FederationAckCommand(Command):
|
|
|
|
"""Sent by the client when it has processed up to a given point in the
|
|
|
|
federation stream. This allows the master to drop in-memory caches of the
|
|
|
|
federation stream.
|
|
|
|
|
|
|
|
This must only be sent from one worker (i.e. the one sending federation)
|
|
|
|
|
|
|
|
Format::
|
|
|
|
|
2020-07-10 13:26:36 -04:00
|
|
|
FEDERATION_ACK <instance_name> <token>
|
2017-03-27 10:40:37 -04:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
NAME = "FEDERATION_ACK"
|
|
|
|
|
2021-03-29 11:43:20 -04:00
|
|
|
def __init__(self, instance_name: str, token: int):
|
2020-07-10 13:26:36 -04:00
|
|
|
self.instance_name = instance_name
|
2017-03-27 10:40:37 -04:00
|
|
|
self.token = token
|
|
|
|
|
|
|
|
@classmethod
|
2022-02-08 11:03:08 -05:00
|
|
|
def from_line(
|
|
|
|
cls: Type["FederationAckCommand"], line: str
|
|
|
|
) -> "FederationAckCommand":
|
2020-07-10 13:26:36 -04:00
|
|
|
instance_name, token = line.split(" ")
|
|
|
|
return cls(instance_name, int(token))
|
2017-03-27 10:40:37 -04:00
|
|
|
|
2021-03-29 11:43:20 -04:00
|
|
|
def to_line(self) -> str:
|
2020-07-10 13:26:36 -04:00
|
|
|
return "%s %s" % (self.instance_name, self.token)
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
|
2017-06-27 09:58:10 -04:00
|
|
|
class UserIpCommand(Command):
|
|
|
|
"""Sent periodically when a worker sees activity from a client.
|
|
|
|
|
|
|
|
Format::
|
|
|
|
|
|
|
|
USER_IP <user_id>, <access_token>, <ip>, <device_id>, <last_seen>, <user_agent>
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2017-06-27 09:58:10 -04:00
|
|
|
NAME = "USER_IP"
|
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
access_token: str,
|
|
|
|
ip: str,
|
|
|
|
user_agent: str,
|
2022-04-01 08:08:55 -04:00
|
|
|
device_id: Optional[str],
|
2022-02-08 11:03:08 -05:00
|
|
|
last_seen: int,
|
|
|
|
):
|
2017-06-27 09:58:10 -04:00
|
|
|
self.user_id = user_id
|
|
|
|
self.access_token = access_token
|
|
|
|
self.ip = ip
|
|
|
|
self.user_agent = user_agent
|
|
|
|
self.device_id = device_id
|
|
|
|
self.last_seen = last_seen
|
|
|
|
|
|
|
|
@classmethod
|
2022-02-08 11:03:08 -05:00
|
|
|
def from_line(cls: Type["UserIpCommand"], line: str) -> "UserIpCommand":
|
2017-06-27 11:25:38 -04:00
|
|
|
user_id, jsn = line.split(" ", 1)
|
2017-06-27 09:58:10 -04:00
|
|
|
|
2020-08-19 07:26:03 -04:00
|
|
|
access_token, ip, user_agent, device_id, last_seen = json_decoder.decode(jsn)
|
2017-06-27 11:25:38 -04:00
|
|
|
|
|
|
|
return cls(user_id, access_token, ip, user_agent, device_id, last_seen)
|
2017-06-27 09:58:10 -04:00
|
|
|
|
2022-02-08 11:03:08 -05:00
|
|
|
def to_line(self) -> str:
|
2018-03-29 17:57:28 -04:00
|
|
|
return (
|
|
|
|
self.user_id
|
|
|
|
+ " "
|
2020-08-19 07:26:03 -04:00
|
|
|
+ json_encoder.encode(
|
2019-06-20 05:32:02 -04:00
|
|
|
(
|
2017-06-27 11:25:38 -04:00
|
|
|
self.access_token,
|
|
|
|
self.ip,
|
|
|
|
self.user_agent,
|
|
|
|
self.device_id,
|
|
|
|
self.last_seen,
|
2017-06-27 09:58:10 -04:00
|
|
|
)
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
|
|
|
)
|
2017-06-27 09:58:10 -04:00
|
|
|
|
2022-04-01 08:08:55 -04:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return (
|
|
|
|
f"UserIpCommand({self.user_id!r}, .., {self.ip!r}, "
|
|
|
|
f"{self.user_agent!r}, {self.device_id!r}, {self.last_seen})"
|
|
|
|
)
|
|
|
|
|
2022-05-20 10:28:23 -04:00
|
|
|
def redis_channel_name(self, prefix: str) -> str:
|
|
|
|
return f"{prefix}/USER_IP"
|
|
|
|
|
2017-06-27 09:58:10 -04:00
|
|
|
|
2020-04-07 12:40:22 -04:00
|
|
|
class RemoteServerUpCommand(_SimpleCommand):
|
2020-01-17 05:27:19 -05:00
|
|
|
"""Sent when a worker has detected that a remote server is no longer
|
|
|
|
"down" and retry timings should be reset.
|
|
|
|
|
|
|
|
If sent from a client the server will relay to all other workers.
|
|
|
|
|
|
|
|
Format::
|
|
|
|
|
|
|
|
REMOTE_SERVER_UP <server>
|
|
|
|
"""
|
|
|
|
|
|
|
|
NAME = "REMOTE_SERVER_UP"
|
|
|
|
|
|
|
|
|
2021-07-15 06:02:43 -04:00
|
|
|
_COMMANDS: Tuple[Type[Command], ...] = (
|
2020-01-14 09:08:06 -05:00
|
|
|
ServerCommand,
|
|
|
|
RdataCommand,
|
|
|
|
PositionCommand,
|
|
|
|
ErrorCommand,
|
|
|
|
PingCommand,
|
|
|
|
NameCommand,
|
|
|
|
ReplicateCommand,
|
|
|
|
UserSyncCommand,
|
|
|
|
FederationAckCommand,
|
|
|
|
UserIpCommand,
|
2020-01-17 05:27:19 -05:00
|
|
|
RemoteServerUpCommand,
|
2020-03-30 11:37:24 -04:00
|
|
|
ClearUserSyncsCommand,
|
2021-07-15 06:02:43 -04:00
|
|
|
)
|
2020-01-14 09:08:06 -05:00
|
|
|
|
2017-03-27 10:40:37 -04:00
|
|
|
# Map of command name to command type.
|
2020-01-14 09:08:06 -05:00
|
|
|
COMMAND_MAP = {cmd.NAME: cmd for cmd in _COMMANDS}
|
2017-03-27 10:40:37 -04:00
|
|
|
|
|
|
|
# The commands the server is allowed to send
|
|
|
|
VALID_SERVER_COMMANDS = (
|
|
|
|
ServerCommand.NAME,
|
|
|
|
RdataCommand.NAME,
|
|
|
|
PositionCommand.NAME,
|
|
|
|
ErrorCommand.NAME,
|
|
|
|
PingCommand.NAME,
|
2020-01-17 05:27:19 -05:00
|
|
|
RemoteServerUpCommand.NAME,
|
2017-03-27 10:40:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# The commands the client is allowed to send
|
|
|
|
VALID_CLIENT_COMMANDS = (
|
|
|
|
NameCommand.NAME,
|
|
|
|
ReplicateCommand.NAME,
|
|
|
|
PingCommand.NAME,
|
|
|
|
UserSyncCommand.NAME,
|
2020-03-30 11:37:24 -04:00
|
|
|
ClearUserSyncsCommand.NAME,
|
2017-03-27 10:40:37 -04:00
|
|
|
FederationAckCommand.NAME,
|
2017-06-27 09:58:10 -04:00
|
|
|
UserIpCommand.NAME,
|
2017-03-27 10:40:37 -04:00
|
|
|
ErrorCommand.NAME,
|
2020-01-17 05:27:19 -05:00
|
|
|
RemoteServerUpCommand.NAME,
|
2017-03-27 10:40:37 -04:00
|
|
|
)
|
2020-04-22 08:07:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
def parse_command_from_line(line: str) -> Command:
|
|
|
|
"""Parses a command from a received line.
|
|
|
|
|
|
|
|
Line should already be stripped of whitespace and be checked if blank.
|
|
|
|
"""
|
|
|
|
|
2020-04-22 09:34:31 -04:00
|
|
|
idx = line.find(" ")
|
2020-04-22 08:07:41 -04:00
|
|
|
if idx >= 0:
|
|
|
|
cmd_name = line[:idx]
|
|
|
|
rest_of_line = line[idx + 1 :]
|
|
|
|
else:
|
|
|
|
cmd_name = line
|
|
|
|
rest_of_line = ""
|
|
|
|
|
|
|
|
cmd_cls = COMMAND_MAP[cmd_name]
|
|
|
|
return cmd_cls.from_line(rest_of_line)
|