-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
262 lines (207 loc) · 9.87 KB
/
server.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import os
import socket
import json
import math
import re
from collections import Counter
import threading
# ---------- RPCメソッドの定義 -------------------------------------------------
class RPCMethods:
@staticmethod
def add(x, y):
return x + y
@staticmethod
def subtract(x, y):
return x - y
@staticmethod
def multiply(x, y):
return x * y
@staticmethod
def divide(x, y):
if y == 0:
raise ValueError("0で割ることはできません")
return x / y
@staticmethod
def floor(x):
return math.floor(x)
@staticmethod
def nroot(n, x):
if n <= 0:
raise ValueError("1以上の整数を指定してください")
return math.pow(x, 1 / n)
@staticmethod
def reverse(s):
return s[::-1]
@staticmethod
def validAnagram(str1, str2):
str1_clean = re.sub(r"[^a-z0-9]", "", str1.lower())
str2_clean = re.sub(r"[^a-z0-9]", "", str2.lower())
if len(str1_clean) != len(str2_clean):
return False
str1_elem = Counter(str1_clean)
str2_elem = Counter(str2_clean)
return str1_elem == str2_elem
@staticmethod
def sort_list(array):
if not array or len(array) < 2:
raise ValueError("文字列を空白区切りで2つ以上入力してください")
return sorted(array, key=str.lower)
@staticmethod
def help():
return {
"commands": [
{"name": "add", "description": "足し算", "example": "add 1 2 --> 3"},
{"name": "subtract", "description": "引き算", "example": "subtract 9 2 --> 7"},
{"name": "multiply", "description": "掛け算", "example": "multiply 5 4 --> 20"},
{"name": "divide", "description": "割り算", "example": "divide 10 2 --> 5"},
{"name": "floor", "description": "切捨て", "example": "floor 1.35 --> 1"},
{"name": "nroot", "description": "n乗根", "example": "nroot 3 64 --> 4"},
{"name": "reverse", "description": "文字を反転", "example": "reverse HelloWorld! --> !dlroWolleH"},
{"name": "validAnagram", "description": "アナグラムか確認", "example": "validAnagram HelloWorld! olleH!dlroW --> true"},
{"name": "sort", "description": "リストをソート", "example": "sort spade diamond clover heart --> ['clover', 'diamond', 'heart', 'spade']"},
{"name": "help", "description": "このヘルプを表示"},
]
}
# ---------- クライアントからのリクエストを処理するクラス ----------------------
class RequestHandler:
def __init__(self, connection, client_address, client_map):
self.connection = connection
self.client_address = client_address
# RPCメソッドをハッシュマップにまとめる
self.methods = {
"add": RPCMethods.add,
"subtract": RPCMethods.subtract,
"multiply": RPCMethods.multiply,
"divide": RPCMethods.divide,
"floor": RPCMethods.floor,
"nroot": RPCMethods.nroot,
"reverse": RPCMethods.reverse,
"validAnagram": RPCMethods.validAnagram,
"sort": RPCMethods.sort_list,
"help": RPCMethods.help,
}
def handle_client(self):
try:
current_thread_id = threading.get_ident()
print(f"connection from: {self.client_address}")
welcome_message = {
"result": "サーバに接続しました。\nコマンドを入力してください(helpでコマンド一覧、exitで終了)",
"result_type": "str",
"id": None,
}
self.connection.sendall((json.dumps(welcome_message) + "\n").encode())
data_str = ""
# クライアントからの新しいメッセージを待ち続ける
while True:
# 接続からデータを読み込む(最大4096byte)
data = self.connection.recv(4096)
if not data:
break
# 受け取ったデータを文字列に変換する
data_str += data.decode("utf-8")
# 受け取ったデータを表示する
print(f"Received on thread{current_thread_id}: {data_str}")
# while "\n" in data_str:
# request_json, data_str = data_str.split("\n", 1)
lines = data_str.splitlines()
data_str = ""
for request_json in lines:
# json形式にパース
try:
request = json.loads(request_json)
method = request.get("method")
params = request.get("params", {})
response_id = request.get("id", None)
# 存在するメソッドの場合
if method in self.methods:
if isinstance(params, dict):
result = self.methods[method](**params)
elif isinstance(params, list):
if method == "sort":
result = self.methods[method](params)
else:
result = self.methods[method](*params)
else:
result = "Invalid parameter format"
response = {
"result": result,
"result_type": type(
result
).__name__, # 実際の型を動的に取得して返す
"id": response_id,
}
# 存在しないメソッドの場合
else:
response = {
"error": "指定されたメソッドは実装されていません",
"id": response_id,
}
except Exception as e:
response = {
"error": str(e),
"id": None,
}
# 処理したメッセージをバイナリ形式に変換してクライアントに送り返す
print(f"Response on thread{current_thread_id}: {response}")
self.connection.sendall((json.dumps(response) + "\n").encode())
# 最終的に接続を閉じる
finally:
print("Closing current connection")
print(f"[DEBUG] closed thread {current_thread_id}")
self.connection.close()
# ---------- ソケットを管理するクラス ------------------------------------------
class RPCServer:
def __init__(self, config_path="config.json"):
try:
# ストリームのエンドポイントとなるソケットを作成する
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# サーバの UNIX ソケットの場所を設定する
with open(config_path) as f:
config = json.load(f)
self.server_address = config["filepath"]
except FileNotFoundError:
print(
"設定ファイル(config.json)が見つかりません。プログラムを終了します。"
)
exit(1)
# 以前の接続が残っていた場合に備えて、サーバアドレスを削除する(unlink)
try:
os.unlink(self.server_address)
except FileNotFoundError:
pass
print("Starting up on {}".format(self.server_address))
# サーバのアドレスにソケットを接続する(bind)
self.sock.bind(self.server_address)
# ソケットが接続要求を待機するようにする
self.sock.listen()
self.client_map = {}
def start_server(self):
try:
# クライアントからの接続を待ち続ける
while True:
# クライアントからの接続を受け入れる
connection, client_address = self.sock.accept()
# client_address が空の場合の処理
if not client_address:
client_address = "Unix socket(no client address)"
# クライアントごとにインスタンスを作成
client = RequestHandler(connection, client_address, self.client_map)
# クライアントごとにスレッドを作成
client_thread = threading.Thread(target=client.handle_client)
# クライアントプログラムを終了したらスレッドも終了
client_thread.daemon = True
client_thread.start()
self.client_map[client_thread.ident] = connection
print(f"[DEBUG] client_map: {self.client_map}")
print(f"[DEBUG] threads: {threading.enumerate()}")
except Exception as e:
print(f"エラーが発生しました: {e}")
finally:
if 'connection' in locals():
connection.close()
self.sock.close()
# ---------- メイン処理 --------------------------------------------------------
if __name__ == "__main__":
server = RPCServer()
server.start_server()
exit(0)