Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
awlane | 8516386 | 2025-02-05 10:38:47 -0600 | [diff] [blame] | 3 | # Copyright (C) 2015-2025, The University of Memphis, |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 4 | # Arizona Board of Regents, |
| 5 | # Regents of the University of California. |
| 6 | # |
| 7 | # This file is part of Mini-NDN. |
| 8 | # See AUTHORS.md for a complete list of Mini-NDN authors and contributors. |
| 9 | # |
| 10 | # Mini-NDN is free software: you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License as published by |
| 12 | # the Free Software Foundation, either version 3 of the License, or |
| 13 | # (at your option) any later version. |
| 14 | # |
| 15 | # Mini-NDN is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | # GNU General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU General Public License |
| 21 | # along with Mini-NDN, e.g., in COPYING.md file. |
| 22 | # If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
Varun Patil | 739fc73 | 2025-01-26 13:56:50 -0800 | [diff] [blame] | 24 | import subprocess |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 25 | from minindn.util import getPopen |
awlane | c02f5c3 | 2025-02-03 09:46:19 -0600 | [diff] [blame] | 26 | from typing import Union, Optional |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 27 | |
| 28 | class Application(object): |
Varun Patil | 739fc73 | 2025-01-26 13:56:50 -0800 | [diff] [blame] | 29 | process: subprocess.Popen |
| 30 | |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 31 | def __init__(self, node): |
| 32 | self.node = node |
| 33 | self.process = None |
| 34 | self.logfile = None |
| 35 | self.homeDir = self.node.params['params']['homeDir'] |
| 36 | |
| 37 | # Make directory for log file |
| 38 | self.logDir = '{}/log'.format(self.homeDir) |
| 39 | self.node.cmd('mkdir -p {}'.format(self.logDir)) |
| 40 | |
awlane | c02f5c3 | 2025-02-03 09:46:19 -0600 | [diff] [blame] | 41 | def start(self, command: Union[str, list], logfile: Optional[str]=None, envDict: Optional[dict]=None) -> None: |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 42 | if self.process is None: |
Varun Patil | aed282d | 2023-09-06 13:04:49 -0700 | [diff] [blame] | 43 | if isinstance(command, str): |
| 44 | command = command.split() |
awlane | c02f5c3 | 2025-02-03 09:46:19 -0600 | [diff] [blame] | 45 | if not logfile: |
| 46 | self.process = getPopen(self.node, command, envDict) |
| 47 | return |
| 48 | self.logfile = open('{}/{}'.format(self.logDir, logfile), 'w') |
Varun Patil | aed282d | 2023-09-06 13:04:49 -0700 | [diff] [blame] | 49 | self.process = getPopen(self.node, command, envDict, |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 50 | stdout=self.logfile, stderr=self.logfile) |
| 51 | |
| 52 | def stop(self): |
| 53 | if self.process is not None: |
Varun Patil | 739fc73 | 2025-01-26 13:56:50 -0800 | [diff] [blame] | 54 | try: |
| 55 | self.process.terminate() |
| 56 | self.process.wait(timeout=5) |
| 57 | except subprocess.TimeoutExpired: |
| 58 | self.process.kill() |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 59 | self.process = None |
| 60 | if self.logfile is not None: |
| 61 | self.logfile.close() |