This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
peer.py
55 lines (40 loc) · 1.45 KB
/
peer.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from twisted.protocols.basic import NetstringReceiver
from twisted.internet.protocol import Factory, ClientFactory
from twisted.internet import reactor
from pydispatch import dispatcher
class ClipboardProtocol(NetstringReceiver):
def __init__( self ):
self.address = None
def stringReceived( self, line ):
dispatcher.send( signal='contents-received', sender=self.factory.peer, data=line )
def makeConnection( self, transport ):
NetstringReceiver.makeConnection( self, transport )
address = transport.getPeer()
self.address = address.host
self.factory.peer.connections[self.address] = self
def connectionLost( self, reason ):
try:
del self.factory.peer.connections[self.address]
except KeyError:
pass
class ClipboardProtocolServerFactory(Factory):
protocol = ClipboardProtocol
def __init__( self, peer ):
self.peer = peer
class ClipboardProtocolClientFactory(ClientFactory):
protocol = ClipboardProtocol
def __init__( self, peer ):
self.peer = peer
class ClipboardPeer(object):
def __init__( self ):
self.connections = {}
def start( self ):
reactor.listenTCP( 8123, ClipboardProtocolServerFactory( self ) )
def connectTo( self, address ):
if address not in self.connections:
reactor.connectTCP( address, 8123, ClipboardProtocolClientFactory( self ) )
def sendClipboardContents( self, data ):
for address, protocol in self.connections.items():
protocol.sendString( data )