-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
114 lines (95 loc) · 3.85 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
import Pyro4
import sys
import random
import pickle
from pathlib import Path
namainstance = sys.argv[1] or "fileserver"
class FileServer(object):
def __init__(self):
self.board = [[None for j in range(9)] for i in range(9)]
self.f=replication()
self.replica=self.f.connect()
self.boardplayer = [[None,None,None],[None,None,None],[None,None,None],
[None,None,None],[None,None,None],[None,None,None],
[None,None,None],[None,None,None],[None,None,None]]
def coba2(self):
return "bisa fileserver"
def getserver_board(self):
# self.board[8][5] = 2
# self.board[7][6] = 2
# self.board[1][7] = 2
#
my_file = Path("board_data.db")
success=0
while success==0:
try:
if my_file.is_file():
with open('board_data.db','rb') as f:
self.board = pickle.load(f)
else:
self.board = [[None for j in range(9)] for i in range(9)]
with open('board_data.db','wb') as f:
pickle.dump(self.board,f, pickle.HIGHEST_PROTOCOL)
success=1
except Exception as e:
continue
return self.board
def getserver_boardplayer(self):
my_file = Path("board_playerdata.db")
success=0
while success==0:
try:
if my_file.is_file():
with open('board_playerdata.db','rb') as f:
self.boardplayer = pickle.load(f)
else:
self.boardplayer = [[None,None,None],[None,None,None],[None,None,None],
[None,None,None],[None,None,None],[None,None,None],
[None,None,None],[None,None,None],[None,None,None]]
with open('board_playerdata.db','wb') as f:
pickle.dump(self.boardplayer,f, pickle.HIGHEST_PROTOCOL)
success=1
except Exception as e:
continue
return self.boardplayer
def inputboard(self, newboard,boardplayer,darimana=1):
success=0
while success==0:
try:
self.board = newboard
with open('board_data.db','wb') as f:
pickle.dump(self.board,f, pickle.HIGHEST_PROTOCOL)
self.boardplayer = boardplayer
with open('board_playerdata.db','wb') as f:
pickle.dump(self.boardplayer,f, pickle.HIGHEST_PROTOCOL)
success=1
except Exception as e:
continue
print(darimana)
if darimana == 1 :
print("masuk if darimana")
self.replica.consistency(namainstance,self.board,self.boardplayer)
def start_with_ns():
#name server harus di start dulu dengan pyro4-ns -n localhost -p 7777
#gunakan URI untuk referensi name server yang akan digunakan
#untuk mengetahui instance apa saja yang aktif gunakan pyro4-nsc -n localhost -p 7777 list
daemon = Pyro4.Daemon(host="localhost")
ns = Pyro4.locateNS("localhost",7777)
x_FileServer = Pyro4.expose(FileServer)
uri_fileserver = daemon.register(x_FileServer)
ns.register("{}" . format(namainstance), uri_fileserver)
#untuk instance yang berbeda, namailah fileserver dengan angka
#ns.register("fileserver2", uri_fileserver)
#ns.register("fileserver3", uri_fileserver)
daemon.requestLoop()
class replication:
def connect(self):
uri = "PYRONAME:rm@localhost:7777"
fserver = Pyro4.Proxy(uri)
return fserver
if __name__ == '__main__':
rm = replication()
replica = rm.connect()
replica.add_server(namainstance)
print(replica.get_serverlist())
start_with_ns()