Rednet Server and Client

Discussion in 'ComputerCraft Programming' started by hsun324, Jan 13, 2013.

  1. hsun324

    hsun324 Programmer, Gamer

    Joined:
    May 14, 2012
    Messages:
    362
    Likes Received:
    90
    So, for the new Alice 1.4.6 update I coded a system where I could get data from sensors on computers to a main central control computer where I could get a centralized look at data and control operations.

    DISCLAIMER: Code is not tested yet. Will be tested when 1.4.6 comes out and I get access to two computers w/ modems on them.

    Server:
    Code:
    data = {
      -- data format goes here: i.e.
      --  type: "TIME"
      --  day: 1
      --  year: 2013
    }
    interval = .5      -- send every <interval> seconds
    modemSide = "left"  -- side of computer modem is on (default "left"?)
    reciever = 10      -- reciever computer id
    running = true
     
    -- INTERNAL
    clocked = 0
    preface = "[RD-Serv #"..os.getComputerID().."] Sent message "
    prefaceError = "[RD-Serv #"..os.getComputerID().."] Error: "
    postface = "B to "..reciever.."."
     
    function updateData()
      -- update the data here
    end
     
    function transmitData()
      local status, serialized = pcall(textutils.serialize, data)
      if status then
        rednet.send(reciever, serialized)
        print(preface..++clocked.." of "..#serialized..postface)
      else
        print(prefaceError..serialized)
      end
    end
     
    function waitForTimer()
      local timer = os.startTimer(interval);
      while true do
        local event, id = os.pullEventRaw();
        if event == "timer" and timer == id then
          break
        elseif event == "terminate" then
          rednet.close(modemSide)
          running = false
        end
      end
    end
    rednet.open(modemSide)
    while running do
      updateData()
      transmitData()
      waitForTimer()
    end
    
    Client:
    Code:
    interval = .5        -- interval between ticks if no events are captured
    modemSide = "left"    -- side modem is conneted (default "left"?)
     
    -- INTERNAL
    running = true;
    prefaceError = "[RD-Client #"..os.getComputerID().."] Error: "
     
    function tick()
      -- do tick functionality here
    end
     
    function recievedMessage(data)
      -- do things with data (do not call tick() from within here; tick() will be called in waitForEvent())
    end
     
    function captureEvent(event, param1, param2, param3)
      -- do things with an captured event (do not call tick() from within here; tick() will be called in waitForEvent())
    end
     
    function processMessage(message)
      local status, deserialized = pcall(textutils.deserialize, lastMessage);
      if status then
        recievedMessage(deserialized)
      else
        print(prefaceError..deserialized)
      end
    end
     
     
    function waitForEvent()
      local timer = os.startTimer(interval);
      local event, param1, param2, param3 = os.pullEventRaw();
      if event == "rednet_message" then
        processMessage(param2)
      elseif event == "terminate" then
        rednet.close(modemSide)
        running == false;
      elseif event != "timer" or timer != param1 then
        captureEvent(event, param1, param2, param3)
      end
    end
     
    rednet.open(modemSide)
    while running do
      waitForEvent();
      if running then
        tick()
      end
    end
    
    Trivia: Both programs are 49 lines long. :)
     
    Ali and Chambersiy like this.