Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hidra
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
steinbac
hidra
Commits
9ba0529e
Commit
9ba0529e
authored
9 years ago
by
Manuela Kuhn
Browse files
Options
Downloads
Patches
Plain Diff
Added communication with live viewer infrastructure to receiver
parent
7a03e9d7
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ZeroMQTunnel/receiver.py
+87
-73
87 additions, 73 deletions
ZeroMQTunnel/receiver.py
with
87 additions
and
73 deletions
ZeroMQTunnel/receiver.py
+
87
−
73
View file @
9ba0529e
...
@@ -12,6 +12,7 @@ import errno
...
@@ -12,6 +12,7 @@ import errno
import
os
import
os
import
traceback
import
traceback
from
stat
import
S_ISREG
,
ST_MTIME
,
ST_MODE
from
stat
import
S_ISREG
,
ST_MTIME
,
ST_MODE
import
threading
...
@@ -25,13 +26,13 @@ class FileReceiver:
...
@@ -25,13 +26,13 @@ class FileReceiver:
maxRingBufferSize
=
100
maxRingBufferSize
=
100
timeToWaitForRingBuffer
=
2
timeToWaitForRingBuffer
=
2
ringBuffer
=
[]
ringBuffer
=
[]
alubla
ZmqContext
=
None
liveViewer
ZmqContext
=
None
def
__init__
(
self
,
outputDir
,
bindingPortForDataStream
,
zmqFileserverIp
,
bindingPortForLiveViewer
,
zmqLiveViewerI
P
):
def
__init__
(
self
,
outputDir
,
bindingPortForDataStream
,
zmqFileserverIp
,
bindingPortForLiveViewer
,
zmqLiveViewerI
p
):
self
.
outputDir
=
outputDir
self
.
outputDir
=
outputDir
self
.
bindingPortForDataStream
=
bindingPortForDataStream
self
.
bindingPortForDataStream
=
bindingPortForDataStream
self
.
zmqFileserverIp
=
zmqFileserverIp
self
.
zmqFileserverIp
=
zmqFileserverIp
self
.
z
q
mLiveViewerIp
=
z
q
mLiveViewerIp
self
.
zm
q
LiveViewerIp
=
zm
q
LiveViewerIp
self
.
bindingPortForLiveViewer
=
bindingPortForLiveViewer
self
.
bindingPortForLiveViewer
=
bindingPortForLiveViewer
# initialize ring buffer
# initialize ring buffer
...
@@ -53,9 +54,28 @@ class FileReceiver:
...
@@ -53,9 +54,28 @@ class FileReceiver:
global
globalZmqContext
global
globalZmqContext
self
.
globalZmqContext
=
zmq
.
Context
()
self
.
globalZmqContext
=
zmq
.
Context
()
# thread to communicate with live viewer
#create socket for live viewer
try
:
logging
.
info
(
"
creating socket for communication with live viewer...
"
)
zmqContext
=
self
.
getZmqContext
()
zmqLiveViewerSocket
=
self
.
createSocketForLiveViewer
(
zmqContext
)
logging
.
info
(
"
creating socket for communication with live viewer...done.
"
)
except
Exception
,
e
:
errorMessage
=
"
Unable to create zeromq context.
"
logging
.
error
(
errorMessage
)
logging
.
debug
(
"
Error was:
"
+
str
(
e
))
logging
.
info
(
"
creating socket for communication with live viewer...failed.
"
)
raise
Exception
(
e
)
t1
=
threading
.
Thread
(
target
=
self
.
sendFileToLiveViewer
,
args
=
(
zmqLiveViewerSocket
,))
t1
.
start
()
try
:
try
:
logging
.
info
(
"
Start receiving new files
"
)
logging
.
info
(
"
Start receiving new files
"
)
self
.
startReceiving
()
self
.
startReceiving
(
zmqLiveViewerSocket
)
logging
.
info
(
"
Stopped receiving.
"
)
logging
.
info
(
"
Stopped receiving.
"
)
except
Exception
,
e
:
except
Exception
,
e
:
logging
.
error
(
"
Unknown error while receiving files. Need to abort.
"
)
logging
.
error
(
"
Unknown error while receiving files. Need to abort.
"
)
...
@@ -94,6 +114,14 @@ class FileReceiver:
...
@@ -94,6 +114,14 @@ class FileReceiver:
return
socket
return
socket
def
getZmqSocket_Rep
(
self
,
context
):
pattern_pull
=
zmq
.
REP
assert
isinstance
(
context
,
zmq
.
sugar
.
context
.
Context
)
socket
=
context
.
socket
(
pattern_pull
)
return
socket
def
createPullSocket
(
self
,
context
):
def
createPullSocket
(
self
,
context
):
assert
isinstance
(
context
,
zmq
.
sugar
.
context
.
Context
)
assert
isinstance
(
context
,
zmq
.
sugar
.
context
.
Context
)
socket
=
self
.
getZmqSocket_Pull
(
context
)
socket
=
self
.
getZmqSocket_Pull
(
context
)
...
@@ -104,9 +132,9 @@ class FileReceiver:
...
@@ -104,9 +132,9 @@ class FileReceiver:
return
socket
return
socket
def
create
Push
Socket
(
self
,
context
):
def
createSocket
ForLiveViewer
(
self
,
context
):
assert
isinstance
(
context
,
zmq
.
sugar
.
context
.
Context
)
assert
isinstance
(
context
,
zmq
.
sugar
.
context
.
Context
)
socket
=
self
.
getZmqSocket_
Pull
(
context
)
socket
=
self
.
getZmqSocket_
Rep
(
context
)
logging
.
info
(
"
binding to data socket: tcp://
"
+
self
.
zmqLiveViewerIp
+
"
:%s
"
%
self
.
bindingPortForLiveViewer
)
logging
.
info
(
"
binding to data socket: tcp://
"
+
self
.
zmqLiveViewerIp
+
"
:%s
"
%
self
.
bindingPortForLiveViewer
)
socket
.
bind
(
'
tcp://
'
+
self
.
zmqLiveViewerIp
+
'
:%s
'
%
self
.
bindingPortForLiveViewer
)
socket
.
bind
(
'
tcp://
'
+
self
.
zmqLiveViewerIp
+
'
:%s
'
%
self
.
bindingPortForLiveViewer
)
...
@@ -122,18 +150,61 @@ class FileReceiver:
...
@@ -122,18 +150,61 @@ class FileReceiver:
# if the maximal size is exceeded: remove the oldest files
# if the maximal size is exceeded: remove the oldest files
if
len
(
self
.
ringBuffer
)
>
self
.
maxRingBufferSize
:
if
len
(
self
.
ringBuffer
)
>
self
.
maxRingBufferSize
:
for
mod_time
,
path
in
self
.
ringBuffer
[
self
.
maxRingBufferSize
:]:
for
mod_time
,
path
in
self
.
ringBuffer
[
self
.
maxRingBufferSize
:]:
if
in
t
(
time
.
time
())
-
mod_time
>
self
.
timeToWaitForRingBuffer
:
if
floa
t
(
time
.
time
())
-
mod_time
>
self
.
timeToWaitForRingBuffer
:
os
.
remove
(
path
)
os
.
remove
(
path
)
self
.
ringBuffer
.
remove
([
mod_time
,
path
])
self
.
ringBuffer
.
remove
([
mod_time
,
path
])
# Albula is the live viewer used at the beamlines
# Albula is the live viewer used at the beamlines
def
sendFileTo
Albula
(
self
,
zmqLiveViewerSocket
):
def
sendFileTo
LiveViewer
(
self
,
zmqLiveViewerSocket
):
#send first element in ring buffer to albula
#send first element in ring buffer to albula
pass
pass
def
startReceiving
(
self
):
def
combineMessage
(
self
,
zmqSocket
):
# multipartMessage = zmqSocket.recv_multipart()
# logging.info("New message received.")
# logging.debug("message-type : " + str(type(multipartMessage)))
# logging.debug("message-length: " + str(len(multipartMessage)))
# loopCounter+=1
#save all chunks to file
while
True
:
multipartMessage
=
zmqSocket
.
recv_multipart
()
#append to file
try
:
logging
.
debug
(
"
append to file based on multipart-message...
"
)
#TODO: save message to file using a thread (avoids blocking)
#TODO: instead of open/close file for each chunk recyle the file-descriptor for all chunks opened
self
.
appendChunksToFileFromMultipartMessage
(
multipartMessage
)
logging
.
debug
(
"
append to file based on multipart-message...success.
"
)
except
Exception
,
e
:
errorMessage
=
"
Unable to append multipart-content to file.
"
logging
.
error
(
errorMessage
)
logging
.
debug
(
"
Error was:
"
+
str
(
e
))
logging
.
debug
(
"
append to file based on multipart-message...failed.
"
)
except
:
errorMessage
=
"
Unable to append multipart-content to file. Unknown Error.
"
logging
.
error
(
errorMessage
)
logging
.
debug
(
"
append to file based on multipart-message...failed.
"
)
if
len
(
multipartMessage
[
1
])
==
0
:
#indicated end of file. closing file and leave loop
logging
.
debug
(
"
last file-chunk received. stop appending.
"
)
break
payloadMetadata
=
str
(
multipartMessage
[
0
])
payloadMetadataDict
=
json
.
loads
(
payloadMetadata
)
filename
=
self
.
generateTargetFilepath
(
payloadMetadataDict
)
fileModTime
=
payloadMetadataDict
[
"
fileModificationTime
"
]
logging
.
info
(
"
New file with modification time
"
+
str
(
fileModTime
)
+
"
received and saved:
"
+
str
(
filename
))
# logging.debug("message-type : " + str(type(multipartMessage)))
# logging.debug("message-length: " + str(len(multipartMessage)))
# add to ring buffer
self
.
addFileToRingBuffer
(
str
(
filename
),
fileModTime
)
def
startReceiving
(
self
,
zmqLiveViewerSocket
):
#create pull socket
#create pull socket
try
:
try
:
logging
.
info
(
"
creating local pullSocket for incoming files...
"
)
logging
.
info
(
"
creating local pullSocket for incoming files...
"
)
...
@@ -147,19 +218,6 @@ class FileReceiver:
...
@@ -147,19 +218,6 @@ class FileReceiver:
logging
.
info
(
"
creating local pullSocket for incoming files...failed.
"
)
logging
.
info
(
"
creating local pullSocket for incoming files...failed.
"
)
raise
Exception
(
e
)
raise
Exception
(
e
)
#create pull socket
try
:
logging
.
info
(
"
creating local pushSocket for outgoing files...
"
)
zmqContext
=
self
.
getZmqContext
()
zmqLiveViewerSocket
=
self
.
createPushSocket
(
zmqContext
)
logging
.
info
(
"
creating local pushSocket for outgoing files...done.
"
)
except
Exception
,
e
:
errorMessage
=
"
Unable to create zeromq context.
"
logging
.
error
(
errorMessage
)
logging
.
debug
(
"
Error was:
"
+
str
(
e
))
logging
.
info
(
"
creating local pushSocket for outgoing files...failed.
"
)
raise
Exception
(
e
)
#run loop, and wait for incoming messages
#run loop, and wait for incoming messages
continueStreaming
=
True
continueStreaming
=
True
loopCounter
=
0
#counter of total received messages
loopCounter
=
0
#counter of total received messages
...
@@ -167,42 +225,7 @@ class FileReceiver:
...
@@ -167,42 +225,7 @@ class FileReceiver:
logging
.
debug
(
"
Waiting for new messages...
"
)
logging
.
debug
(
"
Waiting for new messages...
"
)
while
continueReceiving
:
while
continueReceiving
:
try
:
try
:
# multipartMessage = zmqSocket.recv_multipart()
self
.
combineMessage
(
zmqSocket
)
# logging.info("New message received.")
# logging.debug("message-type : " + str(type(multipartMessage)))
# logging.debug("message-length: " + str(len(multipartMessage)))
# loopCounter+=1
#save all chunks to file
while
True
:
multipartMessage
=
zmqSocket
.
recv_multipart
()
#append to file
try
:
logging
.
debug
(
"
append to file based on multipart-message...
"
)
#TODO: save message to file using a thread (avoids blocking)
#TODO: instead of open/close file for each chunk recyle the file-descriptor for all chunks opened
self
.
appendChunksToFileFromMultipartMessage
(
multipartMessage
,
zmqLiveViewerSocket
)
logging
.
debug
(
"
append to file based on multipart-message...success.
"
)
except
Exception
,
e
:
errorMessage
=
"
Unable to append multipart-content to file.
"
logging
.
error
(
errorMessage
)
logging
.
debug
(
"
Error was:
"
+
str
(
e
))
logging
.
debug
(
"
append to file based on multipart-message...failed.
"
)
except
:
errorMessage
=
"
Unable to append multipart-content to file. Unknown Error.
"
logging
.
error
(
errorMessage
)
logging
.
debug
(
"
append to file based on multipart-message...failed.
"
)
if
len
(
multipartMessage
[
1
])
==
0
:
#indicated end of file. closing file and leave loop
logging
.
debug
(
"
last file-chunk received. stop appending.
"
)
break
payloadMetadata
=
str
(
multipartMessage
[
0
])
payloadMetadataDict
=
json
.
loads
(
payloadMetadata
)
filename
=
self
.
generateTargetFilepath
(
payloadMetadataDict
)
fileModTime
=
payloadMetadataDict
logging
.
info
(
"
New file with modification time
"
+
fileModTime
+
"
received and saved:
"
+
str
(
filename
))
# logging.debug("message-type : " + str(type(multipartMessage)))
# logging.debug("message-length: " + str(len(multipartMessage)))
loopCounter
+=
1
loopCounter
+=
1
except
KeyboardInterrupt
:
except
KeyboardInterrupt
:
logging
.
debug
(
"
Keyboard interrupt detected. Stop receiving.
"
)
logging
.
debug
(
"
Keyboard interrupt detected. Stop receiving.
"
)
...
@@ -212,19 +235,10 @@ class FileReceiver:
...
@@ -212,19 +235,10 @@ class FileReceiver:
logging
.
error
(
sys
.
exc_info
())
logging
.
error
(
sys
.
exc_info
())
continueReceiving
=
False
continueReceiving
=
False
# add to ring buffer
self
.
addFileToRingBuffer
(
filename
,
fileModTime
)
# send to albula
self
.
sendNewestFileToAlbula
(
zmqLiveViewerSocket
)
logging
.
info
(
"
shutting down receiver...
"
)
logging
.
info
(
"
shutting down receiver...
"
)
try
:
try
:
logging
.
debug
(
"
shutting down zeromq...
"
)
logging
.
debug
(
"
shutting down zeromq...
"
)
self
.
stopReceiving
(
zmqSocket
,
zm
p
LiveViewer
,
zmqContext
)
self
.
stopReceiving
(
zmqSocket
,
zm
q
LiveViewer
Socket
,
zmqContext
)
logging
.
debug
(
"
shutting down zeromq...done.
"
)
logging
.
debug
(
"
shutting down zeromq...done.
"
)
except
:
except
:
logging
.
error
(
sys
.
exc_info
())
logging
.
error
(
sys
.
exc_info
())
...
@@ -266,7 +280,7 @@ class FileReceiver:
...
@@ -266,7 +280,7 @@ class FileReceiver:
return
targetPath
return
targetPath
def
appendChunksToFileFromMultipartMessage
(
self
,
multipartMessage
,
zmqLiveViewerSocket
):
def
appendChunksToFileFromMultipartMessage
(
self
,
multipartMessage
):
#extract multipart message
#extract multipart message
try
:
try
:
...
@@ -329,7 +343,7 @@ class FileReceiver:
...
@@ -329,7 +343,7 @@ class FileReceiver:
def
stopReceiving
(
self
,
zmqSocket
,
zmqLiveViwerSocket
,
msgContext
):
def
stopReceiving
(
self
,
zmqSocket
,
zmqLiveVi
e
werSocket
,
msgContext
):
try
:
try
:
logging
.
debug
(
"
closing zmqSocket...
"
)
logging
.
debug
(
"
closing zmqSocket...
"
)
zmqSocket
.
close
()
zmqSocket
.
close
()
...
@@ -387,7 +401,7 @@ def initLogging(filenameFullPath, verbose):
...
@@ -387,7 +401,7 @@ def initLogging(filenameFullPath, verbose):
#log everything to file
#log everything to file
logging
.
basicConfig
(
level
=
loggingLevel
,
logging
.
basicConfig
(
level
=
loggingLevel
,
format
=
'
[%(asctime)s] [PID %(process)d] [%(filename)s] [%(module)s:%(funcName)s] [%(name)s] [%(levelname)s] %(message)s
'
,
format
=
'
[%(asctime)s] [PID %(process)d] [%(filename)s] [%(module)s:%(funcName)s
:%(lineno)d
] [%(name)s] [%(levelname)s] %(message)s
'
,
datefmt
=
'
%Y-%m-%d_%H:%M:%S
'
,
datefmt
=
'
%Y-%m-%d_%H:%M:%S
'
,
filename
=
filenameFullPath
,
filename
=
filenameFullPath
,
filemode
=
"
a
"
)
filemode
=
"
a
"
)
...
@@ -410,7 +424,7 @@ if __name__ == "__main__":
...
@@ -410,7 +424,7 @@ if __name__ == "__main__":
zqmDataStreamPort
=
str
(
arguments
.
tcpPortDataStream
)
zqmDataStreamPort
=
str
(
arguments
.
tcpPortDataStream
)
zmqLiveViewerPort
=
str
(
arguments
.
tcpPortLiveViewer
)
zmqLiveViewerPort
=
str
(
arguments
.
tcpPortLiveViewer
)
zqmDataStreamIp
=
str
(
arguments
.
bindingIpForDataStream
)
zqmDataStreamIp
=
str
(
arguments
.
bindingIpForDataStream
)
zmqLiveViewerIp
=
str
(
arguments
.
bin
g
inIpForLiveViewer
)
zmqLiveViewerIp
=
str
(
arguments
.
bin
d
in
g
IpForLiveViewer
)
logFile
=
arguments
.
logfile
logFile
=
arguments
.
logfile
logfileFilePath
=
arguments
.
logfile
logfileFilePath
=
arguments
.
logfile
...
@@ -420,4 +434,4 @@ if __name__ == "__main__":
...
@@ -420,4 +434,4 @@ if __name__ == "__main__":
#start file receiver
#start file receiver
myWorker
=
FileReceiver
(
outputDir
,
zqmDataStreamPort
,
zqmDataStreamIp
,
bindingPortFor
LiveViewer
,
zmqLiveViewerIp
)
:
myWorker
=
FileReceiver
(
outputDir
,
zqmDataStreamPort
,
zqmDataStreamIp
,
zmq
LiveViewer
Port
,
zmqLiveViewerIp
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment