Source code for qstone.connectors.connection
"""Connection abstraction for nodes."""
import os
from abc import ABC, abstractmethod
from pathlib import Path
[docs]
class FileLock:
"""Implementation of the file locking system."""
def __init__(self, lockfile: str):
"""Lock creation"""
self._lockfile = lockfile
[docs]
def acquire_lock(self) -> bool:
"""Tries to acquire the lock. Atomic way"""
if self._lockfile is None:
locked = True
else:
try:
# Atomic operation
Path(self._lockfile).touch(exist_ok=False)
locked = True
except FileExistsError:
locked = False
return locked
[docs]
def release_lock(self):
"""Releases the lock."""
if self._lockfile:
os.remove(self._lockfile)
[docs]
class Connection(ABC):
"""Abstract class to represent connection between nodes"""
[docs]
@abstractmethod
def preprocess(self, qasm_ptr: str) -> str:
"""Preprocess the data."""
[docs]
@abstractmethod
def postprocess(self, message: str) -> dict:
"""Postprocess the data"""
[docs]
@abstractmethod
def run(
self, qasm_ptr: str, reps: int, host: str, server_port: int, lockfile: str
) -> dict:
"""Run the connection to the server"""