Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
agent
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
7
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ba6ba173
authored
Feb 07, 2024
by
Szeberényi Imre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reconnect fix
parent
145e2a6d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
19 deletions
+32
-19
agent.py
+26
-17
linux/posixvirtio.py
+6
-2
No files found.
agent.py
View file @
ba6ba173
...
@@ -42,22 +42,27 @@ logger.setLevel(level)
...
@@ -42,22 +42,27 @@ logger.setLevel(level)
class
SerialLineReceiver
(
SerialLineReceiverBase
):
class
SerialLineReceiver
(
SerialLineReceiverBase
):
def
__init__
(
self
):
super
(
SerialLineReceiver
,
self
)
.
__init__
()
self
.
tickId
=
LoopingCall
(
self
.
tick
)
reactor
.
addSystemEventTrigger
(
"before"
,
"shutdown"
,
self
.
shutdown
)
self
.
running
=
True
def
connectionMade
(
self
):
def
connectionMade
(
self
):
logger
.
debug
(
"connectionMade"
)
self
.
tickId
.
start
(
5
,
now
=
False
)
self
.
transport
.
dataBuffer
=
b
""
self
.
transport
.
_tempDataBuffer
=
[]
# will be added to dataBuffer in doWrite
self
.
transport
.
_tempDataLen
=
0
self
.
transport
.
write
(
'
\r\n
'
)
self
.
transport
.
write
(
'
\r\n
'
)
self
.
send_command
(
if
self
.
running
:
command
=
'agent_started'
,
self
.
send_command
(
args
=
{
'version'
:
Context
.
get_agent_version
(),
command
=
'agent_started'
,
'system'
:
system
})
args
=
{
'version'
:
Context
.
get_agent_version
(),
'system'
:
system
})
def
shutdown
():
self
.
connectionLost2
(
'shutdown'
)
d
=
defer
.
Deferred
()
reactor
.
callLater
(
0.3
,
d
.
callback
,
"1"
)
return
d
reactor
.
addSystemEventTrigger
(
"before"
,
"shutdown"
,
shutdown
)
def
connectionLost
(
self
,
reason
):
def
connectionLost
(
self
,
reason
):
reactor
.
stop
()
logger
.
debug
(
"connectionLost"
)
self
.
tickId
.
stop
()
def
connectionLost2
(
self
,
reason
):
def
connectionLost2
(
self
,
reason
):
self
.
send_command
(
command
=
'agent_stopped'
,
self
.
send_command
(
command
=
'agent_stopped'
,
...
@@ -70,10 +75,13 @@ class SerialLineReceiver(SerialLineReceiverBase):
...
@@ -70,10 +75,13 @@ class SerialLineReceiver(SerialLineReceiverBase):
except
Exception
:
except
Exception
:
logger
.
exception
(
"Twisted hide exception"
)
logger
.
exception
(
"Twisted hide exception"
)
def
__init__
(
self
):
def
shutdown
(
self
):
super
(
SerialLineReceiver
,
self
)
.
__init__
()
self
.
running
=
False
self
.
lc
=
LoopingCall
(
self
.
tick
)
logger
.
debug
(
"shutdown"
)
self
.
lc
.
start
(
5
,
now
=
False
)
self
.
connectionLost2
(
'shutdown'
)
d
=
defer
.
Deferred
()
reactor
.
callLater
(
0.3
,
d
.
callback
,
"1"
)
return
d
def
send_status
(
self
):
def
send_status
(
self
):
import
psutil
import
psutil
...
@@ -113,6 +121,7 @@ class SerialLineReceiver(SerialLineReceiverBase):
...
@@ -113,6 +121,7 @@ class SerialLineReceiver(SerialLineReceiverBase):
self
.
_pretty_fun
(
func
),
", "
.
join
(
missing_kwargs
)))
self
.
_pretty_fun
(
func
),
", "
.
join
(
missing_kwargs
)))
def
_get_command
(
self
,
command
,
args
):
def
_get_command
(
self
,
command
,
args
):
logger
.
debug
(
"_get_command
%
s
%
s"
%
(
command
,
args
))
if
not
isinstance
(
command
,
basestring
)
or
command
.
startswith
(
'_'
):
if
not
isinstance
(
command
,
basestring
)
or
command
.
startswith
(
'_'
):
raise
AttributeError
(
u'Invalid command:
%
s'
%
command
)
raise
AttributeError
(
u'Invalid command:
%
s'
%
command
)
try
:
try
:
...
@@ -142,6 +151,7 @@ class SerialLineReceiver(SerialLineReceiverBase):
...
@@ -142,6 +151,7 @@ class SerialLineReceiver(SerialLineReceiverBase):
return
"<
%
s>"
%
type
(
fun
)
.
__name__
return
"<
%
s>"
%
type
(
fun
)
.
__name__
def
handle_command
(
self
,
command
,
args
):
def
handle_command
(
self
,
command
,
args
):
logger
.
debug
(
"handle_command
%
s
%
s"
%
(
command
,
args
))
func
=
self
.
_get_command
(
command
,
args
)
func
=
self
.
_get_command
(
command
,
args
)
retval
=
func
(
**
args
)
retval
=
func
(
**
args
)
self
.
send_response
(
self
.
send_response
(
...
@@ -151,7 +161,6 @@ class SerialLineReceiver(SerialLineReceiverBase):
...
@@ -151,7 +161,6 @@ class SerialLineReceiver(SerialLineReceiverBase):
def
handle_response
(
self
,
response
,
args
):
def
handle_response
(
self
,
response
,
args
):
pass
pass
def
main
():
def
main
():
# Get proper serial class and port name
# Get proper serial class and port name
(
serial
,
port
)
=
get_serial
()
(
serial
,
port
)
=
get_serial
()
...
...
linux/posixvirtio.py
View file @
ba6ba173
...
@@ -34,6 +34,7 @@ class SerialPort(abstract.FileDescriptor):
...
@@ -34,6 +34,7 @@ class SerialPort(abstract.FileDescriptor):
self
.
port
,
os
.
O_RDWR
|
os
.
O_NOCTTY
|
os
.
O_NONBLOCK
)
self
.
port
,
os
.
O_RDWR
|
os
.
O_NOCTTY
|
os
.
O_NONBLOCK
)
self
.
reactor
=
reactor
self
.
reactor
=
reactor
self
.
protocol
=
protocol
self
.
protocol
=
protocol
self
.
connected
=
1
self
.
protocol
.
makeConnection
(
self
)
self
.
protocol
.
makeConnection
(
self
)
self
.
startReading
()
self
.
startReading
()
...
@@ -60,9 +61,12 @@ class SerialPort(abstract.FileDescriptor):
...
@@ -60,9 +61,12 @@ class SerialPort(abstract.FileDescriptor):
serial data.
serial data.
"""
"""
abstract
.
FileDescriptor
.
connectionLost
(
self
,
reason
)
abstract
.
FileDescriptor
.
connectionLost
(
self
,
reason
)
self
.
protocol
.
connectionLost
(
reason
)
os
.
close
(
self
.
_serial
)
os
.
close
(
self
.
_serial
)
sleep
(
2
)
logger
.
debug
(
"Reconecting after 5s"
)
sleep
(
5
)
self
.
_serial
=
os
.
open
(
self
.
_serial
=
os
.
open
(
self
.
port
,
os
.
O_RDWR
|
os
.
O_NOCTTY
|
os
.
O_NONBLOCK
)
self
.
port
,
os
.
O_RDWR
|
os
.
O_NOCTTY
|
os
.
O_NONBLOCK
)
self
.
connected
=
1
self
.
protocol
.
makeConnection
(
self
)
self
.
startReading
()
self
.
startReading
()
logger
.
info
(
"Reconnecting"
)
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