blob: aca153d105a1e9f15af76cdce66362d630ebd5c7 [file] [log] [blame]
Ashlesh Gawande6c86e302019-09-17 22:27:05 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
3# Copyright (C) 2015-2019, The University of Memphis,
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
24from minindn.util import getPopen
awlanec02f5c32025-02-03 09:46:19 -060025from typing import Union, Optional
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050026
27class Application(object):
28 def __init__(self, node):
29 self.node = node
30 self.process = None
31 self.logfile = None
32 self.homeDir = self.node.params['params']['homeDir']
33
34 # Make directory for log file
35 self.logDir = '{}/log'.format(self.homeDir)
36 self.node.cmd('mkdir -p {}'.format(self.logDir))
37
awlanec02f5c32025-02-03 09:46:19 -060038 def start(self, command: Union[str, list], logfile: Optional[str]=None, envDict: Optional[dict]=None) -> None:
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050039 if self.process is None:
Varun Patilaed282d2023-09-06 13:04:49 -070040 if isinstance(command, str):
41 command = command.split()
awlanec02f5c32025-02-03 09:46:19 -060042 if not logfile:
43 self.process = getPopen(self.node, command, envDict)
44 return
45 self.logfile = open('{}/{}'.format(self.logDir, logfile), 'w')
Varun Patilaed282d2023-09-06 13:04:49 -070046 self.process = getPopen(self.node, command, envDict,
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050047 stdout=self.logfile, stderr=self.logfile)
48
49 def stop(self):
50 if self.process is not None:
51 self.process.kill()
52 self.process = None
53 if self.logfile is not None:
54 self.logfile.close()