Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit f3ac47b

Browse files
committed
add redisprogram and bump package version
1 parent 3c5ae38 commit f3ac47b

File tree

5 files changed

+66
-8
lines changed

5 files changed

+66
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ node1.send('hi alice!')
4747
python3 examples/lan_chat.py
4848
```
4949

50-
**To simulate a small network topology with 6 nodes:**
50+
**Simulate a small network topology with 6 nodes:**
5151

5252
```bash
5353
python3 examples/small_network.py
5454
```
5555

56-
**To simulate a larger network with randomized connections between nodes:**
56+
**Simulate a larger network with randomized connections between nodes:**
5757

5858
```bash
5959
python3 examples/large_network.py

mesh/programs.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import os
23
import threading
34
from time import sleep
45

@@ -61,6 +62,7 @@ def recv(self, packet, interface):
6162
def R(pattern):
6263
return re.compile(pattern)
6364

65+
6466
class RoutedProgram(BaseProgram):
6567
"""Base program which easily routes messages to handler functions.
6668
@@ -96,3 +98,60 @@ def send(self, message, interface):
9698
self.node.log('\n> [SENT] %s' % line)
9799
packet = bytes(line, 'utf-8') if type(line) is str else line
98100
self.node.send(packet, interface)
101+
102+
103+
class RedisProgram(BaseProgram):
104+
"""
105+
A program which places all incoming an outgoing packets into a redis queue.
106+
The keys used for the queue can be passed in, these are the defaults:
107+
db: redis://127.0.0.1/0
108+
in queue: node-{pid}-recv
109+
out queue: node-{pid}-send
110+
"""
111+
def __init__(self, node, recv_key=None, send_key=None, redis_conf=None):
112+
super(RedisProgram, self).__init__(node)
113+
import redis
114+
pid = os.getpid()
115+
self.recv_key = recv_key or 'node-{}-recv'.format(pid)
116+
self.send_key = send_key or 'node-{}-send'.format(pid)
117+
self.nodeq = redis.Redis(**(redis_conf or {
118+
'host': '127.0.0.1',
119+
'port': 6379,
120+
'db': 0,
121+
}))
122+
123+
def run(self):
124+
print('[√] Redis program is buffering IO to db:{0} keys:{1} & {2}.'.format(
125+
0, self.recv_key, self.send_key))
126+
127+
while self.keep_listening:
128+
for interface in self.node.interfaces:
129+
if self.get_recvs(interface):
130+
continue
131+
if self.put_sends():
132+
continue
133+
134+
sleep(0.01)
135+
136+
def recv(self, packet, interface):
137+
print('[IN]: {}'.format(packet))
138+
self.nodeq.rpush(self.recv_key, packet)
139+
140+
def send(self, packet, interface=None):
141+
print('[OUT]: {}'.format(packet))
142+
self.node.send(packet, interface)
143+
144+
def get_recvs(self, interface):
145+
try:
146+
msg = self.node.inq[interface].get(timeout=0)
147+
self.recv(msg, interface)
148+
return True
149+
except Empty:
150+
return False
151+
152+
def put_sends(self):
153+
out = self.nodeq.rpop(self.send_key)
154+
if out:
155+
self.send(out)
156+
return True
157+
return False

mesh_networking.egg-info/PKG-INFO

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Metadata-Version: 1.1
22
Name: mesh-networking
3-
Version: 0.0.2
3+
Version: 0.0.3
44
Summary: A library for creating flexible network topologies
55
Home-page: https://github.com/pirate/mesh-networking
66
Author: Nick Sweeting
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
mesh

setup.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
from setuptools import setup, find_packages
2-
1+
from setuptools import setup
32

43
SHORT_DESC = 'A library for creating flexible network topologies'
54
LONG_DESC = 'This library helps you test large networks of nodes across physical and simulated links.'
65

76
setup(
87
name='mesh-networking',
9-
version='0.0.2',
8+
version='0.0.3',
109
description=SHORT_DESC,
1110
long_description=LONG_DESC,
1211

@@ -35,7 +34,7 @@
3534
],
3635
keywords='networking routing mesh osi scapy udp tcp iptables irc',
3736

38-
packages=find_packages('mesh', exclude=['misc', 'examples']),
37+
packages=['mesh'],
3938
test_suite='mesh.tests',
4039
install_requires=[],
4140
)

0 commit comments

Comments
 (0)