-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluenet.lua
103 lines (87 loc) · 2.62 KB
/
bluenet.lua
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
-- Bluenet 2.0 client API
-- This file is not to be run manually
-- Copyright 2018 Shawn Anastasio
-- Licensed under terms of the GNU GPL v3 license
-- Include protocol library
dofile("bluenet2/protocol.lua")
-- Table of strings of open modem sides
local modems = {}
local local_id = os.getComputerID()
-- Internal functions
local function announce_host_up(side)
local packet = Packet(VERSION, PACKET_TYPE_ANNOUNCE, AnnounceExtra(local_id, ANNCMD_HOST_UP))
peripheral.call(side, "transmit", LAN_CHANNEL, 0, packet:serialize())
end
local function announce_host_down(side)
local packet = Packet(VERSION, PACKET_TYPE_ANNOUNCE, AnnounceExtra(local_id, ANNCMD_HOST_UP))
peripheral.call(side, "transmit", LAN_CHANNEL, 0, packet:serialize())
end
-- Rednet-compatible API
-- TODO: broadcast, host/unhost/lookup (CC 1.6+)?
function open(side)
if table_contains(modems, side) ~= nil then
-- Already open, silently ignore
return
end
peripheral.call(side, "open", LAN_CHANNEL)
modems[#modems+1] = side
announce_host_up(side)
end
function close(side)
local key = table_contains(modems, side)
if key == nil then
-- Connection not open, ignore
return
end
announce_host_down(side)
modems[key] = nil
end
function isOpen(side)
if table_contains(modem, side) == nil then
return false
else
return true
end
end
function send(dst, data, proto)
-- Construct a packet
local packet = Packet(VERSION, PACKET_TYPE_DATA, DataExtra(local_id, dst, data, proto))
-- Serialize the packet and send to the router over LAN_CHANNEL on all enabled modems
local packet_ser = packet:serialize()
for _, v in pairs(modems) do
peripheral.call(v, "transmit", LAN_CHANNEL, 0, packet_ser)
end
end
function receive(arg1, arg2)
-- Check if a protocol was provided
local timeout = nil
local proto = nil
if type(arg1) == "number" then
timeout = arg1
else
proto = arg1
timeout = arg2
end
if timeout ~= nil then
os.startTimer(timeout)
end
while true do
local event, side, s_channel, _, msg, _ = os.pullEvent()
if event == "timer" and timeout ~= nil then
-- Operation timed out, abort
return nil
elseif event == "modem_message" and s_channel == LAN_CHANNEL then
-- Received a message on the LAN_CHANNEL, return it if the dst is us
local packet = Packet.unserialize(msg)
if packet:get_type() == PACKET_TYPE_DATA then
local extra = DataExtra.from_raw(packet:get_extra())
if extra:get_dst() == local_id then
-- Packet is for us, check protocol if existing
if (proto ~= nil and extra:get_proto() == proto) or proto == nil then
return extra:get_src(), extra:get_data(), extra:get_proto()
end
end
end
end
end
end