PDA

View Full Version : Small wrapping question



Mastiff
June 29th, 2005, 05:16 AM
OK, I have my communications wrapped up like this:

Kom={}
function Kom.C1(msg) --server
if not Kom.con1 then
Kom.con1,err = comserv.NewConnection( '192.168.0.1', 20005, 'girder');
win.Sleep (100)
end
local seq, err = Kom.con1:SendEvent(msg,200,1)
if err then
Kom.con1:Close()
Kom.con1,err = comserv.NewConnection( '192.168.0.1', 20005, 'girder');
win.Sleep (100)
local seq, err = Kom.con1:SendEvent(msg,200,1)
end
end
This creates the table Kom and allows me to send comserver events as easy as this:

Kom.C1("test")

What I would like is to expand this so I can use the same sending for anything without a payload and this for anything with a a payload:


Kom.C1("test","payload1","payload2")

But without having to use nil if there's no payload. Is this possible?

birty
June 29th, 2005, 05:28 AM
i think if you omit a parameter then it is passed to the fuction as nil, so you can declare the function in the second form and then when you call

Kom.C1("test")
the payloads will be nil (you will need to check for this)

Mastiff
June 29th, 2005, 07:37 AM
Thanks! That means that I don't have to change anything in the original function script, right? The message will incorporate both the payloads and the event?

And do you mean check at the reiving end? If that's so, there's no problem, since I have no commands that can be both with and without payloads.

Mastiff
June 29th, 2005, 07:51 AM
Ouch...not that easy, of course.
CID.Number = "909"
CID.Name = "Tor Mastiff"
CID.Address = "The Road 26, 0000 Smalltown"
CID.Datering = "29.06 at 17.30"
Kom.C2("CID.Telefon",CID.Number,CID.Name,CID.Address,CID.Datering)

This sends the command, but without the payloads. I assume this is the line that needs changing:

local seq, err = Kom.con2:SendEvent(msg1,200,1,msg2,msg3)

But just adding the msg1 and so on did nothing. I was thinking something of the lines of %1, %2, %3 in old DOS batch files. I recon this has to be the case here as well, but that the format is wrong. Right?

birty
June 29th, 2005, 08:58 AM
you need to change the original function declaration to add the extra parameters. i meant that in the function you will need to check whether the playloads are nil, if they are then the parameters were omitted when the function was called

Mastiff
June 29th, 2005, 09:01 AM
Aha. OK. I know my limitations... That will have to wait until somebody who knows what they're soing has a few minutes extra time. :oops:

danward79
June 29th, 2005, 09:50 AM
Tor,

Can you not just use this.



Kom={}
function Kom.C1(msg, pld1, pld2) --server
if not Kom.con1 then
Kom.con1,err = comserv.NewConnection( '192.168.0.1', 20005, 'girder');
win.Sleep (100)
end
local seq, err = Kom.con1:SendEvent(msg,200,1)
if err then
Kom.con1:Close()
Kom.con1,err = comserv.NewConnection( '192.168.0.1', 20005, 'girder');
win.Sleep (100)
local seq, err = Kom.con1:SendEvent(msg,200,1, (pld1 or nil), (pld2 or nil))
end
end


calling it like you say


Kom.C1("test","payload1","payload2")

That should work, I think! :oops:

Mastiff
June 29th, 2005, 09:52 AM
Gotcha! Gotta test that one. I actually didn't know that I could pass payloads directly to a function!

Rob H
June 29th, 2005, 09:57 AM
local seq, err = Kom.con1:SendEvent(msg,200,1, (pld1 or nil), (pld2 or nil))


You don't need the (pld1 or nil) and (pld2 or nil), just pld1 and pld2 will do.

danward79
June 29th, 2005, 09:59 AM
Infact I don't think you even need this line as it is


local seq, err = Kom.con1:SendEvent(msg,200,1, (pld1 or nil), (pld2 or nil))

This should work too


local seq, err = Kom.con1:SendEvent(msg,200,1, pld1, pld2)

danward79
June 29th, 2005, 10:00 AM
Posting at the same time !

:D

Mastiff
June 29th, 2005, 10:04 AM
I'm down to this, and this works:

