Skip to content
master.py 2.2 KiB
Newer Older
#  Copyright 2019  Dom Sekotill <dom.sekotill@kodo.org.uk>
#
#  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.

"""
Master control client class
"""

import os
import pathlib
from enum import auto
from typing import Set

from .. import util
from .base import BaseClient
from .interfaces import InterfaceClient


class MasterCommands(util.AutoName):
	"""
	Commands for master control clients
	"""

	INTERFACES = auto()
	INTERFACE_ADD = auto()


class MasterClient(BaseClient):
	"""
	A client for listing, adding and removing interfaces, and getting per-client interfaces
	"""

	ctrl_dir = None

	async def connect(self, path: os.PathLike):
		if not isinstance(path, pathlib.Path):
			path = pathlib.Path(path)
		await super().connect(path)
		self.ctrl_dir = path.parent

	async def managed_interfaces(self) -> Set:
		"""
		Return a set of the interfaces currently managed by the daemon
		"""
		return await self.send_message(
			MasterCommands.INTERFACES.value,
			convert=lambda x: x.splitlines(),
		)

	async def add_interface(self, ifname: str, driver: str = '', driver_param: str = ''):
		"""
		Add a network interface to the daemon's control interfaces
		"""
		await self.send_message(
			MasterCommands.INTERFACE_ADD.value,
			ifname, '', driver, self.ctrl_dir.as_posix(), driver_param,
		)

	async def connect_interface(self, ifname: str) -> InterfaceClient:
		"""
		Return an InterfaceClient controlling the named interface

		If the daemon is not currently managing the interface, it is added to the daemon's 
		interfaces.
		"""
		if ifname not in await self.managed_interfaces():
			await self.add_interface(ifname)
		client = InterfaceClient(logger=self.logger)
		await client.connect(self.ctrl_dir.joinpath(ifname).as_posix())
		return client