2016-11-07 12:16:51 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
2022-01-19 13:58:36 +02:00
|
|
|
from gns3server.utils.asyncio import wait_for_file_creation
|
2016-11-07 12:16:51 +02:00
|
|
|
from gns3server.compute.error import NodeError
|
|
|
|
|
|
|
|
"""
|
2022-01-19 13:58:36 +02:00
|
|
|
This module handle connection to unix socket
|
2016-11-07 12:16:51 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class SerialReaderWriterProtocol(asyncio.Protocol):
|
|
|
|
def __init__(self):
|
|
|
|
self._output = asyncio.StreamReader()
|
2017-02-15 20:31:38 +02:00
|
|
|
self._closed = False
|
2016-11-07 12:16:51 +02:00
|
|
|
self.transport = None
|
|
|
|
|
|
|
|
def read(self, n=-1):
|
|
|
|
return self._output.read(n=n)
|
|
|
|
|
|
|
|
def at_eof(self):
|
|
|
|
return self._output.at_eof()
|
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
if self.transport:
|
|
|
|
self.transport.write(data)
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def drain(self):
|
2016-11-07 12:16:51 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
def connection_made(self, transport):
|
|
|
|
self.transport = transport
|
|
|
|
|
|
|
|
def data_received(self, data):
|
2017-02-15 20:31:38 +02:00
|
|
|
if not self._closed:
|
|
|
|
self._output.feed_data(data)
|
2016-11-07 12:16:51 +02:00
|
|
|
|
2016-11-18 12:27:50 +02:00
|
|
|
def close(self):
|
2017-02-15 20:31:38 +02:00
|
|
|
self._closed = True
|
2016-11-18 12:27:50 +02:00
|
|
|
self._output.feed_eof()
|
|
|
|
|
2016-11-07 12:16:51 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def _asyncio_open_serial_unix(path):
|
2016-11-07 12:16:51 +02:00
|
|
|
"""
|
|
|
|
Open a unix socket or a windows named pipe
|
|
|
|
|
|
|
|
:returns: An IO like object
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
# wait for VM to create the pipe file.
|
2018-10-15 13:05:49 +03:00
|
|
|
await wait_for_file_creation(path)
|
2016-11-07 12:16:51 +02:00
|
|
|
except asyncio.TimeoutError:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise NodeError(f'Pipe file "{path}" is missing')
|
2016-11-07 12:16:51 +02:00
|
|
|
|
|
|
|
output = SerialReaderWriterProtocol()
|
2017-02-06 18:59:00 +02:00
|
|
|
try:
|
2018-10-15 13:05:49 +03:00
|
|
|
await asyncio.get_event_loop().create_unix_connection(lambda: output, path)
|
2017-02-06 18:59:00 +02:00
|
|
|
except ConnectionRefusedError:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise NodeError(f'Can\'t open pipe file "{path}"')
|
2016-11-07 12:16:51 +02:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def asyncio_open_serial(path):
|
2016-11-07 12:16:51 +02:00
|
|
|
"""
|
2022-01-19 13:58:36 +02:00
|
|
|
Open an unix socket
|
2016-11-07 12:16:51 +02:00
|
|
|
|
|
|
|
:returns: An IO like object
|
|
|
|
"""
|
|
|
|
|
2022-01-19 13:58:36 +02:00
|
|
|
return await _asyncio_open_serial_unix(path)
|