Skip to content

Commit beaabed

Browse files
committed
Add custom identities
1 parent 6c9d4fb commit beaabed

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,27 @@ Closes a modem for communications.
2929
### `ecnet2.isOpen([modem: string])`
3030
Returns whether a modem is open for communications.
3131

32+
### `ecnet2.Identity(path: string)`
33+
Creates or loads in an identity from a given identity directory path.
34+
3235
### `ecnet2.address(): string`
33-
Returns the address for connecting to this device.
36+
DEPRECATED. Use `ecnet2.Identity("/.ecnet2").address` instead.
37+
38+
### `ecnet2.Protocol(interface: IProtocol): Protocol`
39+
DEPRECATED. Use `ecnet2.Identity("/.ecnet2"):Protocol(...)` instead.
3440

3541
### `ecnet2.daemon`
3642
Function used for managing listener and connection events.
3743
Intended to be put in parallel with users code.
3844

39-
### `ecnet2.Protocol(interface: IProtocol): Protocol`
40-
Creates a protocol from a given interface.
45+
## Type `Identity`
46+
Contains secret data needed to communicate under an address.
47+
48+
### `Identity.address: string`
49+
The address for connecting to this device through this identity.
50+
51+
### `Identity:Protocol(interface: IProtocol): Protocol`
52+
Creates a protocol from a given interface under this identity.
4153

4254
## Type `IProtocol`
4355
A table containing a description for a protocol.

ecnet2/init.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
local constants = require "ecnet2.constants"
22
local Identity = require "ecnet2.Identity"
33
local modems = require "ecnet2.modems"
4-
local Protocol = require "ecnet2.Protocol"
54
local ecnetd = require "ecnet2.ecnetd"
5+
local expect = require "cc.expect"
66

77
local module = {}
88

@@ -14,7 +14,15 @@ local function fetchIdentity()
1414
return identity
1515
end
1616

17+
--- Loads or creates an identity file in the given path.
18+
--- @param path string The path to load or create the identity at.
19+
--- @return ecnet2.Identity
20+
function module.Identity(path)
21+
return Identity(expect(1, path, "string"))
22+
end
23+
1724
--- Returns the address for this device.
25+
--- @deprecated Use `ecnet2.Identity("/.ecnet2").address` instead.
1826
--- @return string address The address.
1927
function module.address()
2028
return fetchIdentity().address
@@ -27,10 +35,11 @@ module.isOpen = modems.isOpen
2735
module.daemon = ecnetd.daemon
2836

2937
--- Creates a protocol from a given interface.
38+
--- @deprecated Use `ecnet2.Identity("/.ecnet2"):Protocol(...)` instead.
3039
--- @param interface ecnet2.IProtocol A table describing the protocol.
3140
--- @return ecnet2.Protocol protocol The resulting protocol.
3241
function module.Protocol(interface)
33-
return Protocol(interface, fetchIdentity())
42+
return fetchIdentity():Protocol(interface)
3443
end
3544

3645
return module

examples/ping/client.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ http.websocket(data.url).close()
1212
-- Open the top modem for comms.
1313
ecnet2.open("top")
1414

15+
-- Define an identity.
16+
local id = ecnet2.Identity("/.ecnet2")
17+
1518
-- Define a protocol.
16-
local ping = ecnet2.Protocol {
19+
local ping = id:Protocol {
1720
-- Programs will only see packets sent on the same protocol.
1821
-- Only one active listener can exist at any time for a given protocol name.
1922
name = "ping",
@@ -40,4 +43,4 @@ local function main()
4043
end
4144
end
4245

43-
parallel.waitForAny(main, ecnet2.daemon)
46+
parallel.waitForAny(main, ecnet2.daemon)

examples/ping/server.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ http.websocket(data.url).close()
1111
-- Open the top modem for comms.
1212
ecnet2.open("top")
1313

14+
-- Define an identity.
15+
local id = ecnet2.Identity("/.ecnet2")
16+
1417
-- Define a protocol.
15-
local ping = ecnet2.Protocol {
18+
local ping = id:Protocol {
1619
-- Programs will only see packets sent on the same protocol.
1720
-- Only one active listener can exist at any time for a given protocol name.
1821
name = "ping",

0 commit comments

Comments
 (0)