2016-11-07 10:16:51 +00: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 sys
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from gns3server.utils.asyncio import wait_for_file_creation, wait_for_named_pipe_creation
|
|
|
|
from gns3server.compute.error import NodeError
|
|
|
|
|
|
|
|
"""
|
|
|
|
This module handle connection to unix socket or Windows named pipe
|
|
|
|
"""
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
import win32file
|
|
|
|
import win32pipe
|
|
|
|
import msvcrt
|
|
|
|
|
|
|
|
|
|
|
|
class SerialReaderWriterProtocol(asyncio.Protocol):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._output = asyncio.StreamReader()
|
2017-02-15 18:31:38 +00:00
|
|
|
self._closed = False
|
2016-11-07 10:16:51 +00: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 10:05:49 +00:00
|
|
|
async def drain(self):
|
2016-11-07 10:16:51 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def connection_made(self, transport):
|
|
|
|
self.transport = transport
|
|
|
|
|
|
|
|
def data_received(self, data):
|
2017-02-15 18:31:38 +00:00
|
|
|
if not self._closed:
|
|
|
|
self._output.feed_data(data)
|
2016-11-07 10:16:51 +00:00
|
|
|
|
2016-11-18 10:27:50 +00:00
|
|
|
def close(self):
|
2017-02-15 18:31:38 +00:00
|
|
|
self._closed = True
|
2016-11-18 10:27:50 +00:00
|
|
|
self._output.feed_eof()
|
|
|
|
|
2016-11-07 10:16:51 +00:00
|
|
|
|
|
|
|
class WindowsPipe:
|
|
|
|
"""
|
|
|
|
Write input and output stream to the same object
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, path):
|
|
|
|
self._handle = open(path, "a+b")
|
|
|
|
self._pipe = msvcrt.get_osfhandle(self._handle.fileno())
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def read(self, n=-1):
|
2016-11-07 10:16:51 +00:00
|
|
|
(read, num_avail, num_message) = win32pipe.PeekNamedPipe(self._pipe, 0)
|
|
|
|
if num_avail > 0:
|
|
|
|
(error_code, output) = win32file.ReadFile(self._pipe, num_avail, None)
|
|
|
|
return output
|
2018-10-15 10:05:49 +00:00
|
|
|
await asyncio.sleep(0.01)
|
2016-11-07 10:16:51 +00:00
|
|
|
return b""
|
|
|
|
|
|
|
|
def at_eof(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
win32file.WriteFile(self._pipe, data)
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def drain(self):
|
2016-11-07 10:16:51 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def _asyncio_open_serial_windows(path):
|
2016-11-07 10:16:51 +00:00
|
|
|
"""
|
|
|
|
Open a windows named pipe
|
|
|
|
|
|
|
|
:returns: An IO like object
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
await wait_for_named_pipe_creation(path)
|
2016-11-07 10:16:51 +00:00
|
|
|
except asyncio.TimeoutError:
|
|
|
|
raise NodeError('Pipe file "{}" is missing'.format(path))
|
|
|
|
return WindowsPipe(path)
|
|
|
|
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def _asyncio_open_serial_unix(path):
|
2016-11-07 10:16:51 +00: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 10:05:49 +00:00
|
|
|
await wait_for_file_creation(path)
|
2016-11-07 10:16:51 +00:00
|
|
|
except asyncio.TimeoutError:
|
|
|
|
raise NodeError('Pipe file "{}" is missing'.format(path))
|
|
|
|
|
|
|
|
output = SerialReaderWriterProtocol()
|
2017-02-06 16:59:00 +00:00
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
await asyncio.get_event_loop().create_unix_connection(lambda: output, path)
|
2017-02-06 16:59:00 +00:00
|
|
|
except ConnectionRefusedError:
|
|
|
|
raise NodeError('Can\'t open pipe file "{}"'.format(path))
|
2016-11-07 10:16:51 +00:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def asyncio_open_serial(path):
|
2016-11-07 10:16:51 +00:00
|
|
|
"""
|
|
|
|
Open a unix socket or a windows named pipe
|
|
|
|
|
|
|
|
:returns: An IO like object
|
|
|
|
"""
|
|
|
|
|
|
|
|
if sys.platform.startswith("win"):
|
2018-10-15 10:05:49 +00:00
|
|
|
return (await _asyncio_open_serial_windows(path))
|
2016-11-07 10:16:51 +00:00
|
|
|
else:
|
2018-10-15 10:05:49 +00:00
|
|
|
return (await _asyncio_open_serial_unix(path))
|