Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- MMAPI - MineMon2 client api
- - client broadcasts signal (locate)
- - server identifies itself
- - client registers (id, name, goals)
- {
- - (client does work)
- - client posts progress notification (goalID, progress)
- }
- PROBLEM: client authenticates with the server before posting progress notifications. If the server is restarted, it will "forget" the client and will return a "WHO" packet (handled in recvthread). In response, the client is supposed to trigger register() again with the same data. This does not happen, but it keeps doing work.
- ]]
- local inited = false;
- local registerdata = false;
- local serverID = false;
- local lastPacket = false;
- local function send(server, msg, wait)
- lastPacket=msg;
- rednet.send(server, msg, wait);
- end
- local function recvthread()
- while true do
- local _,src,msg = os.pullEvent("rednet_message");
- if serverID and serverID == src then
- if msg == "MM2;;WHO" then
- --debug note: yes, this triggers. and the table is OK.
- if not registerdata then error("Register needed!"); end
- register(unpack(registerdata));
- elseif msg == "MM2;;WTF" then
- error("Unknown error from server - last packet was " .. lastPacket);
- end
- end
- end
- end
- function initialize(main)
- parallel.waitForAny(recvthread, main);
- end
- function locate(timeout, tries)
- lastPacket="MM2;;LOCATE";
- local rb = rednet.broadcast(lastPacket);
- timeout = timeout or 1;
- tries = tries or 3;
- while true do
- local src, msg = rednet.receive(timeout);
- if msg == "MM2;;SERVER" then
- serverID=src;
- return src;
- end
- tries = tries - 1;
- if tries == 0 then return false; end
- end
- return false;
- end
- function register(server, name, status, goals) --register(srvID, "turtleface :3", "starting", {overall=200, width=40, height=50}) -- first defined goal will be treated as the overall goal for a turtle.
- registerdata={server,name,status,goals};
- send(server, string.format("MM2;;REG;;%s;;%s;;%s", name, status, textutils.serialize(goals)), false);
- end
- function status(server, status)
- send(server, string.format("MM2;;STATUS;;%s", status), true);
- end
- function progress(server, goal, val, max)
- local p = table.concat({"MM2;;PROG", goal, val, max}, ";;");
- send(server, p, true);
- end
- --[[
- MM2TEST - simulates a turtle "drone" doing work
- ]]
- os.loadAPI("mmapi");
- local function main()
- print("Locating MM2 server...");
- local serverID = mmapi.locate();
- if not serverID then
- print("Failed to find server!");
- error();
- end
- local goals = {shafts=3, tunnel=8}
- mmapi.register(serverID, os.getComputerLabel(), "starting", goals)
- sleep(2);
- for i=1,goals.shafts do
- mmapi.status(serverID, "digging tunnel");
- for j=1,goals.tunnel do
- mmapi.progress(serverID, "tunnel", j);
- sleep(1);
- end
- mmapi.status(serverID, "moving to next");
- mmapi.progress(serverID, "tunnel", 0);
- mmapi.progress(serverID, "shafts", i);
- sleep(2);
- end
- mmapi.status(serverID, "complete");
- end
- local connected = false;
- for _, v in ipairs({"right", "left", "top", "bottom", "front", "back"}) do
- if peripheral.getType(v) == "modem" then connected = true; rednet.open(v); break; end
- end
- if not connected then print("Modem not found."); error(); end
- mmapi.initialize(main)
Advertisement
Add Comment
Please, Sign In to add comment