2022-01-21 23:08:29 +00:00
|
|
|
"""
|
2022-12-05 17:58:23 +00:00
|
|
|
Module for sending and receiving data over a socket connection with the PiSCSI backend
|
2022-01-21 23:08:29 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
from flask import abort
|
|
|
|
from flask_babel import _
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
from piscsi.exceptions import (
|
2022-11-30 05:19:17 +00:00
|
|
|
EmptySocketChunkException,
|
|
|
|
InvalidProtobufResponse,
|
|
|
|
FailedSocketConnectionException,
|
|
|
|
)
|
2022-12-05 17:58:23 +00:00
|
|
|
from piscsi.socket_cmds import SocketCmds
|
2022-01-21 23:08:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SocketCmdsFlask(SocketCmds):
|
|
|
|
"""
|
2022-12-05 17:58:23 +00:00
|
|
|
Class for sending and receiving data over a socket connection with the PiSCSI backend
|
2022-01-21 23:08:29 +00:00
|
|
|
"""
|
2022-11-30 05:19:17 +00:00
|
|
|
|
2022-01-21 23:08:29 +00:00
|
|
|
# pylint: disable=useless-super-delegation
|
|
|
|
def __init__(self, host="localhost", port=6868):
|
|
|
|
super().__init__(host, port)
|
|
|
|
|
|
|
|
def send_pb_command(self, payload):
|
|
|
|
"""
|
|
|
|
Takes a (str) containing a serialized protobuf as argument.
|
2022-12-05 17:58:23 +00:00
|
|
|
Establishes a socket connection with PiSCSI.
|
2022-01-21 23:08:29 +00:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return super().send_pb_command(payload)
|
|
|
|
except FailedSocketConnectionException as err:
|
|
|
|
# After failing all attempts, throw a 404 error
|
2022-11-30 05:19:17 +00:00
|
|
|
abort(
|
|
|
|
404,
|
|
|
|
_(
|
2022-12-05 17:58:23 +00:00
|
|
|
"The PiSCSI Web Interface failed to connect to PiSCSI at "
|
|
|
|
"%(host)s:%(port)s with error: %(error_msg)s. The PiSCSI "
|
2022-11-30 05:19:17 +00:00
|
|
|
"process is not running or may have crashed.",
|
|
|
|
host=self.host,
|
|
|
|
port=self.port,
|
|
|
|
error_msg=str(err),
|
|
|
|
),
|
2022-01-21 23:08:29 +00:00
|
|
|
)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def send_over_socket(self, sock, payload):
|
|
|
|
"""Sends a payload over a given socket"""
|
|
|
|
try:
|
|
|
|
return super().send_over_socket(sock, payload)
|
|
|
|
except EmptySocketChunkException:
|
|
|
|
abort(
|
2022-11-30 05:19:17 +00:00
|
|
|
503,
|
|
|
|
_(
|
2022-12-05 17:58:23 +00:00
|
|
|
"The PiSCSI Web Interface lost connection to PiSCSI. "
|
2022-01-21 23:08:29 +00:00
|
|
|
"Please go back and try again. "
|
|
|
|
"If the issue persists, please report a bug."
|
2022-11-30 05:19:17 +00:00
|
|
|
),
|
2022-01-21 23:08:29 +00:00
|
|
|
)
|
|
|
|
return None
|
|
|
|
except InvalidProtobufResponse:
|
|
|
|
abort(
|
2022-11-30 05:19:17 +00:00
|
|
|
500,
|
|
|
|
_(
|
2022-12-05 17:58:23 +00:00
|
|
|
"The PiSCSI Web Interface did not get a valid response from PiSCSI. "
|
2022-01-21 23:08:29 +00:00
|
|
|
"Please go back and try again. "
|
|
|
|
"If the issue persists, please report a bug."
|
2022-11-30 05:19:17 +00:00
|
|
|
),
|
2022-01-21 23:08:29 +00:00
|
|
|
)
|
|
|
|
return None
|