robhol

MM2

Oct 15th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.40 KB | None | 0 0
  1. --[[
  2.  
  3.     MMAPI - MineMon2 client api
  4.  
  5.     - client broadcasts signal (locate)
  6.     - server identifies itself
  7.     - client registers (id, name, goals)
  8.     {
  9.     - (client does work)
  10.     - client posts progress notification (goalID, progress)
  11.     }
  12.  
  13.     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.
  14.    
  15. ]]
  16.  
  17. local inited = false;
  18. local registerdata = false;
  19. local serverID = false;
  20.  
  21. local lastPacket = false;
  22. local function send(server, msg, wait)
  23.     lastPacket=msg;
  24.     rednet.send(server, msg, wait);
  25. end
  26.  
  27. local function recvthread()
  28.  
  29.     while true do
  30.         local _,src,msg = os.pullEvent("rednet_message");
  31.  
  32.         if serverID and serverID == src then
  33.  
  34.             if msg == "MM2;;WHO" then
  35.                 --debug note: yes, this triggers. and the table is OK.
  36.                 if not registerdata then error("Register needed!"); end
  37.  
  38.                 register(unpack(registerdata));
  39.  
  40.             elseif msg == "MM2;;WTF" then
  41.                 error("Unknown error from server - last packet was " .. lastPacket);
  42.             end
  43.  
  44.         end
  45.     end
  46.  
  47. end
  48.  
  49. function initialize(main)
  50.     parallel.waitForAny(recvthread, main);
  51. end
  52.  
  53. function locate(timeout, tries)
  54.  
  55.     lastPacket="MM2;;LOCATE";
  56.     local rb = rednet.broadcast(lastPacket);
  57.    
  58.     timeout = timeout or 1;
  59.     tries = tries or 3;
  60.  
  61.     while true do
  62.         local src, msg = rednet.receive(timeout);
  63.         if msg == "MM2;;SERVER" then
  64.             serverID=src;
  65.             return src;
  66.         end
  67.         tries = tries - 1;
  68.         if tries == 0 then return false; end
  69.     end
  70.  
  71.     return false;
  72. end
  73.  
  74. 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.
  75.     registerdata={server,name,status,goals};
  76.     send(server, string.format("MM2;;REG;;%s;;%s;;%s", name, status, textutils.serialize(goals)), false);
  77. end
  78.  
  79. function status(server, status)
  80.     send(server, string.format("MM2;;STATUS;;%s", status), true);
  81. end
  82.  
  83. function progress(server, goal, val, max)
  84.     local p = table.concat({"MM2;;PROG", goal, val, max}, ";;");
  85.     send(server, p, true);
  86. end
  87.  
  88.  
  89.  
  90.  
  91.  
  92. --[[
  93.  
  94. MM2TEST - simulates a turtle "drone" doing work
  95.  
  96. ]]
  97.  
  98. os.loadAPI("mmapi");
  99.  
  100. local function main()
  101.  
  102.     print("Locating MM2 server...");
  103.     local serverID = mmapi.locate();
  104.  
  105.     if not serverID then
  106.         print("Failed to find server!");
  107.         error();
  108.     end
  109.  
  110.     local goals = {shafts=3, tunnel=8}
  111.     mmapi.register(serverID, os.getComputerLabel(), "starting", goals)
  112.  
  113.     sleep(2);
  114.     for i=1,goals.shafts do
  115.  
  116.         mmapi.status(serverID, "digging tunnel");
  117.        
  118.         for j=1,goals.tunnel do
  119.             mmapi.progress(serverID, "tunnel", j);
  120.             sleep(1);
  121.         end
  122.  
  123.         mmapi.status(serverID, "moving to next");
  124.         mmapi.progress(serverID, "tunnel", 0);
  125.         mmapi.progress(serverID, "shafts", i);
  126.  
  127.         sleep(2);
  128.     end
  129.  
  130.     mmapi.status(serverID, "complete");
  131.  
  132. end
  133.  
  134.  
  135. local connected = false;
  136. for _, v in ipairs({"right", "left", "top", "bottom", "front", "back"}) do
  137.     if peripheral.getType(v) == "modem" then connected = true; rednet.open(v); break; end
  138. end
  139.  
  140. if not connected then print("Modem not found."); error(); end
  141.  
  142. mmapi.initialize(main)
Advertisement
Add Comment
Please, Sign In to add comment