-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from dwikler/refact-echoscp
Refact echoscp
- Loading branch information
Showing
9 changed files
with
289 additions
and
65 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
def handle_cecho(event, logger): | ||
"""Handler for evt.EVT_C_ECHO. | ||
Parameters | ||
---------- | ||
event : events.Event | ||
The corresponding event. | ||
logger : logging.Logger | ||
The application's logger. | ||
Returns | ||
------- | ||
int | ||
The status of the C-ECHO operation, always ``0x0000`` (Success). | ||
""" | ||
requestor = event.assoc.requestor | ||
timestamp = event.timestamp.strftime("%Y-%m-%d %H:%M:%S") | ||
aet, addr, port = requestor.ae_title, requestor.address, requestor.port | ||
logger.info( | ||
f"Received C-ECHO request from {aet}@{addr}:{port} at {timestamp}" | ||
) | ||
|
||
return 0x0000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from pynetdicom import evt | ||
from pynetdicom.sop_class import Verification | ||
|
||
from tdwii_plus_examples.basescp import BaseSCP | ||
from tdwii_plus_examples.cechohandler import handle_cecho | ||
|
||
|
||
class CEchoSCP(BaseSCP): | ||
""" | ||
A subclass of the BaseSCP class that implements the DICOM Verification | ||
Service Class Provider (SCP). | ||
Usage: | ||
Create an instance of CEchoSCP and call the run method inherited | ||
from the BaseSCP parent class to start listening for incoming | ||
DICOM association requests. | ||
The CEchoSCP class is generally not used directly. Instead, create | ||
a subclass and override the [_add_contexts] and [_add_handlers] methods | ||
to add presentation contexts and handlers for specific DICOM services. | ||
Attributes: | ||
ae_title (str): The title of the Application Entity (AE) | ||
bind_address (str): The IP address or hostname of the AE | ||
port (int): The port number the AE will listen on | ||
logger (logging.Logger): A logger instance | ||
Methods: | ||
_add_contexts(self) | ||
Adds presentation contexts to the SCP instance. | ||
_add_handlers(self) | ||
Adds handlers for DICOM communication events to the SCP instance. | ||
""" | ||
def __init__(self, | ||
ae_title: str = "ECHO_SCP", | ||
bind_address: str = "", | ||
port: int = 11112, | ||
logger=None): | ||
|
||
""" | ||
Initializes a new instance of the CEchoSCP class. | ||
This method creates an AE without presentation contexts. | ||
Parameters | ||
---------- | ||
ae_title : str | ||
The title of the Application Entity (AE) | ||
Optional, default: "ECHO_SCP" | ||
bind_address : str | ||
A specific IP address or hostname of the AE | ||
Optional, default: "" will bind to all interfaces. | ||
port: int | ||
The port number to listen on | ||
Optional, default: 11112 (as registered for DICOM at IANA) | ||
logger: logging.Logger | ||
A logger instance | ||
Optional, default: None, a debug logger will be used. | ||
""" | ||
super().__init__( | ||
ae_title=ae_title, | ||
bind_address=bind_address, | ||
port=port, | ||
logger=logger) | ||
|
||
def _add_contexts(self): | ||
""" | ||
Adds the DICOM Verification SOP Class presentation context to the AE. | ||
Only the default Implicit VR Little Endian transfer syntax is included. | ||
This method overrides the base class method to add support for the | ||
Verification SOP Class which is required for any DICOM SCP. | ||
This method is intended to be overridden in derived classes. | ||
""" | ||
super()._add_contexts() | ||
self.ae.add_supported_context(Verification, "1.2.840.10008.1.2") | ||
|
||
def _add_handlers(self): | ||
""" | ||
Adds a handler for for processing incoming C-ECHO requests. | ||
This method overrides the base class method to add a handler | ||
for the Verification SOP Class, allowing the AE to respond | ||
to C-ECHO requests. | ||
This method is intended to be overridden in derived classes. | ||
""" | ||
super()._add_handlers() | ||
self.handlers.append((evt.EVT_C_ECHO, handle_cecho, | ||
[self.logger])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env python | ||
import argparse | ||
import logging | ||
|
||
from tdwii_plus_examples.cechoscp import CEchoSCP | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description="Run a DICOM Verification SCP." | ||
) | ||
parser.add_argument( | ||
'-a', '--ae_title', type=str, default='ECHO_SCP', | ||
help='Application Entity Title' | ||
) | ||
parser.add_argument( | ||
'-b', '--bind_address', type=str, default='', | ||
help='Specific IP address or hostname, omit to bind to all interfaces' | ||
) | ||
parser.add_argument( | ||
'-p', '--port', type=int, default=11112, | ||
help='Port number' | ||
) | ||
parser.add_argument( | ||
'-v', '--verbose', action='store_true', | ||
help='Set log level to INFO' | ||
) | ||
parser.add_argument( | ||
'-d', '--debug', action='store_true', | ||
help='Set log level to DEBUG' | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
log_level = logging.WARNING | ||
if args.verbose: | ||
log_level = logging.INFO | ||
elif args.debug: | ||
log_level = logging.DEBUG | ||
|
||
logging.basicConfig(level=log_level) | ||
logger = logging.getLogger('echoscp') | ||
|
||
logger.info("Starting up the DICOM Verification SCP...") | ||
cechoscp = CEchoSCP(ae_title=args.ae_title, bind_address=args.bind_address, | ||
port=args.port, logger=logger) | ||
cechoscp.run() | ||
# Keep the main application running | ||
try: | ||
while True: | ||
pass # You can replace this with your main application logic | ||
except KeyboardInterrupt: | ||
logger.info("Shutting down the DICOM Verification SCP...") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.