This repository has been archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added simple tcp server for the queue, and created a tcp client (empty)
- Loading branch information
1 parent
54243a1
commit 51386c1
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
TCP client for the Chula message queue daemon | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#!/usr/bin/python | ||
|
||
""" | ||
Chula message queue daemon | ||
""" | ||
|
||
import os | ||
import socket | ||
import sys | ||
import thread | ||
|
||
from chula import collection | ||
from chula.queue import message | ||
|
||
localhost = socket.gethostname() | ||
localhost = 'localhost' | ||
config = collection.Collection() | ||
config.mqueue_db = 'sqlite:memory' | ||
|
||
class MessageQueueServer(object): | ||
def worker(self, client): | ||
chars_left = 1 | ||
msg = [''] | ||
msg_length = None | ||
|
||
for x in xrange(1000): | ||
chunk = client.recv(chars_left) | ||
if chunk == '': | ||
client.send('OK ') | ||
break | ||
|
||
if msg_length is None: | ||
if chunk == ':': | ||
try: | ||
msg_length = int(msg.pop()) | ||
chars_left = msg_length | ||
continue | ||
except ValueError: | ||
client.send('BAD') | ||
break | ||
else: | ||
msg[0] += chunk | ||
else: | ||
if chars_left > 1: | ||
chars_left -= len(chunk) | ||
msg.append(chunk) | ||
|
||
# Do something with the data | ||
print '[%s]' % ''.join(msg) | ||
queue = message.MessageQueue(config) | ||
queue.add('testing', 'payload', 'type') | ||
queue.close() | ||
|
||
client.shutdown(0) | ||
client.close() | ||
|
||
def start(self): | ||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
s.bind((localhost, 8080)) | ||
s.listen(5) | ||
|
||
# Serve forever | ||
while True: | ||
try: | ||
(clientsocket, address) = s.accept() | ||
thread.start_new_thread(self.worker, (clientsocket,)) | ||
except KeyboardInterrupt: | ||
break | ||
|
||
s.shutdown(0) | ||
s.close() | ||
|
||
if __name__ == '__main__': | ||
print 'Running with pid: %s' % os.getpid() | ||
daemon = sys.argv[0].rsplit('/', 1)[-1] | ||
#pid = open('/var/run/%s.pid' % daemon, 'w') | ||
#pid.write(str(os.getpid())) | ||
#pid.close() | ||
|
||
server = MessageQueueServer() | ||
server.start() |