8000 Rednet · neumond/python-computer-craft@bf3b0e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf3b0e8

Browse files
committed
Rednet
1 parent dcff5ba commit bf3b0e8

File tree

2 files changed

+87
-13
lines changed

2 files changed

+87
-13
lines changed

computercraft/subapis/rednet.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,55 @@
1-
from typing import Any, List
1+
from typing import Any, List, Optional, Tuple, Union
22

33
from .base import BaseSubAPI
44
from ..lua import LuaNum
5-
from ..rproc import nil, integer, boolean, array_integer
5+
from ..rproc import nil, integer, option_string, boolean, array_integer, option_integer, fact_option, fact_tuple
6+
7+
8+
recv_result = fact_option(fact_tuple(
9+
integer,
10+
lambda v: v,
11+
option_string,
12+
tail_nils=1,
13+
))
614

715

816
class RednetAPI(BaseSubAPI):
17+
CHANNEL_REPEAT = 65533
18+
CHANNEL_BROADCAST = 65535
19+
920
async def open(self, side: str):
1021
return nil(await self._send('open', side))
1122

12-
async def close(self, side: str):
23+
async def close(self, side: str = None):
1324
return nil(await self._send('close', side))
1425

15-
async def send(self, receiverID: int, message: Any, protocol: str = None):
16-
return nil(await self._send('send', receiverID, message, protocol))
26+
async def send(self, receiverID: int, message: Any, protocol: str = None) -> bool:
27+
return boolean(await self._send('send', receiverID, message, protocol))
1728

1829
async def broadcast(self, message: Any, protocol: str = None):
1930
return nil(await self._send('broadcast', message, protocol))
2031

21-
async def receive(self, protocolFilter: str = None, timeout: LuaNum = None) -> int:
22-
return integer(await self._send('receive', protocolFilter, timeout))
32+
async def receive(
33+
self, protocolFilter: str = None, timeout: LuaNum = None,
34+
) -> Optional[Tuple[int, Any, Optional[str]]]:
35+
return recv_result(await self._send('receive', protocolFilter, timeout))
2336

24-
async def isOpen(self, side: str) -> bool:
37+
async def isOpen(self, side: str = None) -> bool:
2538
return boolean(await self._send('isOpen', side))
2639

2740
async def host(self, protocol: str, hostname: str):
2841
return nil(await self._send('host', protocol, hostname))
2942

30-
async def unhost(self, protocol: str, hostname: str):
31-
return nil(await self._send('unhost', protocol, hostname))
32-
33-
async def lookup(self, protocol: str, hostname: str) -> List[int]:
34-
return array_integer(await self._send('lookup', protocol, hostname))
43+
async def unhost(self, protocol: str):
44+
return nil(await self._send('unhost', protocol))
45+
46+
async def lookup(self, protocol: str, hostname: str = None) -> Union[Optional[int], List[int]]:
47+
result = await self._send('lookup', protocol, hostname)
48+
if hostname is None:
49+
if result is None:
50+
return []
51+
if isinstance(result, list):
52+
return array_integer(result)
53+
return [integer(result)]
54+
else:
55+
return option_integer(result)

testmod.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,4 +1807,57 @@ async def test_paintutils(api):
18071807
await api.print('Test finished successfully')
18081808

18091809

1810+
async def test_rednet(api):
1811+
from computercraft.subapis.rednet import RednetAPI
1812+
tbl = await get_object_table(api, 'rednet')
1813+
del tbl['function']['run']
1814+
assert get_class_table(RednetAPI) == tbl
1815+
1816+
side = 'back'
1817+
1818+
await step(api, f'Attach modem to {side} side of computer')
1819+
1820+
assert await api.rednet.isOpen(side) is False
1821+
assert await api.rednet.isOpen() is False
1822+
1823+
with assert_raises(LuaException):
1824+
await api.rednet.close('doesnotexist')
1825+
1826+
assert await api.rednet.close(side) is None
1827+
1828+
with assert_raises(LuaException):
1829+
await api.rednet.open('doesnotexist')
1830+
1831+
assert await api.rednet.open(side) is None
1832+
assert await api.rednet.isOpen(side) is True
1833+
1834+
with assert_raises(LuaException) 6D40 :
1835+
# disallowed hostname
1836+
await api.rednet.host('helloproto', 'localhost')
1837+
assert await api.rednet.host('helloproto', 'alpha') is None
1838+
1839+
cid = await api.os.getComputerID()
1840+
1841+
assert await api.rednet.lookup('helloproto', 'localhost') == cid
1842+
assert await api.rednet.lookup('helloproto') == [cid]
1843+
assert await api.rednet.lookup('nonexistent', 'localhost') is None
1844+
assert await api.rednet.lookup('nonexistent') == []
1845+
1846+
assert await api.rednet.unhost('helloproto') is None
1847+
1848+
assert await api.rednet.send(cid + 100, 'message', 'anyproto') is True
1849+
assert await api.rednet.broadcast('message', 'anyproto') is None
1850+
1851+
assert await api.rednet.receive(timeout=1) is None
1852+
assert await asyncio.gather(
1853+
api.rednet.receive(timeout=1),
1854+
api.rednet.send(cid, 'message'),
1855+
) == [(cid, 'message', None), True]
1856+
1857+
assert await api.rednet.close() is None
1858+
assert await api.rednet.isOpen(side) is False
1859+
1860+
await api.print('Test finished successfully')
1861+
1862+
18101863
# vector won't be implemented, use python equivalent

0 commit comments

Comments
 (0)
0