41 lines
1 KiB
Python
41 lines
1 KiB
Python
import enum
|
|
import socket
|
|
import threading
|
|
|
|
from typing import Callable
|
|
|
|
from nntp.tiny.newsgroup import Newsgroup
|
|
from nntp.tiny.session import Session
|
|
|
|
class ServerCapability(enum.Flag):
|
|
NONE = 0
|
|
AUTH = enum.auto()
|
|
POST = enum.auto()
|
|
|
|
class Server():
|
|
def __init__(self, connect_to_db: Callable):
|
|
self.connect_to_db = connect_to_db
|
|
self.capabilities = ServerCapability.NONE
|
|
self.newsgroups = dict()
|
|
|
|
self._init_newsgroups()
|
|
|
|
def _init_newsgroups(self):
|
|
db = self.connect_to_db()
|
|
|
|
for newsgroup in db.query(Newsgroup).each():
|
|
self.newsgroups[newsgroup.name.casefold()] = newsgroup
|
|
|
|
def run(self):
|
|
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
listener.bind(('localhost', 1190))
|
|
listener.listen()
|
|
|
|
while True:
|
|
sock, addr = listener.accept()
|
|
|
|
session = Session(self, sock)
|
|
thread = threading.Thread(target=session.handle)
|
|
thread.start()
|
|
|
|
listener.close()
|