This repository has been archived by the owner on Jun 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
229 additions
and
99 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 |
---|---|---|
@@ -1,18 +1,10 @@ | ||
sudo: required | ||
language: python | ||
|
||
env: | ||
- ARCH_TRAVIS_VERBOSE=1 | ||
|
||
arch: | ||
packages: | ||
- python3 | ||
- python-pip | ||
- mpv | ||
script: | ||
- python --version | ||
- sudo pip install coveralls | ||
- coverage run --source=fuocore setup.py test | ||
- coveralls | ||
python: | ||
- "3.5" | ||
- "3.6" | ||
|
||
script: | ||
- "curl -s https://raw.githubusercontent.com/mikkeloscar/arch-travis/master/arch-travis.sh | bash" | ||
- pip install coveralls | ||
- coverage run --source=fuocore setup.py test | ||
- coveralls |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +0,0 @@ | ||
from fuocore.daemon.main import run # noqa | ||
from fuocore.daemon.main import run_live_lyric_pubsub # noqa | ||
|
||
|
||
__all__ = ('run', 'run_live_lyric_pubsub') | ||
File renamed without changes.
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
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
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,119 @@ | ||
from collections import defaultdict | ||
import logging | ||
from threading import Thread | ||
|
||
from fuocore.daemon.thread_tcp_server import TcpServer | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class DeadSubscriber(Exception): | ||
pass | ||
|
||
|
||
class Subscriber(object): | ||
def __init__(self, addr, conn): | ||
self._addr = addr | ||
self._conn = conn | ||
|
||
def __eq__(self, obj): | ||
return self._addr == obj._addr | ||
|
||
def __hash__(self): | ||
return id(self._addr) | ||
|
||
|
||
def sendto_subscriber(subscriber, msg): | ||
try: | ||
subscriber._conn.send(bytes(msg, 'utf-8')) | ||
except BrokenPipeError: | ||
subscriber._conn.close() | ||
del subscriber | ||
raise DeadSubscriber | ||
|
||
|
||
class Gateway(object): | ||
def __init__(self): | ||
self.topics = set() | ||
self._relations = defaultdict(set) # {'topic': subscriber_set} | ||
|
||
def add_topic(self, topic): | ||
self.topics.add(topic) | ||
|
||
def remove_topic(self, topic): | ||
if topic in self.topics: | ||
self.topics.remove(topic) | ||
|
||
def link(self, topic, subscriber): | ||
self._relations[topic].add(subscriber) | ||
|
||
def unlink(self, topic, subscriber): | ||
if topic in self.topics and subscriber in self._relations[topic]: | ||
self._relations[topic].remove(subscriber) | ||
|
||
def remove_subscriber(self, subscriber): | ||
for topic in self.topics: | ||
if subscriber in self._relations[topic]: | ||
self._relations[topic].remove(subscriber) | ||
|
||
def publish(self, msg, topic): | ||
# NOTE: use queue? maybe. | ||
subscribers = self._relations[topic] | ||
for subscriber in subscribers: | ||
try: | ||
sendto_subscriber(subscriber, msg) | ||
except DeadSubscriber: | ||
# NOTE: need lock? | ||
self._relations[topic].remove(subscriber) | ||
break | ||
|
||
|
||
def handle(conn, addr, gateway, *args, **kwargs): | ||
""" | ||
NOTE: use tcp instead of udp because some operations need ack | ||
""" | ||
conn.sendall(b'OK feeluown live lyric\n') | ||
while True: | ||
try: | ||
s = conn.recv(1024).decode('utf-8').strip() | ||
if not s: | ||
conn.close() | ||
break | ||
except ConnectionResetError: | ||
logger.debug('Client close the connection.') | ||
break | ||
|
||
parts = s.split(' ') | ||
if len(parts) != 2: | ||
conn.send(b"Invalid command\n") | ||
continue | ||
cmd, topic = parts | ||
if cmd.lower() != 'sub': | ||
conn.send(bytes("Unknown command '{}'\n".format(cmd.lower()), 'utf-8')) | ||
continue | ||
if topic not in gateway.topics: | ||
conn.send(bytes("Unknown topic '{}'\n".format(topic), 'utf-8')) | ||
continue | ||
subscriber = Subscriber(addr, conn) | ||
gateway.link(topic, subscriber) | ||
break | ||
|
||
|
||
def run(host='0.0.0.0', port=23334): | ||
gateway = Gateway() | ||
server = TcpServer(host, port, handle_func=handle) | ||
Thread(target=server.run, args=(gateway,)).start() | ||
logger.info('run pubsub server in {host}:{port}'.format( | ||
host=host, port=port)) | ||
return gateway, server | ||
|
||
|
||
if __name__ == '__main__': | ||
import time | ||
gateway, server = run() | ||
print('pubsub is running.') | ||
gateway.add_topic('topic.live_lyric') | ||
while True: | ||
time.sleep(1) | ||
gateway.publish('miao\n', 'topic.live_lyric') |
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,33 @@ | ||
import logging | ||
import socket | ||
from threading import Thread | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TcpServer(object): | ||
def __init__(self, host, port, handle_func): | ||
self.host = host | ||
self.port = port | ||
self.handle_func = handle_func | ||
self.sock = None | ||
|
||
def run(self, *args, **kwargs): | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
sock.bind((self.host, self.port)) | ||
sock.listen() | ||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
self.sock = sock | ||
while True: | ||
try: | ||
conn, addr = sock.accept() | ||
except ConnectionError as e: | ||
logger.warning(e) | ||
break | ||
logger.info('{} connected.'.format(addr)) | ||
Thread(target=self.handle_func, args=(conn, addr, *args), | ||
kwargs=kwargs).start() | ||
|
||
def close(self): | ||
if self.sock is not None: | ||
self.sock.close() |
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
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
Empty file.
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
File renamed without changes.
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
File renamed without changes.
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
Oops, something went wrong.