Skip to content

Commit f874ac4

Browse files
committed
Add as_drivers/client_server alpha code.
1 parent 89f0079 commit f874ac4

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# flash.py Heartbeat code for simple uasyncio-based echo server
2+
3+
# Released under the MIT licence
4+
# Copyright (c) Peter Hinch 2019
5+
6+
import uasyncio as asyncio
7+
from sys import platform
8+
9+
10+
async def heartbeat(tms):
11+
if platform == 'pyboard': # V1.x or D series
12+
from pyb import LED
13+
led = LED(1)
14+
elif platform == 'esp8266':
15+
from machine import Pin
16+
led = Pin(2, Pin.OUT, value=1)
17+
elif platform == 'linux':
18+
return # No LED
19+
else:
20+
raise OSError('Unsupported platform.')
21+
while True:
22+
if platform == 'pyboard':
23+
led.toggle()
24+
elif platform == 'esp8266':
25+
led(not led())
26+
await asyncio.sleep_ms(tms)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# uclient.py Demo of simple uasyncio-based client for echo server
2+
3+
# Released under the MIT licence
4+
# Copyright (c) Peter Hinch 2019-2020
5+
6+
import usocket as socket
7+
import uasyncio as asyncio
8+
import ujson
9+
from heartbeat import heartbeat # Optional LED flash
10+
11+
server = '192.168.0.41'
12+
port = 8123
13+
14+
async def run():
15+
# Optional fast heartbeat to confirm nonblocking operation
16+
asyncio.create_task(heartbeat(100))
17+
sock = socket.socket()
18+
def close():
19+
sock.close()
20+
print('Server disconnect.')
21+
try:
22+
serv = socket.getaddrinfo(server, port)[0][-1]
23+
sock.connect(serv)
24+
except OSError as e:
25+
print('Cannot connect to {} on port {}'.format(server, port))
26+
sock.close()
27+
return
28+
while True:
29+
sreader = asyncio.StreamReader(sock)
30+
swriter = asyncio.StreamWriter(sock, {})
31+
data = ['value', 1]
32+
while True:
33+
try:
34+
swriter.write('{}\n'.format(ujson.dumps(data)))
35+
await swriter.drain()
36+
res = await sreader.readline()
37+
except OSError:
38+
close()
39+
return
40+
try:
41+
print('Received', ujson.loads(res))
42+
except ValueError:
43+
close()
44+
return
45+
await asyncio.sleep(2)
46+
data[1] += 1
47+
48+
try:
49+
asyncio.run(run())
50+
except KeyboardInterrupt:
51+
print('Interrupted') # This mechanism doesn't work on Unix build.
52+
finally:
53+
_ = asyncio.new_event_loop()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# userver.py Demo of simple uasyncio-based echo server
2+
3+
# Released under the MIT licence
4+
# Copyright (c) Peter Hinch 2019
5+
6+
import usocket as socket
7+
import uasyncio as asyncio
8+
import uselect as select
9+
import ujson
10+
from heartbeat import heartbeat # Optional LED flash
11+
12+
class Server:
13+
14+
async def run(self, port=8123):
15+
print('Awaiting client connection.')
16+
self.cid = 0
17+
asyncio.create_task(heartbeat(100))
18+
self.server = await asyncio.start_server(self.run_client, '0.0.0.0', port)
19+
while True:
20+
await asyncio.sleep(100)
21+
22+
async def run_client(self, sreader, swriter):
23+
self.cid += 1
24+
print('Got connection from client', self.cid)
25+
try:
26+
while True:
27+
res = await sreader.readline()
28+
if res == b'':
29+
raise OSError
30+
print('Received {} from client {}'.format(ujson.loads(res.rstrip()), self.cid))
31+
swriter.write(res)
32+
await swriter.drain() # Echo back
33+
except OSError:
34+
pass
35+
print('Client {} disconnect.'.format(self.cid))
36+
await sreader.wait_closed()
37+
print('Client {} socket closed.'.format(self.cid))
38+
39+
def close(self):
40+
print('Closing server')
41+
self.server.close()
42+
await self.server.wait_closed()
43+
print('Server closed')
44+
45+
server = Server()
46+
try:
47+
asyncio.run(server.run())
48+
except KeyboardInterrupt:
49+
print('Interrupted') # This mechanism doesn't work on Unix build.
50+
finally:
51+
server.close()
52+
_ = asyncio.new_event_loop()

0 commit comments

Comments
 (0)