dayanara 0.1.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dayanara-0.1.3/LICENCE +21 -0
- dayanara-0.1.3/PKG-INFO +68 -0
- dayanara-0.1.3/README.md +55 -0
- dayanara-0.1.3/pyproject.toml +21 -0
- dayanara-0.1.3/setup.cfg +4 -0
- dayanara-0.1.3/src/dayanara/__init__.py +3 -0
- dayanara-0.1.3/src/dayanara/core.py +154 -0
- dayanara-0.1.3/src/dayanara/main.py +32 -0
- dayanara-0.1.3/src/dayanara/msg_types.py +6 -0
- dayanara-0.1.3/src/dayanara/network.py +152 -0
- dayanara-0.1.3/src/dayanara/olaf.py +95 -0
- dayanara-0.1.3/src/dayanara/peer.py +28 -0
- dayanara-0.1.3/src/dayanara/test/test.dayanara.py +38 -0
- dayanara-0.1.3/src/dayanara/test/test.network.py +0 -0
- dayanara-0.1.3/src/dayanara/test/test.olaf.py +0 -0
- dayanara-0.1.3/src/dayanara/test/test.peer.py +0 -0
- dayanara-0.1.3/src/dayanara/test/test_core.py +0 -0
- dayanara-0.1.3/src/dayanara.egg-info/PKG-INFO +68 -0
- dayanara-0.1.3/src/dayanara.egg-info/SOURCES.txt +19 -0
- dayanara-0.1.3/src/dayanara.egg-info/dependency_links.txt +1 -0
- dayanara-0.1.3/src/dayanara.egg-info/top_level.txt +1 -0
dayanara-0.1.3/LICENCE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Olaf Pedersen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
dayanara-0.1.3/PKG-INFO
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dayanara
|
|
3
|
+
Version: 0.1.3
|
|
4
|
+
Summary: A lightweight peer-to-peer networking protocol implementation in Python with bootstrap server support for peer discovery and room-based connections.
|
|
5
|
+
Author: Olaf Pedersen
|
|
6
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Requires-Python: >=3.8
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENCE
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# Dayanara
|
|
16
|
+
|
|
17
|
+
Dayanara is a lightweight peer-to-peer networking library for Python that allows
|
|
18
|
+
peers to discover each other using bootstrap servers and communicate inside rooms.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install dayanara
|
|
24
|
+
````
|
|
25
|
+
|
|
26
|
+
## Basic usage
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from dayanara import Dayanara
|
|
30
|
+
|
|
31
|
+
d = Dayanara()
|
|
32
|
+
d.join("room1")
|
|
33
|
+
|
|
34
|
+
d.send("Hola")
|
|
35
|
+
msg = d.receive()
|
|
36
|
+
print(msg)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## API
|
|
40
|
+
|
|
41
|
+
### Dayanara
|
|
42
|
+
|
|
43
|
+
#### Dayanara(bootstraps=None, debug=False)
|
|
44
|
+
|
|
45
|
+
Create a new Dayanara instance.
|
|
46
|
+
|
|
47
|
+
* `bootstraps`: list of bootstrap servers (optional)
|
|
48
|
+
* `debug`: enable debug output (default: False)
|
|
49
|
+
|
|
50
|
+
#### join(room: str)
|
|
51
|
+
|
|
52
|
+
Join a room and start peer discovery.
|
|
53
|
+
|
|
54
|
+
#### send(data)
|
|
55
|
+
|
|
56
|
+
Send data to all connected peers.
|
|
57
|
+
|
|
58
|
+
#### receive()
|
|
59
|
+
|
|
60
|
+
Block until a message is received and return it.
|
|
61
|
+
|
|
62
|
+
#### peers_list()
|
|
63
|
+
|
|
64
|
+
Return the list of known peers.
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
MIT
|
dayanara-0.1.3/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
|
|
2
|
+
# Dayanara
|
|
3
|
+
|
|
4
|
+
Dayanara is a lightweight peer-to-peer networking library for Python that allows
|
|
5
|
+
peers to discover each other using bootstrap servers and communicate inside rooms.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install dayanara
|
|
11
|
+
````
|
|
12
|
+
|
|
13
|
+
## Basic usage
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from dayanara import Dayanara
|
|
17
|
+
|
|
18
|
+
d = Dayanara()
|
|
19
|
+
d.join("room1")
|
|
20
|
+
|
|
21
|
+
d.send("Hola")
|
|
22
|
+
msg = d.receive()
|
|
23
|
+
print(msg)
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## API
|
|
27
|
+
|
|
28
|
+
### Dayanara
|
|
29
|
+
|
|
30
|
+
#### Dayanara(bootstraps=None, debug=False)
|
|
31
|
+
|
|
32
|
+
Create a new Dayanara instance.
|
|
33
|
+
|
|
34
|
+
* `bootstraps`: list of bootstrap servers (optional)
|
|
35
|
+
* `debug`: enable debug output (default: False)
|
|
36
|
+
|
|
37
|
+
#### join(room: str)
|
|
38
|
+
|
|
39
|
+
Join a room and start peer discovery.
|
|
40
|
+
|
|
41
|
+
#### send(data)
|
|
42
|
+
|
|
43
|
+
Send data to all connected peers.
|
|
44
|
+
|
|
45
|
+
#### receive()
|
|
46
|
+
|
|
47
|
+
Block until a message is received and return it.
|
|
48
|
+
|
|
49
|
+
#### peers_list()
|
|
50
|
+
|
|
51
|
+
Return the list of known peers.
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
MIT
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "dayanara"
|
|
7
|
+
version = "0.1.3"
|
|
8
|
+
description = "A lightweight peer-to-peer networking protocol implementation in Python with bootstrap server support for peer discovery and room-based connections."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = { file = "LICENSE" }
|
|
12
|
+
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "Olaf Pedersen" }
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
classifiers = [
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Operating System :: OS Independent",
|
|
21
|
+
]
|
dayanara-0.1.3/setup.cfg
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
from .peer import Peer
|
|
2
|
+
from .olaf import Olaf
|
|
3
|
+
from .network import Network
|
|
4
|
+
from .msg_types import *
|
|
5
|
+
import queue
|
|
6
|
+
import time
|
|
7
|
+
import sys
|
|
8
|
+
import socket
|
|
9
|
+
|
|
10
|
+
class Core:
|
|
11
|
+
def __init__(self, bootstraps, debug):
|
|
12
|
+
self.bootstraps = bootstraps
|
|
13
|
+
self.app_queue = queue.Queue()
|
|
14
|
+
self.network_queue = queue.Queue()
|
|
15
|
+
self.network = Network(bootstraps=bootstraps)
|
|
16
|
+
self.binary = Olaf()
|
|
17
|
+
self.peer = Peer()
|
|
18
|
+
self.b_count = 0
|
|
19
|
+
self.debug = debug
|
|
20
|
+
|
|
21
|
+
def connect(self):
|
|
22
|
+
"""Thread que recibe mensajes"""
|
|
23
|
+
while True:
|
|
24
|
+
try:
|
|
25
|
+
message, addr = self.peer.socket_receive()
|
|
26
|
+
msg_type = message[0]
|
|
27
|
+
if self.debug: print(f"Mensaje recibido: {message}")
|
|
28
|
+
|
|
29
|
+
if msg_type == APP_R:
|
|
30
|
+
self.app_queue.put((message, addr))
|
|
31
|
+
|
|
32
|
+
elif msg_type == BOOTSTRAP_R:
|
|
33
|
+
self.bootstrap_res(message, addr)
|
|
34
|
+
|
|
35
|
+
elif msg_type == PING:
|
|
36
|
+
self.ping_res(message, addr)
|
|
37
|
+
|
|
38
|
+
elif msg_type == ROOM_FULL: # falta implementar logica de room full
|
|
39
|
+
self.room_full(message, addr)
|
|
40
|
+
|
|
41
|
+
else: continue
|
|
42
|
+
|
|
43
|
+
except socket.timeout:
|
|
44
|
+
# Ignorar timeouts silenciosamente
|
|
45
|
+
continue
|
|
46
|
+
except Exception as e:
|
|
47
|
+
# agregar logging luego
|
|
48
|
+
continue
|
|
49
|
+
|
|
50
|
+
def heart(self, room):
|
|
51
|
+
while True:
|
|
52
|
+
self.network.evaluate_state()
|
|
53
|
+
if self.debug: print(self.network.get_state())
|
|
54
|
+
|
|
55
|
+
if self.network.send_ping:
|
|
56
|
+
if self.debug: print('enviando ping')
|
|
57
|
+
self.peer.socket_send_all(
|
|
58
|
+
type=PING,
|
|
59
|
+
peers=self.network.get_other_peers(),
|
|
60
|
+
payload=''
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
if self.network.purge:
|
|
64
|
+
if self.debug: print('purgando')
|
|
65
|
+
self.network.delete_inactive()
|
|
66
|
+
|
|
67
|
+
if self.network.send_collector:
|
|
68
|
+
if self.debug: print('enviando collector')
|
|
69
|
+
target = self.network.bootstraps[self.b_count]
|
|
70
|
+
self.peer.socket_send(
|
|
71
|
+
type=PEER_COLLECTOR,
|
|
72
|
+
payload=room,
|
|
73
|
+
target_addr=target
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
if self.network.send_join:
|
|
77
|
+
if self.debug: print('enviando join')
|
|
78
|
+
peers = []
|
|
79
|
+
if self.network.self_addr != None:
|
|
80
|
+
peers = [self.network.self_addr]
|
|
81
|
+
target = self.network.bootstraps[self.b_count]
|
|
82
|
+
self.peer.socket_send(
|
|
83
|
+
type=JOIN_B,
|
|
84
|
+
peers=peers,
|
|
85
|
+
payload=room,
|
|
86
|
+
target_addr=target
|
|
87
|
+
)
|
|
88
|
+
time.sleep(3)
|
|
89
|
+
|
|
90
|
+
def app_send(self, data):
|
|
91
|
+
if data is None:
|
|
92
|
+
raise ValueError("Data is required")
|
|
93
|
+
self.peer.socket_send_all(type=APP_R, peers=self.network.get_other_peers(), payload=data)
|
|
94
|
+
|
|
95
|
+
def app_receive(self):
|
|
96
|
+
message, addr = self.app_queue.get()
|
|
97
|
+
if message is None:
|
|
98
|
+
return ''
|
|
99
|
+
msg_type, peers, payload = message
|
|
100
|
+
return payload
|
|
101
|
+
|
|
102
|
+
def signal_handler(self, sig, frame):
|
|
103
|
+
try:
|
|
104
|
+
self.peer.socket_send_all(type=PING, peers=self.network.get_other_peers(), payload='bye')
|
|
105
|
+
except Exception as e:
|
|
106
|
+
print(f"Error al enviar despedida: {e}")
|
|
107
|
+
|
|
108
|
+
self.peer.socket_close()
|
|
109
|
+
sys.exit(0)
|
|
110
|
+
|
|
111
|
+
def bootstrap_res(self, message, addr):
|
|
112
|
+
_ , peers, payload = message
|
|
113
|
+
|
|
114
|
+
# revisar si se codifica bien el payload
|
|
115
|
+
if self.network.self_addr is None:
|
|
116
|
+
self.network.add_self_addr(payload)
|
|
117
|
+
|
|
118
|
+
for peer in peers:
|
|
119
|
+
self.network.add_peer(peer)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def ping_res(self, message, addr):
|
|
123
|
+
msg_type, peers, payload = message
|
|
124
|
+
|
|
125
|
+
if payload == b'bye':
|
|
126
|
+
peer_to_remove = None
|
|
127
|
+
for peer in self.network.get_peers_list():
|
|
128
|
+
if peer[0] == addr[0] and peer[1] == addr[1]:
|
|
129
|
+
peer_to_remove = peer
|
|
130
|
+
break
|
|
131
|
+
|
|
132
|
+
if peer_to_remove:
|
|
133
|
+
self.network.remove_peer(peer_to_remove)
|
|
134
|
+
if self.debug: print(f"Peer {peer_to_remove[2]} removido")
|
|
135
|
+
else:
|
|
136
|
+
# ✅ PRIMERO: Actualizar timestamp del peer que ENVIÓ el mensaje
|
|
137
|
+
sender_peer = None
|
|
138
|
+
for peer in self.network.get_peers_list():
|
|
139
|
+
if peer[0] == addr[0] and peer[1] == addr[1]:
|
|
140
|
+
sender_peer = peer
|
|
141
|
+
break
|
|
142
|
+
|
|
143
|
+
if sender_peer:
|
|
144
|
+
self.network.update_ts(sender_peer)
|
|
145
|
+
|
|
146
|
+
# ✅ SEGUNDO: Agregar/actualizar peers de la lista
|
|
147
|
+
for peer in peers:
|
|
148
|
+
self.network.add_peer(peer)
|
|
149
|
+
self.network.update_ts(peer)
|
|
150
|
+
|
|
151
|
+
def room_full(self, message, addr):
|
|
152
|
+
if self.debug: print(message)
|
|
153
|
+
|
|
154
|
+
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from .core import Core
|
|
2
|
+
import signal
|
|
3
|
+
import threading
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
class Dayanara(Core):
|
|
7
|
+
def __init__(self, bootstraps=[['127.0.0.1', 5000], ['127.0.0.1', 5001]], debug=False):
|
|
8
|
+
super().__init__(bootstraps, debug)
|
|
9
|
+
|
|
10
|
+
def join(self, room):
|
|
11
|
+
# ✅ Mover signal handler aquí (se ejecuta en thread principal)
|
|
12
|
+
# signal.signal(signal.SIGINT, self.signal_handler)
|
|
13
|
+
|
|
14
|
+
threading.Thread(target=self.connect, daemon=True).start()
|
|
15
|
+
threading.Thread(target=self.heart, args=(room,), daemon=True).start()
|
|
16
|
+
|
|
17
|
+
# # Mantener el programa vivo
|
|
18
|
+
# try:
|
|
19
|
+
# while True:
|
|
20
|
+
# time.sleep(1)
|
|
21
|
+
# except KeyboardInterrupt:
|
|
22
|
+
# print("Cerrando...")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def send(self, data):
|
|
26
|
+
self.app_send(data)
|
|
27
|
+
|
|
28
|
+
def receive(self):
|
|
29
|
+
return self.app_receive()
|
|
30
|
+
|
|
31
|
+
def peers_list(self):
|
|
32
|
+
return self.network.get_peers_list()
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import queue
|
|
3
|
+
import threading
|
|
4
|
+
|
|
5
|
+
class State:
|
|
6
|
+
def __init__(self):
|
|
7
|
+
self.send_join = True
|
|
8
|
+
self.send_collector = False
|
|
9
|
+
self.send_ping = False
|
|
10
|
+
self.purge = False
|
|
11
|
+
|
|
12
|
+
class Network(State):
|
|
13
|
+
def __init__(self, timeout=15, ip='0.0.0.0', port=0, bootstraps=None):
|
|
14
|
+
super().__init__()
|
|
15
|
+
self.self_addr = None
|
|
16
|
+
self.bootstraps = bootstraps # lista de boostraps publicos activos
|
|
17
|
+
|
|
18
|
+
# Registro de nodos
|
|
19
|
+
self.peers_in_room = []
|
|
20
|
+
self.peers_life = {}
|
|
21
|
+
|
|
22
|
+
self.write_queue = queue.Queue()
|
|
23
|
+
self.timeout = timeout
|
|
24
|
+
|
|
25
|
+
self._writer_thread = threading.Thread(
|
|
26
|
+
target=self.write_thread,
|
|
27
|
+
daemon=True
|
|
28
|
+
).start()
|
|
29
|
+
|
|
30
|
+
# ------------------------------------------------threading pipeline------------------------------------------
|
|
31
|
+
def write_thread(self):
|
|
32
|
+
while True:
|
|
33
|
+
|
|
34
|
+
cmd, peer = self.write_queue.get()
|
|
35
|
+
|
|
36
|
+
if cmd == 'add_self_addr':
|
|
37
|
+
self.self_addr = peer
|
|
38
|
+
if peer not in self.peers_in_room:
|
|
39
|
+
self.peers_in_room.append(peer)
|
|
40
|
+
self.peers_life[peer[2]] = time.time()
|
|
41
|
+
|
|
42
|
+
elif cmd == 'add_peer':
|
|
43
|
+
if peer not in self.peers_in_room:
|
|
44
|
+
self.peers_in_room.append(peer)
|
|
45
|
+
self.peers_life[peer[2]] = time.time()
|
|
46
|
+
|
|
47
|
+
elif cmd == 'remove_peer':
|
|
48
|
+
if peer in self.peers_in_room:
|
|
49
|
+
self.peers_in_room.remove(peer)
|
|
50
|
+
del self.peers_life[peer[2]]
|
|
51
|
+
|
|
52
|
+
elif cmd == 'delete_inactive':
|
|
53
|
+
current_time = time.time()
|
|
54
|
+
|
|
55
|
+
for peer_id, last_seen in list(self.peers_life.items()):
|
|
56
|
+
if current_time - last_seen > self.timeout:
|
|
57
|
+
# Buscar el peer completo por su ID
|
|
58
|
+
peer_addr = next((peer for peer in self.peers_in_room if peer[2] == peer_id), None)
|
|
59
|
+
if peer_addr:
|
|
60
|
+
self.peers_in_room.remove(peer_addr)
|
|
61
|
+
del self.peers_life[peer_addr[2]]
|
|
62
|
+
|
|
63
|
+
elif cmd == 'update_ts':
|
|
64
|
+
if peer and peer[2] in self.peers_life:
|
|
65
|
+
self.peers_life[peer[2]] = time.time()
|
|
66
|
+
|
|
67
|
+
else: continue # ignora silenciosamente
|
|
68
|
+
|
|
69
|
+
# ---------------------------------------------------WRITE-------------------------------------------------
|
|
70
|
+
|
|
71
|
+
def add_self_addr(self, public_addr: list) -> None:
|
|
72
|
+
self.write_queue.put(('add_self_addr', public_addr))
|
|
73
|
+
|
|
74
|
+
def add_peer(self, peer_addr: list) -> None:
|
|
75
|
+
self.write_queue.put(('add_peer', peer_addr))
|
|
76
|
+
|
|
77
|
+
def remove_peer(self, peer_addr: list) -> None:
|
|
78
|
+
self.write_queue.put(('remove_peer', peer_addr))
|
|
79
|
+
|
|
80
|
+
def delete_inactive(self) -> None:
|
|
81
|
+
self.write_queue.put(('delete_inactive', None))
|
|
82
|
+
|
|
83
|
+
def update_ts(self, peer_addr: list) -> None:
|
|
84
|
+
self.write_queue.put(('update_ts', peer_addr))
|
|
85
|
+
|
|
86
|
+
# ---------------------------------------------- READ-------------------------------------------------------
|
|
87
|
+
|
|
88
|
+
def get_peers_list(self) -> list: # Read
|
|
89
|
+
''' get the list of peers in the room: return [peer1,peer2,peer3,...]'''
|
|
90
|
+
return self.peers_in_room.copy()
|
|
91
|
+
|
|
92
|
+
def get_other_peers(self) -> list: # Read
|
|
93
|
+
''' get the list of other peers in the room'''
|
|
94
|
+
p_i_r_copy = self.peers_in_room.copy()
|
|
95
|
+
|
|
96
|
+
if self.self_addr is None:
|
|
97
|
+
return []
|
|
98
|
+
return [peer for peer in p_i_r_copy if peer[:2] != self.self_addr[:2]]
|
|
99
|
+
|
|
100
|
+
def min_id(self) -> bool: # Read
|
|
101
|
+
''' check if self_addr has the minimum id'''
|
|
102
|
+
p_i_r_copy = self.peers_in_room.copy()
|
|
103
|
+
|
|
104
|
+
if not p_i_r_copy:
|
|
105
|
+
return False
|
|
106
|
+
min_id = min(peer[2] for peer in p_i_r_copy)
|
|
107
|
+
return self.self_addr[2] == min_id
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
# ---------------------------------------------STATUS-----------------------------------------------
|
|
111
|
+
def evaluate_state(self) -> None:
|
|
112
|
+
p_i_r_copy = self.peers_in_room.copy()
|
|
113
|
+
# Calcular others directamente sin llamar a get_other_peers() para evitar deadlock
|
|
114
|
+
if self.self_addr is None:
|
|
115
|
+
others = []
|
|
116
|
+
else:
|
|
117
|
+
others = [peer for peer in p_i_r_copy if peer[:2] != self.self_addr[:2]]
|
|
118
|
+
has_others = len(others) > 0 # ✅ Criterio correcto
|
|
119
|
+
|
|
120
|
+
# Si hay OTROS peers (no solo yo)
|
|
121
|
+
if has_others:
|
|
122
|
+
self.purge = True
|
|
123
|
+
self.send_ping = True
|
|
124
|
+
self.send_join = False
|
|
125
|
+
|
|
126
|
+
# Solo si soy el peer con menor ID, enviar COLLECTOR al bootstrap
|
|
127
|
+
# Calcular min_id directamente sin llamar al método para evitar deadlock
|
|
128
|
+
if not p_i_r_copy or self.self_addr is None:
|
|
129
|
+
is_min_id = False
|
|
130
|
+
else:
|
|
131
|
+
min_id = min(peer[2] for peer in p_i_r_copy)
|
|
132
|
+
is_min_id = self.self_addr[2] == min_id
|
|
133
|
+
|
|
134
|
+
if is_min_id:
|
|
135
|
+
self.send_collector = True
|
|
136
|
+
else:
|
|
137
|
+
self.send_collector = False
|
|
138
|
+
|
|
139
|
+
# Si estoy solo
|
|
140
|
+
else:
|
|
141
|
+
self.send_join = True
|
|
142
|
+
self.send_ping = False
|
|
143
|
+
self.send_collector = False
|
|
144
|
+
self.purge = False
|
|
145
|
+
|
|
146
|
+
def get_state(self) -> dict:
|
|
147
|
+
return {
|
|
148
|
+
'send_collector': self.send_collector,
|
|
149
|
+
'send_join': self.send_join,
|
|
150
|
+
'send_ping': self.send_ping,
|
|
151
|
+
'purge': self.purge
|
|
152
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import struct
|
|
2
|
+
import socket
|
|
3
|
+
from .msg_types import *
|
|
4
|
+
|
|
5
|
+
class Olaf:
|
|
6
|
+
"""
|
|
7
|
+
Protocolo Olaf (orden de bytes):
|
|
8
|
+
[type (1B)] [peers (num_peers 2B + N*peer)] [payload (len 4B + data)]
|
|
9
|
+
peer = [IPv4 (4B) + port (2B) + id (4B)] = 10 bytes total
|
|
10
|
+
|
|
11
|
+
Convención de direcciones en esta API: listas
|
|
12
|
+
- peers_addr: [[ip:str, port:int, id:int], ...]
|
|
13
|
+
"""
|
|
14
|
+
#--------------------------------------pack------------------------------------------------
|
|
15
|
+
@staticmethod
|
|
16
|
+
def pack_addr(addr) -> bytes:
|
|
17
|
+
ip, port, peer_id = addr or ['0.0.0.0', 0, 0]
|
|
18
|
+
return struct.pack("!4sHI", socket.inet_aton(ip), int(port), int(peer_id)) # 10B
|
|
19
|
+
|
|
20
|
+
@staticmethod
|
|
21
|
+
def pack_peers(peers_addr) -> bytes:
|
|
22
|
+
peers = peers_addr or []
|
|
23
|
+
body = b"".join(Olaf.pack_addr(peer) for peer in peers)
|
|
24
|
+
return struct.pack("!H", len(peers)) + body # [num_peers][peers...]
|
|
25
|
+
|
|
26
|
+
@staticmethod
|
|
27
|
+
def pack_payload(payload) -> bytes:
|
|
28
|
+
if isinstance(payload, str):
|
|
29
|
+
payload = payload.encode("utf-8")
|
|
30
|
+
return struct.pack("!I", len(payload)) + payload # [len][data]
|
|
31
|
+
|
|
32
|
+
#--------------------------------------unpack------------------------------------------------
|
|
33
|
+
@staticmethod
|
|
34
|
+
def unpack_addr(data: bytes, offset: int):
|
|
35
|
+
ip = socket.inet_ntoa(data[offset:offset+4])
|
|
36
|
+
port = struct.unpack_from("!H", data, offset+4)[0]
|
|
37
|
+
peer_id = struct.unpack_from("!I", data, offset+6)[0]
|
|
38
|
+
return [ip, port, peer_id], offset + 10
|
|
39
|
+
|
|
40
|
+
@staticmethod
|
|
41
|
+
def unpack_peers(data: bytes, offset: int):
|
|
42
|
+
num_peers = struct.unpack_from("!H", data, offset)[0]
|
|
43
|
+
offset += 2
|
|
44
|
+
peers = []
|
|
45
|
+
for _ in range(num_peers):
|
|
46
|
+
addr, offset = Olaf.unpack_addr(data, offset)
|
|
47
|
+
peers.append(addr)
|
|
48
|
+
return peers, offset
|
|
49
|
+
|
|
50
|
+
@staticmethod
|
|
51
|
+
def unpack_payload(data: bytes, offset: int):
|
|
52
|
+
payload_len = struct.unpack_from("!I", data, offset)[0]
|
|
53
|
+
offset += 4
|
|
54
|
+
payload = data[offset:offset+payload_len]
|
|
55
|
+
return payload, offset + payload_len
|
|
56
|
+
|
|
57
|
+
#--------------------------------------encode------------------------------------------------
|
|
58
|
+
@staticmethod
|
|
59
|
+
def encode_msg(msg_type: int, peers_addr: list, payload: bytes | str = b"") -> bytes:
|
|
60
|
+
"""
|
|
61
|
+
Inputs:
|
|
62
|
+
- msg_type: int
|
|
63
|
+
- peers_addr: [["127.0.0.1", 12346, 1234567890], ["127.0.0.2", 12347, 1234567891]]
|
|
64
|
+
- payload: bytes o str (si es str se codifica utf-8)
|
|
65
|
+
"""
|
|
66
|
+
# [type][peers][payload]
|
|
67
|
+
if msg_type == BOOTSTRAP_R:
|
|
68
|
+
payload = Olaf.pack_addr(payload)
|
|
69
|
+
else:
|
|
70
|
+
payload = Olaf.pack_payload(payload)
|
|
71
|
+
|
|
72
|
+
type_block = struct.pack("!B", int(msg_type)) # [type] (1B)
|
|
73
|
+
peers_block = Olaf.pack_peers(peers_addr) # [peers] ([2B]+N*8B)
|
|
74
|
+
payload_block = payload # payload ya está empaquetado
|
|
75
|
+
return type_block + peers_block + payload_block
|
|
76
|
+
|
|
77
|
+
#--------------------------------------decode------------------------------------------------
|
|
78
|
+
@staticmethod
|
|
79
|
+
def decode_msg(data: bytes):
|
|
80
|
+
"""
|
|
81
|
+
Output (listas):
|
|
82
|
+
- msg_type: int
|
|
83
|
+
- peers: [["127.0.0.1", 12346, 1234567890], ["127.0.0.2", 12347, 1234567891]]
|
|
84
|
+
- payload: bytes
|
|
85
|
+
"""
|
|
86
|
+
msg_type = struct.unpack_from("!B", data, 0)[0]
|
|
87
|
+
offset = 1 # Después del byte de tipo
|
|
88
|
+
peers, offset = Olaf.unpack_peers(data, offset)
|
|
89
|
+
if msg_type == BOOTSTRAP_R:
|
|
90
|
+
payload, offset = Olaf.unpack_addr(data, offset)
|
|
91
|
+
return msg_type, peers, payload
|
|
92
|
+
else:
|
|
93
|
+
payload, offset = Olaf.unpack_payload(data, offset)
|
|
94
|
+
return msg_type, peers, payload
|
|
95
|
+
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import socket
|
|
2
|
+
from .olaf import Olaf
|
|
3
|
+
|
|
4
|
+
class Peer:
|
|
5
|
+
def __init__(self, ip='0.0.0.0', port=0):
|
|
6
|
+
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
7
|
+
self.sock.bind((ip, port))
|
|
8
|
+
self.binary = Olaf()
|
|
9
|
+
|
|
10
|
+
def socket_send(self, type=None, peers=[], payload='', target_addr=None):
|
|
11
|
+
data = self.binary.encode_msg(type, peers, payload)
|
|
12
|
+
self.sock.sendto(data, (target_addr[0], target_addr[1]))
|
|
13
|
+
|
|
14
|
+
def socket_send_all(self, type=None, peers=[], payload=''):
|
|
15
|
+
for peer in peers:
|
|
16
|
+
self.socket_send(type=type, peers=peers, payload=payload, target_addr=peer)
|
|
17
|
+
|
|
18
|
+
def socket_receive(self, timeout=1.0, buffer_size=1024):
|
|
19
|
+
self.sock.settimeout(timeout) # evita que el socket se bloquee
|
|
20
|
+
try:
|
|
21
|
+
data, addr = self.sock.recvfrom(buffer_size)
|
|
22
|
+
return self.binary.decode_msg(data), [addr[0], addr[1]]
|
|
23
|
+
except socket.timeout:
|
|
24
|
+
raise # Re-lanzar para que el caller sepa que hubo timeout
|
|
25
|
+
|
|
26
|
+
def socket_close(self):
|
|
27
|
+
self.sock.close()
|
|
28
|
+
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
import threading
|
|
4
|
+
|
|
5
|
+
sys.path.append(str(Path(__file__).resolve().parent.parent))
|
|
6
|
+
from main import Dayanara
|
|
7
|
+
|
|
8
|
+
# Setup
|
|
9
|
+
username = input("Nombre: ")
|
|
10
|
+
room = input("Sala: ")
|
|
11
|
+
|
|
12
|
+
# Instanciar
|
|
13
|
+
d = Dayanara()
|
|
14
|
+
|
|
15
|
+
# Join async (NO bloquea)
|
|
16
|
+
d.join(room)
|
|
17
|
+
|
|
18
|
+
# Thread para recibir
|
|
19
|
+
def recibir():
|
|
20
|
+
while True:
|
|
21
|
+
msg = d.receive()
|
|
22
|
+
if msg:
|
|
23
|
+
print(f"\r{msg}")
|
|
24
|
+
print(f"{username}: ", end='', flush=True)
|
|
25
|
+
print(d.conn_peers())
|
|
26
|
+
|
|
27
|
+
threading.Thread(target=recibir, daemon=True).start()
|
|
28
|
+
|
|
29
|
+
# Bucle para enviar
|
|
30
|
+
try:
|
|
31
|
+
while True:
|
|
32
|
+
mensaje = input(f"{username}: ")
|
|
33
|
+
if mensaje:
|
|
34
|
+
d.send(f"{username}: {mensaje}")
|
|
35
|
+
except KeyboardInterrupt:
|
|
36
|
+
print("\nCerrando...")
|
|
37
|
+
sys.exit(0)
|
|
38
|
+
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dayanara
|
|
3
|
+
Version: 0.1.3
|
|
4
|
+
Summary: A lightweight peer-to-peer networking protocol implementation in Python with bootstrap server support for peer discovery and room-based connections.
|
|
5
|
+
Author: Olaf Pedersen
|
|
6
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Requires-Python: >=3.8
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENCE
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# Dayanara
|
|
16
|
+
|
|
17
|
+
Dayanara is a lightweight peer-to-peer networking library for Python that allows
|
|
18
|
+
peers to discover each other using bootstrap servers and communicate inside rooms.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install dayanara
|
|
24
|
+
````
|
|
25
|
+
|
|
26
|
+
## Basic usage
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from dayanara import Dayanara
|
|
30
|
+
|
|
31
|
+
d = Dayanara()
|
|
32
|
+
d.join("room1")
|
|
33
|
+
|
|
34
|
+
d.send("Hola")
|
|
35
|
+
msg = d.receive()
|
|
36
|
+
print(msg)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## API
|
|
40
|
+
|
|
41
|
+
### Dayanara
|
|
42
|
+
|
|
43
|
+
#### Dayanara(bootstraps=None, debug=False)
|
|
44
|
+
|
|
45
|
+
Create a new Dayanara instance.
|
|
46
|
+
|
|
47
|
+
* `bootstraps`: list of bootstrap servers (optional)
|
|
48
|
+
* `debug`: enable debug output (default: False)
|
|
49
|
+
|
|
50
|
+
#### join(room: str)
|
|
51
|
+
|
|
52
|
+
Join a room and start peer discovery.
|
|
53
|
+
|
|
54
|
+
#### send(data)
|
|
55
|
+
|
|
56
|
+
Send data to all connected peers.
|
|
57
|
+
|
|
58
|
+
#### receive()
|
|
59
|
+
|
|
60
|
+
Block until a message is received and return it.
|
|
61
|
+
|
|
62
|
+
#### peers_list()
|
|
63
|
+
|
|
64
|
+
Return the list of known peers.
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
MIT
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
LICENCE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/dayanara/__init__.py
|
|
5
|
+
src/dayanara/core.py
|
|
6
|
+
src/dayanara/main.py
|
|
7
|
+
src/dayanara/msg_types.py
|
|
8
|
+
src/dayanara/network.py
|
|
9
|
+
src/dayanara/olaf.py
|
|
10
|
+
src/dayanara/peer.py
|
|
11
|
+
src/dayanara.egg-info/PKG-INFO
|
|
12
|
+
src/dayanara.egg-info/SOURCES.txt
|
|
13
|
+
src/dayanara.egg-info/dependency_links.txt
|
|
14
|
+
src/dayanara.egg-info/top_level.txt
|
|
15
|
+
src/dayanara/test/test.dayanara.py
|
|
16
|
+
src/dayanara/test/test.network.py
|
|
17
|
+
src/dayanara/test/test.olaf.py
|
|
18
|
+
src/dayanara/test/test.peer.py
|
|
19
|
+
src/dayanara/test/test_core.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dayanara
|