Working protobuf passing.
This is barely working, really. In fact, I have an "unused" field in my enum because if I don't prowide it the protobuf serializes down to an empty buffer which breaks all kinds of things.
This commit is contained in:
parent
461fc7ce0b
commit
aadc8520bb
6 changed files with 179 additions and 15 deletions
|
@ -1,12 +1,48 @@
|
|||
import enum
|
||||
import logging
|
||||
import socket
|
||||
from typing import Tuple
|
||||
import urllib.parse
|
||||
|
||||
from proto import control_pb2
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# Uniquely identifies a datajack connection.
|
||||
# This indicates the handshake style expected and is used
|
||||
# to quickly and easily determine if the client is even speaking the
|
||||
# right protocol for the server.
|
||||
MAGIC = "e8437140-4347-48cc-a31d-dcdc944ffc15"
|
||||
|
||||
# The list of formats supported by this client in descending order of preference
|
||||
# This is used for negotiation of the data format that will be used with the server.
|
||||
DATA_FORMATS = ",".join([
|
||||
"protobuf",
|
||||
"json",
|
||||
])
|
||||
|
||||
# The types of access that are allowed with this connection
|
||||
class Permissions(enum.Enum):
|
||||
pass
|
||||
|
||||
def _parse_netloc(netloc) -> Tuple[str, str, str, int]:
|
||||
app, _, connection = netloc.partition("@")
|
||||
appname, _, version = app.partition(":")
|
||||
host, _, part = connection.partition(":")
|
||||
return appname, version, host, part
|
||||
|
||||
class Connection:
|
||||
def __init__(self, uri):
|
||||
parts = urllib.parse.urlparse(uri)
|
||||
print(parts)
|
||||
netloc = parts.netloc
|
||||
self.host, _, self.port = netloc.partition(":")
|
||||
|
||||
assert parts.scheme == "socket"
|
||||
self.app_name, self.app_version, self.host, self.port = _parse_netloc(netloc)
|
||||
self.namespace = parts.path.lstrip("/")
|
||||
self.public_key = "pretend_key"
|
||||
self.data_format = None
|
||||
self.address = None
|
||||
self.server_key = None
|
||||
|
||||
def __enter__(self):
|
||||
self.connect()
|
||||
|
@ -18,6 +54,7 @@ class Connection:
|
|||
def connect(self):
|
||||
self.socket = socket.socket()
|
||||
self.socket.connect((self.host, int(self.port)))
|
||||
self._handshake()
|
||||
|
||||
def disconnect(self):
|
||||
pass
|
||||
|
@ -25,5 +62,23 @@ class Connection:
|
|||
def send(self, data):
|
||||
self.socket.send(data)
|
||||
|
||||
def _handshake(self):
|
||||
"Handshake with the server, ensure we have all the data we need."
|
||||
fields = [MAGIC, DATA_FORMATS, self.namespace, self.app_name, self.app_version, self.public_key,]
|
||||
cliend_hand = " ".join(fields)
|
||||
self.socket.send(cliend_hand.encode("UTF-8"))
|
||||
server_hand = self.socket.recv(1024)
|
||||
if not server_hand:
|
||||
print("Failed to get server hand")
|
||||
self.data_format, self.address, self.server_key = server_hand.decode("UTF-8").split(" ")
|
||||
LOGGER.info("Data format: %s", self.data_format)
|
||||
command = control_pb2.Command(
|
||||
type=control_pb2.Command.Type.STATUS
|
||||
)
|
||||
to_send = command.SerializeToString()
|
||||
LOGGER.info("Sending '%s'", to_send)
|
||||
self.socket.send(to_send)
|
||||
|
||||
|
||||
def connection(uri) -> Connection:
|
||||
return Connection(uri)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue