Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
UCQuasar

Need help using load to execute command using modem messages

Question

Hi, I have a computer connected to some adapters for my nuclearcraft reactors and I want to run commands to these adapters via modem messages. I've got the load function to execute commands such as reactor.activate and deactivate but it won't return any values such as reactor.isProcessing (true/false) or reactor.getEnergyStored(). I've got a code example below where I've skipped the modem message and made an example string.

Printing test gives me a nil value but printing test2 gives me the expected value of true/false. How do I get it to return something so that I can send the info back to the requesting computer?

Thanks.

2019-02-18_16.23.00.png

Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 1
  • Solution

I believe I see where you're trying to go with this but I think there is an easier way. Store the reactor proxies as values in a key/value table and then send method names and args to be executed.

Here's a simple example..

-- # Reactor server: Recieves commands from remote client to be run
local component = require 'component'
local event = require 'event'

local modem = component.modem
modem.open(1040)

-- # List of reactors to call methods remotely upon
local reactors = {
  r1 = component.proxy(component.get '0c60c'),
  r2 = component.proxy(component.get '0ef58'),
  r3 = component.proxy(component.get '255aa')
}

repeat
  -- # pack it all into a table
  local msg = table.pack(event.pull('modem_message'))
  -- # the droids we're looking for
  local reactor, method = msg[6], msg[7]
  -- # args to call with the remote method (if any)
  local args = #msg > 7 and table.pack(table.unpack(msg, 8)) or {}
  
  -- # if remote reactor id is present then...
  if reactors[reactor] then
    -- # execute the method call with args on a specific reactor. guarded for faulty messages with pcall so we dont crash.
    local result = table.pack(pcall(reactors[reactor][method], table.unpack(args)))
    print(sting.format("called %s on reactor id %s", method, reactor))
    -- # respond to client with result
    modem.send(msg[2], msg[3], table.unpack(result))
  end
  -- # client can send the message 'exit' to quit or hold Ctrl+C on the server
until msg[6] == 'exit'
-- # End of server
-- # client: Send remote commands to be run on server components
-- # A very simple client... Spruce it up as needed
local component = require 'component'
local event = require 'event'

local modem = component.modem
modem.open(1040)

modem.send(server.address, server.port, 'r1', 'isProcessing') -- # exec 'isProcessing' on 'r1' at server
print(event.pull('modem_message', modem.address, server.address, server.port)) -- # print and await server response

-- # End of client

 

Link to post
Share on other sites
  • 0

Your example code works perfectly for what I'm doing. Thank you very much for your help.

Edit - If I wanted to send a bunch of messages at once or had two computers sending messages to the server, how would the server handle multiple messages being sent within a few ticks. Would it hang while it completes the previous message instructions and drop a packet or would it queue up the events?

Link to post
Share on other sites
  • 0

The server code has no queue implementation so if the server takes too long too execute a certain command then i imagine the packet with a command from another client simply won't be executed. If you can wait a bit I'll update the example with a queue or you can take a walk at it yourself. You're very welcome bud.

Link to post
Share on other sites
  • 0
18 minutes ago, Molinko said:

If you can wait a bit I'll update the example with a queue or you can take a walk at it yourself.

It's fine, it was mostly just a curiosity question. I spammed a bunch of packets at the server and it seemed to crunch through them without dropping any and works great for this use case. I might take a crack at a queue system at a later time.

I found that I have a 9 tick delay to send and get a response back and it takes 1 tick to send a modem message, so I'm guessing that as long as I don't send much more than 9 client messages at once without switching to an event pull then it should be fine.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.