-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPv4Example.py
85 lines (60 loc) · 2.73 KB
/
IPv4Example.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
from LibPeer2.Networks.Advertisement import Advertisement
from LibPeer2.Networks.Receiption import Receiption
from LibPeer2.Networks.IPv4 import IPv4
from LibPeer2.Protocols.MX2 import MX2
from LibPeer2.Protocols.MX2.Packet import Packet
from LibPeer2.Protocols.STP import STP
import struct
import uuid
import os
class GiveFileIPv4:
def __init__(self, network, file_path):
self.muxer = MX2()
self.network = network
self.network.bring_up()
self.network.incoming_advertisment.subscribe(self.rx_advertisement)
self.muxer.register_network(self.network)
self.instance = self.muxer.create_instance("GiveFile")
self.transport = STP(self.muxer, self.instance)
self.path = file_path
self.peers = set()
self.instance.incoming_greeting.subscribe(self.rx_greeting)
self.transport.incoming_stream.subscribe(self.incoming)
self.network.advertise(self.instance.reference)
def rx_advertisement(self, adv: Advertisement):
if(adv.instance_reference not in self.peers):
self.muxer.inquire(self.instance, adv.instance_reference, [adv.peer_info])
def rx_greeting(self, origin):
self.peers.add(origin)
self.transport.initialise_stream(origin).subscribe(self.make_request)
def make_request(self, stream):
stream.reply.subscribe(self.reply)
print("Asking peer to gib file...")
stream.write(b"Gib file")
def reply(self, stream):
print("Peer gibs file...")
# Get file size
size = struct.unpack("!L", stream.read(4))[0]
# Save file
file = open(str(uuid.uuid4()), 'wb')
file.write(stream.read(size))
print("Done")
def incoming(self, stream):
print("I have a new stream")
if(stream.read(8) != b"Gib file"):
print("Peer did not ask me to gib file")
return
self.transport.initialise_stream(stream.origin, in_reply_to=stream.id).subscribe(self.send_file)
def send_file(self, stream):
print("Sending my file")
stream.write(struct.pack("!L", os.path.getsize(self.path)))
f = open(self.path, 'rb')
stream.write(f.read())
f.close()
if __name__ == "__main__":
n1 = IPv4("172.16.4.162", 8001)
n2 = IPv4("172.16.4.162", 8002)
p2 = GiveFileIPv4(n1, "/home/bbarrow/Music/Snail Mail - Lush/Snail Mail - Lush - 08 Full Control.flac")
p1 = GiveFileIPv4(n2, "/home/bbarrow/Music/Anders Enger Jensen - The Last Goddess OST/Anders Enger Jensen - The Last Goddess OST - 01 The Last Goddess Theme.flac")
#p1 = GiveFile(conduit, "/home/bbarrow/Pictures/Lame Party Music.png")
#p2 = GiveFile(conduit, "/home/bbarrow/Pictures/Lame Party Music.jpg")