Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
nemu3
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
nemu3
Commits
3d4c6a20
Commit
3d4c6a20
authored
Jun 15, 2010
by
Martín Ferrari
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Client class
parent
3f6da7f2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
1 deletion
+88
-1
src/netns/protocol.py
src/netns/protocol.py
+88
-1
No files found.
src/netns/protocol.py
View file @
3d4c6a20
...
...
@@ -12,7 +12,7 @@ try:
except
ImportError
:
from
yaml
import
Loader
,
Dumper
import
base64
,
os
,
passfd
,
sys
,
yaml
import
base64
,
os
,
passfd
,
re
,
sys
,
yaml
import
netns.subprocess
# Protocol definition
...
...
@@ -337,3 +337,90 @@ class Server(object):
# def do_ROUT_ADD(self, cmdname, prefix, prefixlen, nexthop, ifnr):
# def do_ROUT_DEL(self, cmdname, prefix, prefixlen, nexthop, ifnr):
# ================
class
Client
(
object
):
def
__init__
(
self
,
fd
):
if
hasattr
(
fd
,
"readline"
):
self
.
f
=
fd
else
:
if
hasattr
(
fd
,
"makefile"
):
self
.
f
=
fd
.
makefile
(
fd
,
"r+"
,
1
)
# line buffered
else
:
self
.
f
=
os
.
fdopen
(
fd
,
"r+"
,
1
)
def
_send_cmd
(
self
,
*
args
):
s
=
" "
.
join
(
map
(
str
,
args
))
+
"
\
n
"
self
.
f
.
write
(
s
)
def
_read_reply
(
self
):
"""Reads a (possibly multi-line) response from the server. Returns a
tuple containing (code, text)"""
text
=
""
while
True
:
line
=
self
.
f
.
readline
().
rstrip
()
if
not
line
:
raise
RuntimeError
(
"Protocol error, empty line received"
)
m
=
re
.
search
(
r'^(\
d{
3})([ -])(.*)'
,
line
)
if
not
m
:
raise
RuntimeError
(
"Protocol error, read: %s"
%
line
)
status
=
m
.
group
(
1
)
text
+=
m
.
group
(
3
)
if
m
.
group
(
2
)
==
" "
:
break
return
(
int
(
status
),
text
)
def
_read_and_check_reply
(
self
,
expected
=
2
):
"Reads a response and raises an exception if the code is not 2xx."
code
,
text
=
self
.
_read_reply
()
if
code
/
100
!=
expected
:
raise
"Error on command: %d %s"
%
(
code
,
text
)
return
text
def
shutdown
(
self
):
"Tell the client to quit."
self
.
_send_cmd
((
"QUIT"
,
))
self
.
_read_and_check_reply
()
def
_send_fd
(
self
,
type
,
fd
):
self
.
_send_cmd
(
"PROC"
,
type
)
self
.
_read_and_check_reply
(
3
)
passfd
.
sendfd
(
self
.
f
.
fileno
(),
fd
,
"PROC "
+
type
)
self
.
_read_and_check_reply
()
def
popen
(
self
,
uid
,
gid
,
file
,
argv
=
None
,
cwd
=
None
,
env
=
None
,
stdin
=
None
,
stdout
=
None
,
stderr
=
None
):
"Start a subprocess in the slave."
params
=
[
"PROC"
,
"CRTE"
,
uid
,
gid
,
base64
.
b64encode
(
file
)]
if
argv
:
for
i
in
argv
:
params
.
append
(
base64
.
b64encode
(
i
))
self
.
_send_cmd
(
*
params
)
self
.
_read_and_check_reply
()
if
cwd
:
self
.
_send_cmd
(
"PROC"
,
"CWD"
,
base64
.
b64encode
(
cwd
))
self
.
_read_and_check_reply
()
if
env
:
params
=
[]
for
i
in
env
:
params
.
append
(
base64
.
b64encode
(
i
))
self
.
_send_cmd
(
"PROC"
,
"ENV"
,
params
)
self
.
_read_and_check_reply
()
if
stdin
:
self
.
_send_fd
(
"SIN"
,
stdin
)
if
stdout
:
self
.
_send_fd
(
"SOUT"
,
stdout
)
if
stderr
:
self
.
_send_fd
(
"SERR"
,
stderr
)
self
.
_send_cmd
(
"PROC"
,
"RUN"
)
pid
=
self
.
_read_and_check_reply
().
split
()[
0
]
return
pid
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment