-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRpc_Server.py
More file actions
120 lines (97 loc) · 4.28 KB
/
Rpc_Server.py
File metadata and controls
120 lines (97 loc) · 4.28 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Name: Amey Mahendra Thakur
# Course: Distributed Computing Lab (CSL802)
# Roll No: 50 | Batch: B3
# Date of Experiment: March 31, 2022
# Repository: https://github.com/Amey-Thakur/DISTRIBUTED-COMPUTING-AND-DISTRIBUTED-COMPUTING-LAB
# Description: Experiment 10 - Implementation of Name Resolution and RPC (Server side).
# This script facilitates a chat-based interface where the client can request file status, directory paths, and data.
import time
import socket
import sys
import os
import os.path
def main():
print("="*60)
print(" DISTRIBUTED NAME RESOLUTION & RPC SERVER ")
print("="*60)
print("\n[+] Initializing Server Components...")
time.sleep(1)
# 1. Socket Setup
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
ip = socket.gethostbyname(host)
port = 1234
s.bind((host, port))
print(f"[*] Server Identity: {host} ({ip})")
print(f"[*] Listening on Port: {port}")
name = input("\n[?] Enter Administrator Name: ")
s.listen(1)
print("\n[*] Waiting for incoming client connection...")
conn, addr = s.accept()
print(f"[+] Connection established with {addr[0]} (Port: {addr[1]})")
# 2. Handshake Phase
client_name = conn.recv(1024).decode()
print(f"\n[!] Client '{client_name}' has joined the session.")
conn.send(name.encode())
# 3. Command Menu Transmission
commands_menu = '''
=========================================
AVAILABLE RPC COMMANDS:
1. request-file-status:<filename>
2. request-file-dir:<filename>
3. request-file-data:<filename>
=========================================
'''
conn.send(commands_menu.encode())
# 4. Main Service Loop
while True:
try:
data = conn.recv(1024).decode()
if not data or data == "[e]":
print(f"\n[*] Client '{client_name}' requested termination.")
break
print(f"\n[CLIENT]: {data}")
# Command Parsing
if ":" not in data:
conn.send("Error: Invalid command format. Use 'command:filename'".encode())
continue
command, filename = data.split(':', 1)
data_path = "data/"
filepath = os.path.join(data_path, filename)
# RPC Logic: Name & Resource Resolution
if command == "request-file-status":
status = os.path.exists(filepath)
response = f"[STATUS] {filename} exists." if status else f"[ERROR] {filename} not found."
conn.send(response.encode())
elif command == "request-file-dir":
if os.path.exists(filepath):
abs_path = os.path.abspath(filepath)
conn.send(f"[DIRECTORY] {abs_path}".encode())
else:
conn.send(f"[ERROR] {filename} does not exist.".encode())
elif command == "request-file-data":
if os.path.exists(filepath):
# Inform client to prepare for data stream
conn.send(f"sending-file:{filename}".encode())
ack = conn.recv(1024).decode()
if ack == "ready":
with open(filepath, "r") as f:
file_content = f.read()
conn.send(file_content.encode())
print(f"[*] Successfully transferred {filename} contents.")
else:
conn.send(f"[ERROR] {filename} does not exist.".encode())
else:
conn.send("[ERROR] Unknown RPC Command.".encode())
except Exception as e:
print(f"[!] Processing Error: {e}")
break
except Exception as e:
print(f"[!] Server Failure: {e}")
finally:
print("\n[*] Shutting down server. Cleaning resources...")
s.close()
print("="*60)
if __name__ == "__main__":
main()