-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsync.py
84 lines (66 loc) · 2.68 KB
/
tsync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import subprocess
import argparse
import os
import signal
import socket
# Define paths for the PID files
SERVER_PID_FILE = 'server.pid'
CLIENT_PID_FILE = 'client.pid'
LOG_FILE = 'tsync.log.out'
def get_internal_ip():
"""Get the internal IP address of the current machine."""
try:
# Create a temporary socket to connect to a remote host (Google's DNS server)
temp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
temp_socket.connect(("8.8.8.8", 80))
internal_ip = temp_socket.getsockname()[0]
temp_socket.close()
return internal_ip
except socket.error as e:
print(f"Failed to retrieve internal IP: {e}")
return '127.0.0.1' # Default to loopback address if failed
def get_username():
"""Get the username of the current user."""
return os.getlogin() # Get the current username
def start_processes(username, ip_address):
"""Start the server and client processes."""
with open(LOG_FILE, 'a') as log_file:
server_process = subprocess.Popen(
['python3', 'monitor.py', '-ip', ip_address, '-port', '8082', '-uname', username, '-role', 'client'],
stdout=log_file, stderr=subprocess.STDOUT)
client_process = subprocess.Popen(
['python3', 'monitor.py', '-ip', ip_address, '-port', '8081', '-uname', username, '-role', 'server'],
stdout=log_file, stderr=subprocess.STDOUT)
# Write the PIDs to files
with open(SERVER_PID_FILE, 'w') as f:
f.write(str(server_process.pid))
with open(CLIENT_PID_FILE, 'w') as f:
f.write(str(client_process.pid))
print(f"Started server (PID {server_process.pid}) and client (PID {client_process.pid})")
def stop_process(pid_file):
"""Stop a process given its PID file."""
if os.path.exists(pid_file):
with open(pid_file, 'r') as f:
pid = int(f.read().strip())
try:
os.kill(pid, signal.SIGTERM)
print(f"Stopped process with PID {pid}")
except ProcessLookupError:
print(f"No process with PID {pid} found")
os.remove(pid_file)
else:
print(f"No PID file {pid_file} found")
def stop_processes():
"""Stop the server and client processes."""
stop_process(SERVER_PID_FILE)
stop_process(CLIENT_PID_FILE)
parser = argparse.ArgumentParser()
parser.add_argument('--command', choices=['start', 'stop'], required=True,
help='Command to start or stop the processes')
args = parser.parse_args()
username = get_username()
ip_address = get_internal_ip()
if args.command == 'start':
start_processes(username, ip_address)
elif args.command == 'stop':
stop_processes()