←  APIs and Utilities

ComputerCraft | Programmable Computers for Minecraft

»

Protocol Manager

HPWebcamAble's Photo HPWebcamAble 24 Mar 2015

Do you find yourself making servers and clients often? (Pretend you do)

Do you want a way to quickly create a protocol for them? (Of course)

Then you need...


Protocol Manager


Features (Why you want it)
Make a set list of messages the client and server will send,
then define functions to run when a message is received


Download

On Pastebin: nps4PsMS

In a computer, run
pastebin get nps4PsMS pm


How to Use

The file has extensive documentation
Make sure to read through it!


Here is a quick example program that I wrote:
Spoiler

Bugs and Suggestions

Please comment with any bugs or suggestions!
Edited by HPWebcamAble, 03 May 2015 - 10:10 PM.
Quote

ElvishJerricco's Photo ElvishJerricco 24 Mar 2015

I like the idea, but I'd suggest having the new function return some kind of handle that has the _____Message functions and the event handling functions. Reason being, you don't want your programs handlers to continue running after program execution terminates.
Quote

HPWebcamAble's Photo HPWebcamAble 24 Mar 2015

 ElvishJerricco, on 24 March 2015 - 05:27 AM, said:

you don't want your programs handlers to continue running after program execution terminates

You lost me :P

Are you saying the API can keep running if the program calling it is terminated?
Or do you mean that variables will persist until the comp is restarted?
Quote

ElvishJerricco's Photo ElvishJerricco 25 Mar 2015

 HPWebcamAble, on 24 March 2015 - 10:40 PM, said:

 ElvishJerricco, on 24 March 2015 - 05:27 AM, said:

you don't want your programs handlers to continue running after program execution terminates

You lost me :P

Are you saying the API can keep running if the program calling it is terminated?
Or do you mean that variables will persist until the comp is restarted?

If I write two programs that each use this API, where one adds a protocol, adds a message to listen for, does some stuff, then exits, and the other does the same, any messages on the first program's protocols will still trigger the first program. This is because your API uses the global space of its data, instead of just returning an object that encapsulates what a program would need, so that the program's listeners don't persist past the program's runtime.
Quote

HPWebcamAble's Photo HPWebcamAble 25 Mar 2015

 ElvishJerricco, on 25 March 2015 - 04:25 AM, said:

-snip-

Messages are stored by protocol name, and if two programs try to use the same name, it errors.

But I see your point, I'll probably do this for my Screen API too
Quote

ElvishJerricco's Photo ElvishJerricco 25 Mar 2015

 HPWebcamAble, on 25 March 2015 - 10:28 PM, said:

 ElvishJerricco, on 25 March 2015 - 04:25 AM, said:

-snip-

Messages are stored by protocol name, and if two programs try to use the same name, it errors.

But I see your point, I'll probably do this for my Screen API too

That's not the issue I meant. If program A registers protocol A, and program B registers protocol B, then when program B calls pm.waitForMessage(), program A's registered callback functions will still get called if anything gets sent on protocol A, even if program A has terminated. This is undesirable.
Quote

HPWebcamAble's Photo HPWebcamAble 25 Mar 2015

 ElvishJerricco, on 25 March 2015 - 11:19 PM, said:

If program A registers protocol A, and program B registers protocol B, then when program B calls pm.waitForMessage(), program A's registered callback functions will still get called if anything gets sent on protocol A, even if program A has terminated

That shouldn't happen

waitForMessage() requires you to specify which protocol's messages to look for:
--# From the Pastebin, line 118

function waitForMessage(protocol,timeout,useRaw)
  protocol = protocol or default
  timeout = tonumber(timeout)
  nAssert(protocolSets[protocol],"protocol doesn't exist",2)
  local pull = useRaw and os.pullEventRaw or os.pullEvent
  if timeout then local timer = os.startTimer(timeout) end
  while true do
    local eArgs = {pull()}
    if eArgs[1] == "modem_message" then
      if type(eArgs[5]) == "table" and eArgs[5][1] == protocol and eArgs[5][2] == protocolSets[protocol].version then  --# The second statment checks that the same protocol sent it, the third then references protocolSets using the protocol name provided in the function call
        if protocolSets[protocol][eArgs[5][3]] then
          protocolSets[protocol][eArgs[5][3]](eArgs[4],eArgs[6],unpack(eArgs[5][4]))
        end
      end
    elseif eArgs[1] == "timer" and eArgs[2] == timer then
      return false
    end
  end
  return false
end
Quote

ElvishJerricco's Photo ElvishJerricco 26 Mar 2015

 HPWebcamAble, on 25 March 2015 - 11:59 PM, said:

- snip -

Ah. Your example in the OP doesn't include that. Hence the confusion.
Quote

HPWebcamAble's Photo HPWebcamAble 26 Mar 2015

 ElvishJerricco, on 26 March 2015 - 12:52 AM, said:

 HPWebcamAble, on 25 March 2015 - 11:59 PM, said:

- snip -

Ah. Your example in the OP doesn't include that. Hence the confusion.

Sorry for any confusion :)
I have no idea why but it worked when i tested it

It uses the 'defualt' protocol if you don't give it one

The default is set by 'new', thats why I passed true, to set it as the defualt


Even though it doens't look like I HAVE to, I might still change it to an object at some point
Edited by HPWebcamAble, 26 March 2015 - 01:41 AM.
Quote