mirror of
https://github.com/GNS3/gns3-server.git
synced 2025-01-18 07:23:47 +02:00
Code cleanup around ubridge
This commit is contained in:
parent
dcff51eb75
commit
e4fdfbd091
@ -208,6 +208,7 @@ class Hypervisor(UBridgeHypervisor):
|
|||||||
os.remove(self._stdout_file)
|
os.remove(self._stdout_file)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
log.warning("could not delete temporary uBridge log file: {}".format(e))
|
log.warning("could not delete temporary uBridge log file: {}".format(e))
|
||||||
|
self._process = None
|
||||||
self._started = False
|
self._started = False
|
||||||
|
|
||||||
def read_stdout(self):
|
def read_stdout(self):
|
||||||
|
@ -20,6 +20,7 @@ import time
|
|||||||
import logging
|
import logging
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
from ..utils.asyncio import locked_coroutine
|
||||||
from .ubridge_error import UbridgeError
|
from .ubridge_error import UbridgeError
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@ -48,7 +49,6 @@ class UBridgeHypervisor:
|
|||||||
self._timeout = timeout
|
self._timeout = timeout
|
||||||
self._reader = None
|
self._reader = None
|
||||||
self._writer = None
|
self._writer = None
|
||||||
self._io_lock = asyncio.Lock()
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def connect(self, timeout=10):
|
def connect(self, timeout=10):
|
||||||
@ -176,7 +176,7 @@ class UBridgeHypervisor:
|
|||||||
|
|
||||||
self._host = host
|
self._host = host
|
||||||
|
|
||||||
@asyncio.coroutine
|
@locked_coroutine
|
||||||
def send(self, command):
|
def send(self, command):
|
||||||
"""
|
"""
|
||||||
Sends commands to this hypervisor.
|
Sends commands to this hypervisor.
|
||||||
@ -199,66 +199,65 @@ class UBridgeHypervisor:
|
|||||||
# but still have more data. The only thing we know for sure is the last line
|
# but still have more data. The only thing we know for sure is the last line
|
||||||
# will begin with '100-' or a '2xx-' and end with '\r\n'
|
# will begin with '100-' or a '2xx-' and end with '\r\n'
|
||||||
|
|
||||||
with (yield from self._io_lock):
|
if self._writer is None or self._reader is None:
|
||||||
if self._writer is None or self._reader is None:
|
raise UbridgeError("Not connected")
|
||||||
raise UbridgeError("Not connected")
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
command = command.strip() + '\n'
|
||||||
|
log.debug("sending {}".format(command))
|
||||||
|
self._writer.write(command.encode())
|
||||||
|
yield from self._writer.drain()
|
||||||
|
except OSError as e:
|
||||||
|
raise UbridgeError("Lost communication with {host}:{port} :{error}, Dynamips process running: {run}"
|
||||||
|
.format(host=self._host, port=self._port, error=e, run=self.is_running()))
|
||||||
|
|
||||||
|
# Now retrieve the result
|
||||||
|
data = []
|
||||||
|
buf = ''
|
||||||
|
while True:
|
||||||
try:
|
try:
|
||||||
command = command.strip() + '\n'
|
try:
|
||||||
log.debug("sending {}".format(command))
|
chunk = yield from self._reader.read(1024)
|
||||||
self._writer.write(command.encode())
|
except asyncio.CancelledError:
|
||||||
yield from self._writer.drain()
|
# task has been canceled but continue to read
|
||||||
|
# any remaining data sent by the hypervisor
|
||||||
|
continue
|
||||||
|
if not chunk:
|
||||||
|
raise UbridgeError("No data returned from {host}:{port}, uBridge process running: {run}"
|
||||||
|
.format(host=self._host, port=self._port, run=self.is_running()))
|
||||||
|
buf += chunk.decode("utf-8")
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
raise UbridgeError("Lost communication with {host}:{port} :{error}, Dynamips process running: {run}"
|
raise UbridgeError("Lost communication with {host}:{port} :{error}, uBridge process running: {run}"
|
||||||
.format(host=self._host, port=self._port, error=e, run=self.is_running()))
|
.format(host=self._host, port=self._port, error=e, run=self.is_running()))
|
||||||
|
|
||||||
# Now retrieve the result
|
# If the buffer doesn't end in '\n' then we can't be done
|
||||||
data = []
|
try:
|
||||||
|
if buf[-1] != '\n':
|
||||||
|
continue
|
||||||
|
except IndexError:
|
||||||
|
raise UbridgeError("Could not communicate with {host}:{port}, uBridge process running: {run}"
|
||||||
|
.format(host=self._host, port=self._port, run=self.is_running()))
|
||||||
|
|
||||||
|
data += buf.split('\r\n')
|
||||||
|
if data[-1] == '':
|
||||||
|
data.pop()
|
||||||
buf = ''
|
buf = ''
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
try:
|
|
||||||
chunk = yield from self._reader.read(1024)
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
# task has been canceled but continue to read
|
|
||||||
# any remaining data sent by the hypervisor
|
|
||||||
continue
|
|
||||||
if not chunk:
|
|
||||||
raise UbridgeError("No data returned from {host}:{port}, uBridge process running: {run}"
|
|
||||||
.format(host=self._host, port=self._port, run=self.is_running()))
|
|
||||||
buf += chunk.decode("utf-8")
|
|
||||||
except OSError as e:
|
|
||||||
raise UbridgeError("Lost communication with {host}:{port} :{error}, uBridge process running: {run}"
|
|
||||||
.format(host=self._host, port=self._port, error=e, run=self.is_running()))
|
|
||||||
|
|
||||||
# If the buffer doesn't end in '\n' then we can't be done
|
# Does it contain an error code?
|
||||||
try:
|
if self.error_re.search(data[-1]):
|
||||||
if buf[-1] != '\n':
|
raise UbridgeError(data[-1][4:])
|
||||||
continue
|
|
||||||
except IndexError:
|
|
||||||
raise UbridgeError("Could not communicate with {host}:{port}, uBridge process running: {run}"
|
|
||||||
.format(host=self._host, port=self._port, run=self.is_running()))
|
|
||||||
|
|
||||||
data += buf.split('\r\n')
|
# Or does the last line begin with '100-'? Then we are done!
|
||||||
if data[-1] == '':
|
if data[-1][:4] == '100-':
|
||||||
|
data[-1] = data[-1][4:]
|
||||||
|
if data[-1] == 'OK':
|
||||||
data.pop()
|
data.pop()
|
||||||
buf = ''
|
break
|
||||||
|
|
||||||
# Does it contain an error code?
|
# Remove success responses codes
|
||||||
if self.error_re.search(data[-1]):
|
for index in range(len(data)):
|
||||||
raise UbridgeError(data[-1][4:])
|
if self.success_re.search(data[index]):
|
||||||
|
data[index] = data[index][4:]
|
||||||
|
|
||||||
# Or does the last line begin with '100-'? Then we are done!
|
log.debug("returned result {}".format(data))
|
||||||
if data[-1][:4] == '100-':
|
return data
|
||||||
data[-1] = data[-1][4:]
|
|
||||||
if data[-1] == 'OK':
|
|
||||||
data.pop()
|
|
||||||
break
|
|
||||||
|
|
||||||
# Remove success responses codes
|
|
||||||
for index in range(len(data)):
|
|
||||||
if self.success_re.search(data[index]):
|
|
||||||
data[index] = data[index][4:]
|
|
||||||
|
|
||||||
log.debug("returned result {}".format(data))
|
|
||||||
return data
|
|
||||||
|
Loading…
Reference in New Issue
Block a user