-
Notifications
You must be signed in to change notification settings - Fork 2
/
serverfactory.py
executable file
·116 lines (84 loc) · 3.38 KB
/
serverfactory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__ = 'Tzury Bar Yochay'
__version__ = '0.1'
__license__ = 'GPLv3'
import sys, threading, time
import logging, cPickle, exceptions
from threading import Thread
from twisted.internet.protocol import (
Protocol, DatagramProtocol, Factory, ServerFactory
)
from twisted.internet import reactor
import session
from utils import Storage
from logger import log
from logging import makeLogRecord, getLogger
from logging.config import fileConfig
from logging.handlers import DEFAULT_TCP_LOGGING_PORT
class TCPServerPrtocol(Protocol):
dataReceivedHandler = session.recv_msg
def connectionMade(self):
self.factory.echoers.append(self)
log.info('tcp_connection from %s' % repr(self.transport.client))
def dataReceived(self, data):
if not (host, port) in self.factory.echoers:
self.factory.echoers.append((host, port))
host, port = self.transport.client
self.dataReceivedHandler((host, port), data)
def connectionLost(self, reason):
#todo: -> remove this client form the session_ctx_table
log.info('connection Lost')
self.factory.echoers.remove(self)
class TCPServerFactory(ServerFactory):
protocol = TCPServerPrtocol
echoers = []
def _broadcast(self, clients, data):
for client in clients:
client.transport.write(data)
def send_others(self, (host, port), data):
self._broadcast([echo for echo in self.echoers
if echo.transport.client != (host, port)], data)
def send_all(self, data):
self._broadcast(self.echoers, data)
def send_to(self, (host, port), data):
for e in self.echoers:
if e.transport.client == (host, port):
#print 'TCPServerFactory.send_to', (host, port), repr(data)
e.transport.write(data)
return True
def connected_to(self, (host, port)):
for e in self.echoers:
if e.transport.client == (host, port):
return True
class UDPServer(DatagramProtocol):
echoers = []
dataReceivedHandler = session.recv_msg
def startProtocol(self):
pass
def datagramReceived(self, data, (host, port)):
if not (host, port) in self.echoers:
self.echoers.append((host, port))
self.dataReceivedHandler((host, port), data)
def send_all(self, data):
for (host, port) in self.echoers:
self.send_to((host, port), data)
def send_to(self, (host, port), data):
if self.connected_to((host, port)):
self.transport.write(data, (host, port))
def connected_to(self, (host, port)):
return (host, port) in self.echoers
def serve(listeners):
starters = {
'tcp': TCPServerFactory,
'udp': UDPServer }
reactor_listen = {
'tcp': reactor.listenTCP,
'udp': reactor.listenUDP }
for proto, port in listeners:
starter = starters[proto]()
reactor_listen[proto](port, starter)
session.servers_pool.add(proto, starter)
log.info( 'serving %s on port %s' % (proto, port))
#~ reactor.listenTCP(logging.DEFAULT_TCP_LOGGING_PORT, BroadcastFactory())
reactor.run(installSignalHandlers=0)