Jump to content




Protocol Manager

api networking utility

8 replies to this topic

#1 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 24 March 2015 - 02:13 AM

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.


#2 ElvishJerricco

  • Members
  • 803 posts

Posted 24 March 2015 - 05:27 AM

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.

#3 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 24 March 2015 - 10:40 PM

View PostElvishJerricco, 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?

#4 ElvishJerricco

  • Members
  • 803 posts

Posted 25 March 2015 - 04:25 AM

View PostHPWebcamAble, on 24 March 2015 - 10:40 PM, said:

View PostElvishJerricco, 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.

#5 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 25 March 2015 - 10:28 PM

View PostElvishJerricco, 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

#6 ElvishJerricco

  • Members
  • 803 posts

Posted 25 March 2015 - 11:19 PM

View PostHPWebcamAble, on 25 March 2015 - 10:28 PM, said:

View PostElvishJerricco, 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.

#7 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 25 March 2015 - 11:59 PM

View PostElvishJerricco, 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


#8 ElvishJerricco

  • Members
  • 803 posts

Posted 26 March 2015 - 12:52 AM

View PostHPWebcamAble, on 25 March 2015 - 11:59 PM, said:

- snip -

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

#9 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 26 March 2015 - 01:01 AM

View PostElvishJerricco, on 26 March 2015 - 12:52 AM, said:

View PostHPWebcamAble, 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.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users