blob: d09e94fc2314bee4b2a1243edcf21a432f7e7f47 [file] [log] [blame]
Ashlesh Gawande6c86e302019-09-17 22:27:05 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
awlane85163862025-02-05 10:38:47 -06003# Copyright (C) 2015-2025, The University of Memphis,
Ashlesh Gawande6c86e302019-09-17 22:27:05 -05004# 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 Patil739fc732025-01-26 13:56:50 -080024import subprocess
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050025from minindn.util import getPopen
awlanec02f5c32025-02-03 09:46:19 -060026from typing import Union, Optional
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050027
28class Application(object):
Varun Patil739fc732025-01-26 13:56:50 -080029 process: subprocess.Popen
30
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050031 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
awlanec02f5c32025-02-03 09:46:19 -060041 def start(self, command: Union[str, list], logfile: Optional[str]=None, envDict: Optional[dict]=None) -> None:
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050042 if self.process is None:
Varun Patilaed282d2023-09-06 13:04:49 -070043 if isinstance(command, str):
44 command = command.split()
awlanec02f5c32025-02-03 09:46:19 -060045 if not logfile:
46 self.process = getPopen(self.node, command, envDict)
47 return
48 self.logfile = open('{}/{}'.format(self.logDir, logfile), 'w')
Varun Patilaed282d2023-09-06 13:04:49 -070049 self.process = getPopen(self.node, command, envDict,
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050050 stdout=self.logfile, stderr=self.logfile)
51
52 def stop(self):
53 if self.process is not None:
Varun Patil739fc732025-01-26 13:56:50 -080054 try:
55 self.process.terminate()
56 self.process.wait(timeout=5)
57 except subprocess.TimeoutExpired:
58 self.process.kill()
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050059 self.process = None
60 if self.logfile is not None:
61 self.logfile.close()