-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_microblog_ip.py
120 lines (99 loc) · 3.59 KB
/
4_microblog_ip.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
import json
from typing import List
import socket
import uuid
# Define the microblog post
class MicroblogPost:
def __init__(self, username: str, content: str):
self.username = username
self.content = content
# Define the P2P network node
class MicroblogNode:
def __init__(self, username: str, host: str, port: int):
self.node_id = str(uuid.uuid1()) # Generate a unique node ID
self.username = username
self.host = host
self.port = port
self.posts = [] # List of microblog posts
self.peers = [] # List of peer nodes
self.socket = None # Socket for communicating with peer nodes
# Open the socket and listen for incoming connections
def listen(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind((self.host, self.port))
self.socket.listen()
# Accept incoming connections
while True:
connection, address = self.socket.accept()
# Receive data from the incoming connection
data = connection.recv(1024)
# Deserialize the data
data = json.loads(data)
# Create a new microblog post from the received data
post = MicroblogPost(data['username'], data['content'])
# Add the post to the node's list of posts
self.add_post(post)
# Add a new microblog post to the node
def add_post(self, post: MicroblogPost):
self.posts.append(post)
# Get all microblog posts from the node
def get_posts(self) -> List[MicroblogPost]:
return self.posts
# Add a new peer node to the network
def add_peer(self, peer: 'MicroblogNode'):
self.peers.append(peer)
# Connect to a peer node and add it to the network
def connect_to_peer(self, peer: 'MicroblogNode'):
# Connect to the peer node's socket
peer_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
peer_socket.connect((peer.host, peer.port))
# Add the peer node to the network
self.add_peer(peer)
# Broadcast a microblog post to all connected peer nodes
def broadcast_post(self, post: MicroblogPost):
# Serialize the post data
post_data = json.dumps(post.__dict__)
# Loop over the connected peer nodes
for peer in self.peers:
# Send the post data to the peer node
peer.socket.send(post_data)
# Define the P2P network
class MicroblogNetwork:
def __init__(self):
self.nodes = [] # List of nodes in the network
# Add a new node to the network
def add_node(self, node: MicroblogNode):
self.nodes.append(node)
# Broadcast a microblog post to all nodes in the network
def broadcast_post(self, post: MicroblogPost):
for node in self.nodes:
node.broadcast_post(post)
# Define the main function
def main():
# Create a new P2P network
network = MicroblogNetwork()
# Get the IP address of the local machine
host = socket.gethostbyname(socket.gethostname())
# Create a new node and add it to the network
node1 = MicroblogNode('Alice', host, 8080)
network.add_node(node1)
# Start listening for incoming connections on the node
node1.listen()
# Create another node and add it to the network
node2 = MicroblogNode('Bob', host, 8081)
network.add_node(node2)
# Start listening for incoming connections on the node
node2.listen()
# Connect node2 to node1
node2.connect_to_peer(node1)
# Create a new microblog post and broadcast it to the network
post = MicroblogPost('Alice', 'Hello, world!')
network.broadcast_post(post)
# Get the posts from node1
posts = node1.get_posts()
# Print the posts
for post in posts:
print(f'{post.username}: {post.content}')
# Run the main function
if __name__ == '__main__':
main()