Kom={}
function Kom.C2(msg, pld1, pld2, pl3, pld4) --server
if not Kom.con2 then
Kom.con2,err = comserv.NewConnection( '192.168.0.2', 20005, 'girder');
win.Sleep (100)
end
local seq, err = Kom.con2:SendEvent(msg,200,1, pld1, pld2, pld3, pld4)
if err then
Kom.con2:Close()
Kom.con2,err = comserv.NewConnection( '192.168.0.2', 20005, 'girder');
win.Sleep (100)
local seq, err = Kom.con2:SendEvent(msg,200,1, pld1, pld2, pld3, pld4)
end
end

Thanks a lot again! :D

Rob H
June 29th, 2005, 10:08 AM
You could in fact use this

Kom={}
function Kom.C2(msg, ...) --server
if not Kom.con2 then
Kom.con2,err = comserv.NewConnection( '192.168.0.2', 20005, 'girder');
win.Sleep (100)
end
local seq, err = Kom.con2:SendEvent(msg,200,1, unpack(args))
if err then
Kom.con2:Close()
Kom.con2,err = comserv.NewConnection( '192.168.0.2', 20005, 'girder');
win.Sleep (100)
local seq, err = Kom.con2:SendEvent(msg,200,1, unpack(args))
end
end

Promixis
June 29th, 2005, 11:46 AM
Rob, Mastiff,

I wonder if we shouldn't wrap the com server stuff for automatated connecction to girder clients. ie. similar to NR.lua. That way all connections can be handled by one block of code and users will just say send this send that blah blah blah and then Mastiff won't complain so much and my brain will hurt less.

Mastiff
June 29th, 2005, 11:48 AM
I agree that it would help me, but I don't think your brain can be saved... :wink: So this would be a wrapping where you can choose between send.all and send.192.168.0.1, right?

Rob H
June 29th, 2005, 11:49 AM
Sounds like a good idea to me.

Promixis
June 29th, 2005, 11:57 AM
:)

Ok, Tor, let me know when you got something too look at :lol:

Mastiff
June 29th, 2005, 11:58 AM
Man, you really should have Jeff Foxworthy's job! But then again you probably can't do the accent...

danward79
June 29th, 2005, 11:59 AM
Sounds like a good idea here too, you have to take the fun out of it don't you! :D

With regard to your brain. I agree with Mastiff. I think you should forget about it. :wink:

Mastiff
June 29th, 2005, 12:01 PM
Better be careful, or he'll turn around and say: "Screw it. I'll let you guys take the nagging from Mastiff instead!"

birty
June 29th, 2005, 04:20 PM
i tihnk if girder 4 is certified "Mastiff Proof" then it will be ok for most people :wink:

Mastiff
June 29th, 2005, 11:01 PM
Hey, why do you think I'm a beta tester? Foolproof is for amateurs! Mastiff proof® is where it's at!

Ron
June 30th, 2005, 06:17 AM
We are longing for the day that we get that stamp. We can only dream.

Ron
June 30th, 2005, 06:29 AM
At least we know what it looks like.

Rob H
June 30th, 2005, 06:33 AM
:lol: :lol: :lol:

Mastiff
June 30th, 2005, 07:06 AM
That's a fake! Mine has a dog's head on it! :evil: Or was it a donkey's ass? Can't remember, it's so long since I was able to use it... :lol:

Ron
June 30th, 2005, 07:13 AM
:D :D

Mastiff
July 2nd, 2005, 07:31 AM
Mike, do you know when you'll get to that wrapping? I'm about to let my systems go live, my wife and kids come back Monday. Which gives me a lot less time for bugfixing then I expected. So I will either have to deploy with my own wrapping, or I can hope that you will have something pretty soon. What is it? :roll:

Promixis
July 2nd, 2005, 07:35 AM
Mike, do you know when you'll get to that wrapping? I'm about to let my systems go live, my wife and kids come back Monday. Which gives me a lot less time for bugfixing then I expected. So I will either have to deploy with my own wrapping, or I can hope that you will have something pretty soon. What is it? :roll:

I wish :(

Not yet... on call this weekend :evil:

Mastiff
July 2nd, 2005, 07:36 AM
Dang. OK, I'll go for my own wrapping, then. At least for now.