def increment_using_rpc(current_node): # create a server socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # connect to the server socket s.connect(('localhost', 50001)) # Connects via socket to RPC peer node rpc = JSONRpc(s) # Get a RPC peer proxy object server = rpc.get_peer_proxy() # Serialize the graph as a dictionary # Dictionary structure # {"root_object_id": {"name": "root", # "level": 0, # "value": 0, # "children":["leaf1_object_id", "leaf1_object_id", "leaf2_object_id"] # }, # "leaf1_object_id": {..... # } ser_graph = object_to_dictionary(current_node) # Remote procedure call using the serialized tree and the root node name result = server.server_increment(ser_graph) # Close thr RPC. # Closes the socket 's' also rpc.close() # construct the tree using objects # from the serialized output received from the RPC current_node = dictionary_to_object(result) # return the root node return current_node
# minimalistic client example from # https://github.com/seprich/py-bson-rpc/blob/master/README.md#quickstart import socket from bsonrpc import JSONRpc from bsonrpc.exceptions import FramingError from bsonrpc.framing import (JSONFramingNetstring, JSONFramingNone, JSONFramingRFC7464) # Cut-the-corners TCP Client: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 50002)) rpc = JSONRpc(s, framing_cls=JSONFramingNone) server = rpc.get_peer_proxy() # Execute in server: result = server.swapper('Hello World!') # "!dlroW olleH" print(result) print(server.nop({1: [2, 3]})) rpc.close() # Closes the socket 's' also
Root_List[len(Root_List) - 1].val = Node_Dic[i]['val'] #Will return the last node in the List which is the root of the tree return Root_List[len(Root_List) - 1] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 50001)) rpc = JSONRpc(s, framing_cls=JSONFramingNone) server = rpc.get_peer_proxy() leaf1 = node("leaf1") leaf2 = node("leaf2") root = node("root", [leaf1, leaf1, leaf2]) print("graph before increment") root.show() Dict_Root = [] Dict_Root.append(({ 'name': root.name, 'val': root.val, 'children': ChildrenList(root) })) Dict_Root = Node_into_Dictionary(root, Dict_Root) # do this increment remotely: Incremented_Dict = server.increment(Dict_Root) print("graph after increment") root = Dictionary_into_Node(Incremented_Dict) root.show() rpc.close()