1717The VALID_SERVER_COMMANDS and VALID_CLIENT_COMMANDS define which commands are
1818allowed to be sent by which side.
1919"""
20-
20+ import abc
2121import logging
2222import platform
2323from typing import Tuple , Type
3434logger = logging .getLogger (__name__ )
3535
3636
37- class Command (object ):
37+ class Command (metaclass = abc . ABCMeta ):
3838 """The base command class.
3939
4040 All subclasses must set the NAME variable which equates to the name of the
4141 command on the wire.
4242
4343 A full command line on the wire is constructed from `NAME + " " + to_line()`
44-
45- The default implementation creates a command of form `<NAME> <data>`
4644 """
4745
4846 NAME = None # type: str
4947
50- def __init__ (self , data ):
51- self .data = data
52-
5348 @classmethod
49+ @abc .abstractmethod
5450 def from_line (cls , line ):
5551 """Deserialises a line from the wire into this command. `line` does not
5652 include the command.
5753 """
58- return cls (line )
5954
60- def to_line (self ):
55+ @abc .abstractmethod
56+ def to_line (self ) -> str :
6157 """Serialises the comamnd for the wire. Does not include the command
6258 prefix.
6359 """
64- return self .data
6560
6661 def get_logcontext_id (self ):
6762 """Get a suitable string for the logcontext when processing this command"""
@@ -70,7 +65,21 @@ def get_logcontext_id(self):
7065 return self .NAME
7166
7267
73- class ServerCommand (Command ):
68+ class _SimpleCommand (Command ):
69+ """An implementation of Command whose argument is just a 'data' string."""
70+
71+ def __init__ (self , data ):
72+ self .data = data
73+
74+ @classmethod
75+ def from_line (cls , line ):
76+ return cls (line )
77+
78+ def to_line (self ) -> str :
79+ return self .data
80+
81+
82+ class ServerCommand (_SimpleCommand ):
7483 """Sent by the server on new connection and includes the server_name.
7584
7685 Format::
@@ -155,22 +164,22 @@ def to_line(self):
155164 return " " .join ((self .stream_name , str (self .token )))
156165
157166
158- class ErrorCommand (Command ):
167+ class ErrorCommand (_SimpleCommand ):
159168 """Sent by either side if there was an ERROR. The data is a string describing
160169 the error.
161170 """
162171
163172 NAME = "ERROR"
164173
165174
166- class PingCommand (Command ):
175+ class PingCommand (_SimpleCommand ):
167176 """Sent by either side as a keep alive. The data is arbitary (often timestamp)
168177 """
169178
170179 NAME = "PING"
171180
172181
173- class NameCommand (Command ):
182+ class NameCommand (_SimpleCommand ):
174183 """Sent by client to inform the server of the client's identity. The data
175184 is the name
176185 """
@@ -387,7 +396,7 @@ def to_line(self):
387396 )
388397
389398
390- class RemoteServerUpCommand (Command ):
399+ class RemoteServerUpCommand (_SimpleCommand ):
391400 """Sent when a worker has detected that a remote server is no longer
392401 "down" and retry timings should be reset.
393402
0 commit comments