Commit dd3e14f8 authored by Pedro Oliveira's avatar Pedro Oliveira

fixed argparser -> requires one argument

parent f91a2e1a
...@@ -63,14 +63,7 @@ class Daemon: ...@@ -63,14 +63,7 @@ class Daemon:
"""Start the Daemon.""" """Start the Daemon."""
# Check for a pidfile to see if the Daemon already runs # Check for a pidfile to see if the Daemon already runs
try: if self.is_running():
with open(self.pidfile,'r') as pf:
pid = int(pf.read().strip())
except IOError:
pid = None
if pid:
message = "pidfile {0} already exist. " + \ message = "pidfile {0} already exist. " + \
"Daemon already running?\n" "Daemon already running?\n"
sys.stderr.write(message.format(self.pidfile)) sys.stderr.write(message.format(self.pidfile))
...@@ -78,7 +71,6 @@ class Daemon: ...@@ -78,7 +71,6 @@ class Daemon:
# Start the Daemon # Start the Daemon
self.daemonize() self.daemonize()
print("daemonize")
self.run() self.run()
def stop(self): def stop(self):
...@@ -86,7 +78,7 @@ class Daemon: ...@@ -86,7 +78,7 @@ class Daemon:
# Get the pid from the pidfile # Get the pid from the pidfile
try: try:
with open(self.pidfile,'r') as pf: with open(self.pidfile, 'r') as pf:
pid = int(pf.read().strip()) pid = int(pf.read().strip())
except IOError: except IOError:
pid = None pid = None
...@@ -120,4 +112,19 @@ class Daemon: ...@@ -120,4 +112,19 @@ class Daemon:
"""You should override this method when you subclass Daemon. """You should override this method when you subclass Daemon.
It will be called after the process has been daemonized by It will be called after the process has been daemonized by
start() or restart().""" start() or restart()."""
\ No newline at end of file
def is_running(self):
try:
with open(self.pidfile, 'r') as pf:
pid = int(pf.read().strip())
except IOError:
return False
""" Check For the existence of a unix pid. """
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True
...@@ -10,11 +10,15 @@ import argparse ...@@ -10,11 +10,15 @@ import argparse
class MyDaemon(Daemon): class MyDaemon(Daemon):
def stop(self):
print("stopping...")
# TODO: remove all interfaces
super(MyDaemon, self).stop()
def run(self): def run(self):
Main.main() Main.main()
server_address = './uds_socket' server_address = './uds_socket'
# Make sure the socket does not already exist # Make sure the socket does not already exist
try: try:
os.unlink(server_address) os.unlink(server_address)
...@@ -43,10 +47,10 @@ class MyDaemon(Daemon): ...@@ -43,10 +47,10 @@ class MyDaemon(Daemon):
connection.sendall(pickle.dumps(Main.list_neighbors())) connection.sendall(pickle.dumps(Main.list_neighbors()))
elif args.add_interface: elif args.add_interface:
Main.add_interface(args.add_interface[0]) Main.add_interface(args.add_interface[0])
connection.sendall(pickle.dumps('')) connection.shutdown(socket.SHUT_RDWR)
elif args.remove_interface: elif args.remove_interface:
Main.remove_interface(args.remove_interface[0]) Main.remove_interface(args.remove_interface[0])
connection.sendall(pickle.dumps('')) connection.shutdown(socket.SHUT_RDWR)
finally: finally:
# Clean up the connection # Clean up the connection
connection.close() connection.close()
...@@ -54,22 +58,15 @@ class MyDaemon(Daemon): ...@@ -54,22 +58,15 @@ class MyDaemon(Daemon):
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description='PIM') parser = argparse.ArgumentParser(description='PIM')
parser.add_argument("-li", "--list_interfaces", action="store_true", default=False, group = parser.add_mutually_exclusive_group(required=True)
help="List All PIM Interfaces") group.add_argument("-start", "--start", action="store_true", default=False, help="Start PIM")
parser.add_argument("-ln", "--list_neighbors", action="store_true", default=False, group.add_argument("-stop", "--stop", action="store_true", default=False, help="Stop PIM")
help="List All PIM Neighbors") group.add_argument("-restart", "--restart", action="store_true", default=False, help="Restart PIM")
parser.add_argument("-ai", "--add_interface", nargs=1, group.add_argument("-li", "--list_interfaces", action="store_true", default=False, help="List All PIM Interfaces")
help="Add PIM interface") group.add_argument("-ln", "--list_neighbors", action="store_true", default=False, help="List All PIM Neighbors")
parser.add_argument("-ri", "--remove_interface", nargs=1, group.add_argument("-ai", "--add_interface", nargs=1, metavar='IP_INTERFACE', help="Add PIM interface")
help="Remove PIM interface") group.add_argument("-ri", "--remove_interface", nargs=1, metavar='IP_INTERFACE', help="Remove PIM interface")
parser.add_argument("-start", "--start", action="store_true", default=False, group.add_argument("-v", "--verbose", action="store_true", default=False, help="Verbose (print all debug messages)")
help="Start PIM")
parser.add_argument("-stop", "--stop", action="store_true", default=False,
help="Stop PIM")
parser.add_argument("-restart", "--restart", action="store_true", default=False,
help="Restart PIM")
parser.add_argument("-v", "--verbose", action="store_true", default=False,
help="Verbose (print all debug messages)")
args = parser.parse_args() args = parser.parse_args()
print(parser.parse_args()) print(parser.parse_args())
...@@ -78,7 +75,6 @@ if __name__ == "__main__": ...@@ -78,7 +75,6 @@ if __name__ == "__main__":
if args.start: if args.start:
print("start") print("start")
daemon.start() daemon.start()
print("start")
sys.exit(0) sys.exit(0)
elif args.stop: elif args.stop:
daemon.stop() daemon.stop()
...@@ -89,6 +85,10 @@ if __name__ == "__main__": ...@@ -89,6 +85,10 @@ if __name__ == "__main__":
elif args.verbose: elif args.verbose:
os.system("tailf stdout") os.system("tailf stdout")
sys.exit(0) sys.exit(0)
elif not daemon.is_running():
print("PIM is not running")
parser.print_usage()
sys.exit(0)
# Create a UDS socket # Create a UDS socket
...@@ -99,7 +99,7 @@ if __name__ == "__main__": ...@@ -99,7 +99,7 @@ if __name__ == "__main__":
print('connecting to %s' % server_address) print('connecting to %s' % server_address)
try: try:
sock.connect(server_address) sock.connect(server_address)
except (socket.error): except socket.error:
print("erro socket") print("erro socket")
print(sys.stderr) print(sys.stderr)
sys.exit(1) sys.exit(1)
...@@ -108,8 +108,8 @@ if __name__ == "__main__": ...@@ -108,8 +108,8 @@ if __name__ == "__main__":
sock.sendall(pickle.dumps(args)) sock.sendall(pickle.dumps(args))
data = sock.recv(1024 * 256) data = sock.recv(1024 * 256)
print(pickle.loads(data)) if data:
print(pickle.loads(data))
finally: finally:
print('closing socket') print('closing socket')
sock.close() sock.close()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment