def contact_branches(self): branches = self._parse_file() init_balance = self.total_init_balance / len(branches) # print init_balance protobuf_branches = [] for br in branches: curr_branch = bank_pb2.InitBranch().Branch() curr_branch.name = br.name curr_branch.ip = br.ip curr_branch.port = int(br.port) protobuf_branches.append(curr_branch) initbranch = bank_pb2.InitBranch() initbranch.balance = init_balance initbranch.all_branches.extend(protobuf_branches) branchmsg = bank_pb2.BranchMessage() branchmsg.init_branch.CopyFrom(initbranch) for br in branches: s = socket.socket() s.connect((br.ip, int(br.port))) s.sendall(branchmsg.SerializeToString()) self.branch_connections.append(s) sleep(10)
def initialize_bank(money, branches_file): with open(branches_file, 'r') as f: branches = [line.split() for line in f] branch_money = int(money / len(branches)) remainder = money - (branch_money * len(branches)) init = bank_pb2.InitBranch() init.balance = branch_money for branch_arr in branches: branch = init.all_branches.add() branch.name = branch_arr[0] branch.ip = branch_arr[1] branch.port = int(branch_arr[2]) message = bank_pb2.BranchMessage() message.init_branch.MergeFrom(init) for branch in message.init_branch.all_branches: print(branch.name) print(branch.ip) print(branch.port) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((branch.ip, branch.port)) sockets.append((sock, branch.name)) if remainder == 0: message.init_branch.balance = branch_money message_socket(sock, message) else: message.init_branch.balance = branch_money + remainder message_socket(sock, message) remainder = 0
def send_money_to_branches(total_balance, branches): balance_per_branch = int(total_balance) / len(branches) print "Sending " + str( balance_per_branch) + " per branch to these branches: " + str( [x[0] for x in branches]) # Generate and populate the init_branch message init_branch_msg = bank_pb2.InitBranch() init_branch_msg.balance = balance_per_branch for branch in branches: pb_branch = init_branch_msg.all_branches.add() pb_branch.name = branch[0] pb_branch.ip = branch[1] pb_branch.port = int(branch[2]) pb_msg = bank_pb2.BranchMessage() pb_msg.init_branch.CopyFrom(init_branch_msg) branch_sockets = [] # Establish connections to all branches and send them the init_branch message. # Also record the socket objects created which will be used later to send snapshot messages. for branch in branches: client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect((branch[1], int(branch[2]))) client_socket.send(pb_msg.SerializeToString()) branch_sockets.append((client_socket, branch[0])) return branch_sockets
def initbranch(self): try: num_of_branches = sum(1 for line in open(self.FILENAME)) except: print "EXCEPTION ! Error opening input file" sys.exit(0) if num_of_branches < 2 : print "EXCEPTION ! Input file should contain at least 2 branches" sys.exit(0) try: balance = int ( self.TOTAL_BALANCE ) / int ( num_of_branches ) except: print "EXCEPTION ! Input file expty" sys.exit(0) with open(self.FILENAME, 'rU') as f: for line in f: branchname,ipaddress,portnumber = line.split(" ") sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) HOST, PORT = ipaddress, int(portnumber) init_branch_msg = bank_pb2.InitBranch() init_branch_msg.balance = int(balance) with open(self.FILENAME, 'rU') as f: for line in f: branchname1,ipaddress1,portnumber1 = line.split(" ") pb_branch = init_branch_msg.all_branches.add() pb_branch.name = branchname1 pb_branch.ip = ipaddress1 pb_branch.port = int(portnumber1) branchUpdateMessage = bank_pb2.InitBranch.Branch() branchUpdateMessage.name = branchname branchUpdateMessage.ip = ipaddress branchUpdateMessage.port = int(portnumber) pb_msg = bank_pb2.BranchMessage() pb_msg.init_branch.CopyFrom(init_branch_msg) encoded = pb_msg.SerializeToString() self.socket_obj.send_message_Protobuf(HOST , PORT, encoded) pb_message = bank_pb2.BranchMessage() pb_message.ParseFromString(encoded) self.list = pb_message.init_branch.all_branches list1 = [] for item in self.list: list1.append(item) self.list = list1
BRANCH_LIST.append(d) if __name__ == '__main__': parseBranchDetails(str(sys.argv[2])) snap_id = 0 try: while True: if len(SNAPSHOTS) == 0: remaining = int(sys.argv[1]) % len(BRANCH_LIST) for branch in BRANCH_LIST: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print "Initializing Branch: %s\n" % str(branch["name"]) s.connect((branch['ip'], branch['port'])) initMsg = bank_pb2.InitBranch() if remaining == 0: initMsg.balance = int(sys.argv[1]) / len(BRANCH_LIST) else: initMsg.balance = int( sys.argv[1]) / len(BRANCH_LIST) + remaining remaining = 0 for b in BRANCH_LIST: branches = initMsg.all_branches.add() branches.name = b['name'] branches.ip = b['ip'] branches.port = b['port'] msg = bank_pb2.BranchMessage() msg.init_branch.CopyFrom(initMsg)
data = line.strip().split(" ") d={} d["name"] = data[0] d["ip"]=data[1] d["port"]=int(data[2]) branhList.append(d) #print branhList while True: if initcount == 0: divide_Balance = int(sys.argv[1]) / len(branhList) for branch in branhList: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) logger.debug("Initializing Branch:" + str(branch["name"])) s.connect((branch['ip'], branch['port'])) init_branch = bank_pb2.InitBranch() init_branch.balance = divide_Balance for branchobj in branhList: branches = init_branch.all_branches.add() branches.name = branchobj['name'] branches.ip = branchobj['ip'] branches.port = branchobj['port'] message = bank_pb2.BranchMessage() message.init_branch.CopyFrom(init_branch) s.sendall(pickle.dumps(message)) s.close() initcount = 1 time.sleep(10)
def main(): branch_names = [] branch_ip = [] branch_port = [] snapshot_count = 1 f1 = open(sys.argv[2], 'r') for f in f1: columns = f.split(' ') columns = [c.strip() for c in columns] branch_names.append(columns[0]) branch_ip.append(columns[1]) branch_port.append(columns[2]) f1.close() init_Branch = bank_pb2.InitBranch() init_Branch.balance = int(sys.argv[1]) / len(branch_names) all_branches = [None] * len(branch_names) for i in range(len(branch_names)): all_branches[i] = bank_pb2.InitBranch.Branch( ) #init_Branch.all_branches.add() all_branches[i].name = branch_names[i] all_branches[i].ip = branch_ip[i] all_branches[i].port = int(branch_port[i]) init_Branch.all_branches.extend(all_branches) branch_Message = bank_pb2.BranchMessage() branch_Message1 = bank_pb2.BranchMessage() branch_Message.init_branch.balance = init_Branch.balance branch_Message.init_branch.all_branches.extend(init_Branch.all_branches) for i in range(len(branch_names)): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #out of loop s.connect((branch_ip[i], int(branch_port[i]))) s.send(branch_Message.SerializeToString()) s.close() while True: sleep(1) branch_number = random.randint(0, len(branch_names) - 1) init_snapshot_message = bank_pb2.InitSnapshot() init_snapshot_message.snapshot_id = snapshot_count branch_Message.init_snapshot.snapshot_id = init_snapshot_message.snapshot_id s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((branch_ip[branch_number], int(branch_port[branch_number]))) s.send(branch_Message.SerializeToString()) s.close() sleep(2) retrieve_snapshot_message = bank_pb2.RetrieveSnapshot() retrieve_snapshot_message.snapshot_id = snapshot_count branch_Message.retrieve_snapshot.snapshot_id = retrieve_snapshot_message.snapshot_id for i in range(len(branch_names)): br_n = [] for j in range(len(branch_names)): if i != j: br_n.append(branch_names[j]) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #out of loop s.connect((branch_ip[i], int(branch_port[i]))) s.send(branch_Message.SerializeToString()) data = s.recv(1024) branch_Message1.ParseFromString(data) print "\nSnapshot ID:", branch_Message1.return_snapshot.local_snapshot.snapshot_id print branch_names[ i] + ":", branch_Message1.return_snapshot.local_snapshot.balance count = 0 for f in branch_Message1.return_snapshot.local_snapshot.channel_state: print br_n[count] + "->" + branch_names[i] + ":" + str(f) count += 1 s.close() snapshot_count += 1
self.mutex.release() req = msg.SerializeToString() sock.send(req) # print("sent!") sock.close() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) time.sleep(5) if len(sys.argv) != 3: print("Usage:", sys.argv[0], "[BRANCH_NAME] [PORT]") sys.exit(-1) port = int(sys.argv[2]) branch_name = sys.argv[1] branch = branch('localhost', port, branch_name) init = bank_pb2.InitBranch() br_msg = bank_pb2.BranchMessage() listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listen_socket.bind(("", port)) listen_socket.listen(2) con, ip = listen_socket.accept() br_msg.ParseFromString(con.recv(8000)) branch.initBranch(br_msg.init_branch) #print(branch.number_of_branches) br_list = branch.branches_list client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) snapId = 0 # # threading.Thread(target=Server, args=(listen_socket,)).start()
b_ports = [] r_counter = 0 # Parse branches.txt with open(branches, 'r') as b: for line in b: b_counter = b_counter + 1 words = line.split() b_names.append(words[0]) b_ips.append(words[1]) b_ports.append(int(words[2])) # Create the InitBranch message amount = math.floor(total_money / b_counter) d = total_money - int(amount) * b_counter init_msg = bank_pb2.InitBranch() init_msg.balance = int(amount) # Populate its fields for i in range(0, b_counter): branch = init_msg.all_branches.add() branch.name = b_names[i] branch.ip = b_ips[i] branch.port = b_ports[i] branch = init_msg.all_branches.add() branch.name = 'controller' branch.ip = ip branch.port = port # Wrap it inside BranchMessage object