Skip to content

Commit

Permalink
Rearranged methods
Browse files Browse the repository at this point in the history
  • Loading branch information
HeinrichHartmann committed Dec 31, 2013
1 parent d05c2dd commit a690474
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 81 deletions.
4 changes: 2 additions & 2 deletions test/test_argparse.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import unittest

# use symlink in test directory
# ln -s ../zmqdump zmqdump.py
from zmqdump import *

class TestArgParse(unittest.TestCase):

parser = None

def setUp(self):
zdp = Zmqdump()
self.parser = zdp.parser
self.parser = setup_parser()

def tearDown(self):
pass
Expand Down
34 changes: 0 additions & 34 deletions test/test_sub.py

This file was deleted.

113 changes: 68 additions & 45 deletions zmqdump
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@
#
# zmqdump - dump zmq messages on a socket
#
# https://github.com/HeinrichHartmann/zmqdump
#
# @author: Heinrich Hartmann <derhein@gmail.com>
#
# https://github.com/HeinrichHartmann/zmqdump
#
# License: GPLv2
#

import argparse
import sys
import time
import sys
import zmq

DEBUG = 0

def main():
"""
zmqdump command line script
"""

global DEBUG

parser = setupParser()
parser = setup_parser()
conf = parser.parse_args()

if (conf.verbose):
DEBUG = 10;
DEBUG = 10

zdp = Zmqdump(conf)
zdp.loop()
Expand All @@ -34,83 +39,101 @@ class Zmqdump:
socket_type = None

# Does the socket receive or send messages?
isTalking = False;
is_talking = False


def __init__(self, conf):
self.setSocket(conf.socket_type)

self.setHwm(conf.hwm)
self.setPattern(conf.pattern)

method = "bind" if conf.bind else "connect"
self.initConnection(method, conf.endpoint)

time.sleep(conf.delay * 0.001)
"""
Constructor
Configuration options are translated into socket
configuration.
"""

def setPattern(self, pattern):
if (DEBUG): print "Setting subscription pattern '%s'" % pattern
if (self.socket_type == "SUB"):
self.socket.setsockopt(zmq.SUBSCRIBE, pattern)
self.setup_socket(conf.socket_type)

self.set_hwm(conf.hwm)

def initConnection(self, method, endpoint):
assert(method in ["bind", "connect"])
assert(type(endpoint) is str and "://" in endpoint)
if (DEBUG): print "%s socket to %s" % (method, endpoint)

if (method == "bind"):
self.socket.bind(endpoint)
elif (method == "connect"):
self.socket.connect(endpoint)
self.set_pattern(conf.pattern)

method = "bind" if conf.bind else "connect"
self.init_connection(method, conf.endpoint)

def setHwm(self, hwm):
assert(type(hwm) is int)
if (DEBUG): print "Setting hwm to " + str(hwm)
time.sleep(conf.delay * 0.001)

self.socket.setsockopt(zmq.RCVHWM, hwm)
self.socket.setsockopt(zmq.SNDHWM, hwm)

def setup_socket(self, socket_type):
"""
Create context and socket obejcts.
"""

def setSocket(self, socket_type):
if (DEBUG): print "Setup %s-socket" % (socket_type)
if (DEBUG): print "Setting up %s-socket" % (socket_type)

self.context = zmq.Context()

if (socket_type == "PUB"):
self.socket_type = "PUB"
self.socket = self.context.socket(zmq.PUB)
self.isTalking = True;
self.is_talking = True

elif (socket_type == "SUB"):
self.socket_type = "SUB"
self.socket = self.context.socket(zmq.SUB)
self.isTalking = False;
self.is_talking = False

elif (socket_type == "PUSH"):
self.socket_type = "PUSH"
self.socket = self.context.socket(zmq.PUSH)
self.isTalking = True;
self.is_talking = True

elif (socket_type == "PULL"):
self.socket_type = "PULL"
self.socket = self.context.socket(zmq.PULL)
self.isTalking = False;
self.is_talking = False

else:
raise Exception("socket type not supported: " + socket_type)


def set_hwm(self, hwm):
assert(type(hwm) is int)
if (DEBUG): print "Setting hwm to " + str(hwm)

self.socket.setsockopt(zmq.RCVHWM, hwm)
self.socket.setsockopt(zmq.SNDHWM, hwm)


def set_pattern(self, pattern):
if (DEBUG): print "Setting subscription pattern '%s'" % pattern
if (self.socket_type == "SUB"):
self.socket.setsockopt(zmq.SUBSCRIBE, pattern)


def init_connection(self, method, endpoint):
"""
Bind/Connect socket
"""
assert(method in ["bind", "connect"])
assert(type(endpoint) is str and "://" in endpoint)
if (DEBUG): print "%s socket to %s" % (method, endpoint)

if (method == "bind"):
self.socket.bind(endpoint)
elif (method == "connect"):
self.socket.connect(endpoint)


def loop(self):
if (self.isTalking):
self.sendLoop()
"""
Start listning/writing to socket.
"""
if (self.is_talking):
self.send_loop()
else:
self.printLoop()
self.print_loop()


def printLoop(self):
def print_loop(self):
if (DEBUG): print "Listening on socket."
while (True):
try:
Expand All @@ -119,7 +142,7 @@ class Zmqdump:
break


def sendLoop(self):
def send_loop(self):
if (DEBUG): print "Writing to socket."
for line in sys.stdin:
try:
Expand All @@ -128,7 +151,7 @@ class Zmqdump:
break


def setupParser():
def setup_parser():
parser = argparse.ArgumentParser(
prog = "zmqdump",
description = "dump zmq messages on a socket"
Expand Down

0 comments on commit a690474

Please sign in to comment.