Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
蜕变的菜鸟
glances
提交
9aa5cbc1
G
glances
项目概览
蜕变的菜鸟
/
glances
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glances
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
9aa5cbc1
编写于
10月 29, 2012
作者:
N
Nicolas Hennion
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add INIT and QUIT. Clean the code. Do stuff...
上级
6acba29b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
117 addition
and
42 deletion
+117
-42
glances/glances.py
glances/glances.py
+117
-42
未找到文件。
glances/glances.py
浏览文件 @
9aa5cbc1
...
...
@@ -1877,12 +1877,23 @@ class glancesScreen:
# Caption
screen_x
=
self
.
screen
.
getmaxyx
()[
1
]
screen_y
=
self
.
screen
.
getmaxyx
()[
0
]
msg
=
_
(
"Press 'h' for help"
)
if
(
screen_y
>
self
.
caption_y
and
screen_x
>
self
.
caption_x
+
32
):
if
(
client_tag
):
msg_client
=
_
(
"Connected to:"
)
+
" "
+
format
(
server_ip
)
msg_help
=
_
(
"Press 'h' for help"
)
if
(
client_tag
):
if
(
screen_y
>
self
.
caption_y
and
screen_x
>
self
.
caption_x
+
len
(
msg_client
)):
self
.
term_window
.
addnstr
(
max
(
self
.
caption_y
,
screen_y
-
1
),
self
.
caption_x
,
msg_client
,
len
(
msg_client
),
self
.
title_color
if
self
.
hascolors
else
curses
.
A_UNDERLINE
)
if
(
screen_x
>
self
.
caption_x
+
len
(
msg_client
)
+
3
+
len
(
msg_help
)):
self
.
term_window
.
addnstr
(
max
(
self
.
caption_y
,
screen_y
-
1
),
self
.
caption_x
+
len
(
msg_client
),
" | "
+
msg_help
,
3
+
len
(
msg_help
))
else
:
self
.
term_window
.
addnstr
(
max
(
self
.
caption_y
,
screen_y
-
1
),
self
.
caption_x
,
msg
,
self
.
default_color
)
self
.
caption_x
,
msg_help
,
len
(
msg_help
)
)
def
displayHelp
(
self
):
"""
Show the help panel
...
...
@@ -2183,24 +2194,108 @@ class glancesCsv:
class
GlancesHandler
(
SocketServer
.
BaseRequestHandler
):
"""
This class manages the TCP server
This class manages the TCP request
Return:
0: GET Command success
1: command unknown
2: INIT Command success
3: QUIT Command success
"""
def
handle
(
self
):
# Get command from the Glances client
self
.
datarecv
=
self
.
request
.
recv
(
1024
)
.
strip
()
self
.
datarecv
=
self
.
request
.
recv
(
1024
)
print
"Receive the command %s from %s"
%
(
format
(
self
.
datarecv
),
format
(
self
.
client_address
[
0
]))
if
(
self
.
datarecv
==
"GET"
):
# Send data to the Glances client
stats
.
update
()
# JSONify and ZIP stats
# To be replaced when version 2.2.0 of the JSON will be available
#~ self.datasend = zlib.compress(json.dumps(stats.getAll(), namedtuple_as_object = True))
self
.
datasend
=
zlib
.
compress
(
json
.
dumps
(
stats
.
getAll
()))
self
.
request
.
sendall
(
self
.
datasend
)
return
0
elif
(
self
.
datarecv
==
"INIT"
):
# Send Glances version to the client
self
.
datasend
=
__version__
self
.
request
.
sendall
(
self
.
datasend
)
return
2
elif
(
self
.
datarecv
==
"QUIT"
):
# Send Glances version to the client
self
.
datasend
=
"BYE"
self
.
request
.
sendall
(
self
.
datasend
)
return
3
else
:
print
"Unknown command received"
return
1
class
GlancesServer
(
SocketServer
.
TCPServer
):
"""
This class creates and manages the TCP server
"""
def
__init__
(
self
,
server_address
,
handler_class
=
GlancesHandler
):
SocketServer
.
TCPServer
.
__init__
(
self
,
server_address
,
handler_class
)
return
def
serve_forever
(
self
):
"""
Main loop for the TCP server
"""
while
True
:
self
.
handle_request
()
return
class
GlancesClient
():
"""
This class creates and manages the TCP client
"""
def
__init__
(
self
,
server_address
,
server_port
=
61209
):
self
.
server_address
=
server_address
self
.
server_port
=
server_port
return
def
__sendandrecv__
(
self
,
command
,
jsonzip
=
True
):
# Create a socket (SOCK_STREAM means a TCP socket)
SocketClient
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
# Connect to server and send data
SocketClient
.
connect
((
self
.
server_address
,
self
.
server_port
))
# Send command
SocketClient
.
sendall
(
command
)
# Receive data from the server and shut down
buff_received
=
''
while
True
:
buff
=
SocketClient
.
recv
(
8192
)
if
not
buff
:
break
buff_received
+=
buff
# Close the socket
SocketClient
.
close
()
# Return the data sent by the server
if
jsonzip
:
# If asked: deJSONify and decompress
return
json
.
loads
(
zlib
.
decompress
(
buff_received
))
else
:
return
buff_received
def
client_init
(
self
):
if
self
.
__sendandrecv__
(
"INIT"
,
jsonzip
=
False
)
!=
__version__
:
return
1
else
:
return
0
def
client_get
(
self
):
return
self
.
__sendandrecv__
(
"GET"
)
def
client_quit
(
self
):
return
self
.
__sendandrecv__
(
"QUIT"
,
jsonzip
=
False
)
# Global def
#===========
...
...
@@ -2355,14 +2450,21 @@ def init():
if
server_tag
:
# Init the server
print
(
_
(
"Start Glances as a server"
))
server
=
SocketServer
.
TCP
Server
((
"0.0.0.0"
,
server_port
),
GlancesHandler
)
server
=
Glances
Server
((
"0.0.0.0"
,
server_port
),
GlancesHandler
)
# Init stats
stats
=
glancesStats
(
server_tag
=
True
)
elif
client_tag
:
# Init the client (displaying server stat in the CLI)
client
=
GlancesClient
(
server_ip
,
server_port
)
if
client
.
client_init
()
!=
0
:
print
(
_
(
"Error: The server version is not compatible"
))
sys
.
exit
(
2
)
# Init Limits
limits
=
glancesLimits
()
...
...
@@ -2407,45 +2509,15 @@ def main():
if
server_tag
:
# Start the server loop
# !!! TODO: Use http://www.doughellmann.com/PyMOTW/SocketServer/
server
.
serve_forever
()
elif
client_tag
:
# Start the client loop
while
True
:
# Create a socket (SOCK_STREAM means a TCP socket)
client
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
# Connect to server and send data
client
.
connect
((
server_ip
,
server_port
))
# Send command
client
.
sendall
(
"GET
\n
"
)
# Receive data from the server and shut down
buff_received
=
''
while
True
:
# !!! TODO: Test with higher buffer size
buff
=
client
.
recv
(
1024
)
if
not
buff
:
break
buff_received
+=
buff
client_stats
=
json
.
loads
(
zlib
.
decompress
(
buff_received
))
# Close
client
.
close
()
# Get system informations
stats
.
update
(
client_stats
)
while
True
:
# Get server system informations
stats
.
update
(
client
.
client_get
())
# Update the screen
screen
.
update
(
stats
)
# Update the HTML output
if
html_tag
:
htmloutput
.
update
(
stats
)
# Update the CSV output
if
csv_tag
:
csvoutput
.
update
(
stats
)
else
:
# Start the classical CLI loop
while
True
:
...
...
@@ -2468,8 +2540,11 @@ def end():
if
server_tag
:
# Stop the server loop
print
(
_
(
"Stop Glances server"
))
server
.
server_close
()
else
:
# !!! TODO: something special to dor with client_tag ?
if
client_tag
:
# Stop the client loop
client
.
client_quit
()
# Stop the classical CLI loop
screen
.
end
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录