2018-11-20 19:45:27 +00:00
|
|
|
import sys
|
|
|
|
import asyncio
|
|
|
|
import random
|
2021-08-19 02:59:04 +00:00
|
|
|
import syndicate
|
2021-08-19 19:50:21 +00:00
|
|
|
from syndicate import patterns as P, actor, dataspace
|
|
|
|
from syndicate.schema import simpleChatProtocol, gatekeeper, sturdy
|
2021-08-19 02:59:04 +00:00
|
|
|
from syndicate.during import During
|
2018-11-20 19:45:27 +00:00
|
|
|
|
2021-08-19 02:59:04 +00:00
|
|
|
Present = simpleChatProtocol.Present
|
|
|
|
Says = simpleChatProtocol.Says
|
2018-11-20 19:45:27 +00:00
|
|
|
|
2021-08-19 02:59:04 +00:00
|
|
|
conn_str = '<ws "ws://localhost:8001/">'
|
|
|
|
cap_str = '<ref "syndicate" [] #[pkgN9TBmEd3Q04grVG4Zdw==]>'
|
|
|
|
cap = sturdy.SturdyRef.decode(syndicate.parse(cap_str))
|
|
|
|
|
|
|
|
# sys.stderr.write(
|
|
|
|
# 'Usage: chat.py [ <tcp "HOST" PORT> | <ws "ws://HOST[:PORT]/"> | <unix "PATH"> ]\n')
|
|
|
|
# sys.exit(1)
|
|
|
|
|
|
|
|
me = 'user_' + str(random.randint(10, 1000))
|
2018-11-20 19:45:27 +00:00
|
|
|
|
2019-06-13 11:59:05 +00:00
|
|
|
_print = print
|
|
|
|
def print(*items):
|
|
|
|
_print(*items)
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2021-08-19 02:59:04 +00:00
|
|
|
def main_facet(turn, root_facet, ds):
|
|
|
|
print('main_facet', ds)
|
|
|
|
f = turn._facet
|
|
|
|
turn.publish(ds, Present(me))
|
2021-08-19 17:26:12 +00:00
|
|
|
|
2021-08-19 20:08:53 +00:00
|
|
|
@dataspace.during(turn, ds, P.rec('Present', P.CAPTURE))
|
2021-08-19 17:26:12 +00:00
|
|
|
def on_presence(turn, who):
|
|
|
|
print('%s joined' % (who,))
|
|
|
|
return lambda turn: print('%s left' % (who,))
|
|
|
|
|
2021-08-19 20:08:53 +00:00
|
|
|
@dataspace.on_message(turn, ds, P.rec('Says', P.CAPTURE, P.CAPTURE))
|
2021-08-19 17:26:12 +00:00
|
|
|
def on_says(turn, who, what):
|
|
|
|
print('%s says %r' % (who, what))
|
2018-11-20 19:45:27 +00:00
|
|
|
|
2021-08-19 19:50:21 +00:00
|
|
|
@turn.linked_task()
|
2021-08-19 17:26:12 +00:00
|
|
|
async def accept_input():
|
|
|
|
reader = asyncio.StreamReader()
|
2021-08-19 18:06:33 +00:00
|
|
|
await actor.find_loop().connect_read_pipe(lambda: asyncio.StreamReaderProtocol(reader), sys.stdin)
|
|
|
|
while line := (await reader.readline()).decode('utf-8'):
|
2021-08-19 18:04:38 +00:00
|
|
|
actor.Turn.external(f, lambda turn: turn.send(ds, Says(me, line.strip())))
|
2021-08-19 18:06:33 +00:00
|
|
|
actor.Turn.external(f, lambda turn: turn.stop(root_facet))
|
2018-11-20 19:45:27 +00:00
|
|
|
|
2021-08-19 02:59:04 +00:00
|
|
|
def main(turn):
|
|
|
|
root_facet = turn._facet
|
2021-08-19 16:59:48 +00:00
|
|
|
|
2021-08-19 19:50:21 +00:00
|
|
|
@During().add_handler
|
2021-08-19 16:59:48 +00:00
|
|
|
def handle_gatekeeper(turn, gk):
|
2021-08-19 19:50:21 +00:00
|
|
|
@During().add_handler
|
|
|
|
def handle_ds(turn, ds):
|
|
|
|
return turn.facet(lambda turn: main_facet(turn, root_facet, ds.embeddedValue))
|
|
|
|
turn.publish(gk.embeddedValue, gatekeeper.Resolve(cap, turn.ref(handle_ds)))
|
2018-11-20 19:45:27 +00:00
|
|
|
|
2021-08-19 02:59:04 +00:00
|
|
|
conn = syndicate.relay.TunnelRelay.from_str(turn,
|
|
|
|
conn_str,
|
2021-08-19 20:10:08 +00:00
|
|
|
gatekeeper_peer = turn.ref(handle_gatekeeper))
|
2018-11-20 19:45:27 +00:00
|
|
|
|
2021-08-19 02:59:04 +00:00
|
|
|
actor.start_actor_system(main, name = 'chat', debug = False)
|