Asking server to wait for input before proceeding

79 views
Skip to first unread message

matthi...@gmail.com

unread,
Jan 3, 2015, 8:34:03 AM1/3/15
to pods...@googlegroups.com
Hi all,

I am completely new to PodSixNet and to networking in general. I tried looking for my topic in previous posts but couldn't find any. Please help me out.

How can I make the server to pause on some task while waiting for input from a client?

For example, when 3 people join, I want to start a game. However, I don't want the game to start until everyone has entered their name.

e.g. in my server class

def Connect(self, channel, addr):

            self.players.append(channel)

            channel.Send({"action":"askname"})  # asks Client for a name

           if len(self.players) == 3:
                    self.game.start()


Obviously, this code starts the game as soon as there are 3 players  - regardless of whether they user has finished entering their name. 

I tried to add 

while not channel.name != "default name":
        pass

But this pauses the entire server altogether.

I can think of some round about way of getting it to work.... But somehow I think this should be a standard procedure. This is my first time setting up a network - any help would be appreciated. 

Matthias
           

    


Tho Deb

unread,
Jan 3, 2015, 12:02:26 PM1/3/15
to pods...@googlegroups.com
Hi Matthias,

The first step, I guess, is to change this bit.
           if len(self.players) == 3: # need to also check if they all entered their name
                    self.game.start()

But I think you need to code a bit more logic to do what you want cleanly. For example, what happens when 2 users have entered their name and somebody (who may or may not have entered their name) disconnects?

You could start your server in connection mode, and set a connection counter on the server. In conn mode,when the counter reaches 3, ask clients their name and switch the server to name asking mode. In name asking mode, when the server receives all names, switch to game mode. If people leave in name asking mode, revert to conn mode and reset counter to number of people left. It's basically a state machine.

Also, you could change your protocol so the clients send their name without the server having to ask them. Then you could consider a client "connected" only if it has sent its name, not just when it has opened a channel.

Cheers!
--
You received this message because you are subscribed to the Google Groups "PodSixNet" group.
To unsubscribe from this group and stop receiving emails from it, send an email to podsixnet+...@googlegroups.com.
To post to this group, send email to pods...@googlegroups.com.
Visit this group at http://groups.google.com/group/podsixnet.
For more options, visit https://groups.google.com/d/optout.

Matthias

unread,
Jan 3, 2015, 7:17:47 PM1/3/15
to pods...@googlegroups.com
Hi,

Thanks for the input.

I think there’s some food for my thought here. I am not used to thinking about networking so it never crossed my mind what would happen if someone tries to disconnect. I will need to put that in.

I cannot actually use the “Ask Client for name before connecting” idea because I need to make sure that players have different names. For that, they need to connect first to the server, get a list of names and then proceed. However, that is still a good idea. Maybe I can start the game without their names and ask for their names once they have joined.

However, in general, I still don’t know how to make the server wait on some task before proceeding. I wonder if it is possible.

Thanks!
Matthias


Sent from Mailbox


--
You received this message because you are subscribed to a topic in the Google Groups "PodSixNet" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/podsixnet/mcas6-tLrLQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to podsixnet+...@googlegroups.com.

Tho Deb

unread,
Jan 3, 2015, 9:36:37 PM1/3/15
to pods...@googlegroups.com
> how to make the server wait on some task before proceeding
The podsixnet server should never be "busy" waiting, for example, for a SQL query to return, or a player to do something. Servers (in general, not just podsixnet servers) always listen for incoming messages, process each of them, and then go back to listening, and loop like that forever. Web servers only do stuff when they receive a client request, they don't have timers, and they don't have frames like games do. So the looping idea works perfectly. Game servers have their own physics/update loop, though. Your game world and NPCs need to be updated even when no player is online. That's why podsixnet tells you to do things the following way (http://mccormick.cx/projects/PodSixNet/):
myserver = MyServer()
while True: # loop forever
    myserver
.Pump() # listen and process messages
    sleep
(1/10) # 10 frames per second
The sleep is a bad practice (you should be polling instead of being idle), but you get the idea. If you want the server to do something when 3 players are in, do something like that:
myserver = MyServer()
while
True: # loop forever
    if len(myserver.players) == 3:
 myserver.start()
myserver
.Pump() # listen and process messages
    sleep
(1/10) # 10 frames per second
Does that answer your question?

Matthias

unread,
Jan 3, 2015, 9:43:12 PM1/3/15
to pods...@googlegroups.com
Yes, I think I get it. 

Basically, what I wanted to do is not sensible. The more I played with the program, the more I realised what I wanted to do wasn’t really sensible. I solved the issue by simply moving “game.start()” to AFTER the set name step instead of the Connect() step

Thanks so much! Much to learn. 

I got another question, which is not related. I realised I can’t send my own defined objects through the Network. So I can send 

Hand =  [ [ 1,2,3], [4,5,6] ] 

to to the client as a data. 

But if I have hand as part of player.hand, then I can’t send player anymore. Is that another restriction or am I doing something wrong?


 But perhaps I should ask this in another post.



Sent from Mailbox

Tho Deb

unread,
Jan 3, 2015, 10:05:57 PM1/3/15
to pods...@googlegroups.com
Objects have to be serialized to go through the network. https://en.wikipedia.org/wiki/Serialization
Podsixnet uses rencode to serialize data. rencode supports basic built-in Python types like int, float, dict, list, and so on. (btw, it does not make the difference between tuples and lists)
https://code.google.com/p/podsixnet/source/browse/podsixnet/PodSixNet/rencode.py
I never tried it, but reading the rencode doc, you may be able to also serialize a Player object (and not just built-in types) if your Player class has a _pack(self) method. Not sure, but I think _pack should only return built-in types (int, list, etc.).


Matthias

unread,
Jan 4, 2015, 4:14:33 AM1/4/15
to pods...@googlegroups.com
I see. Thanks so much for the explanation…

I couldn’t really get the rencode thing to work. Instead, I am just sending the attributes of my class one-by-one. It isn’t pretty, but it works.


Sent from Mailbox

Chris McCormick

unread,
Jan 6, 2015, 2:41:58 AM1/6/15
to pods...@googlegroups.com
On 04/01/15 10:43, Matthias wrote:
> Yes, I think I get it.
>
> Basically, what I wanted to do is not sensible. The more I played with
> the program, the more I realised what I wanted to do wasn’t really
> sensible. I solved the issue by simply moving “game.start()” to AFTER
> the set name step instead of the Connect() step

What you should do is make your application robust to the state of hte
network connection. That is, it should keep running and inform the user
when the server is disconnected etc. Do not tie the number of users
connected to the actual networking. Have it as a separately handled case
as indicated by Tho Deb. Somewhere in the application loop check for
number of players connected for example.

> I got another question, which is not related. I realised I can’t send my
> own defined objects through the Network. So I can send
>
> Hand = [ [ 1,2,3], [4,5,6] ]
>
> to to the client as a data.

What you should do is give each class that you want to share over the
network a method which outputs it's minimal state as Python basic types.
E.g. position, hit points, whatever else.

def state(self):
return {
"hitpoints": self.hitpoints,
"position": self.position,
}

Then when you receive a new state of an object you update the local
copy. When you change the local copy you send the state.

Cheers,

Chris.

--
http://mccormick.cx/

Matthias

unread,
Jan 6, 2015, 6:05:58 AM1/6/15
to pods...@googlegroups.com
Hi all

Thanks for all the input. I am learning quite a bit about network management.

As for sending my own object via Python basic types, this is what ended up doing in the end. I didn’t know about serialisation thing. When I did parallel programming with C++  MPI, I had to send the pointer to an object and the memory size allocated to the object. If I am understanding this serialisation thing correctly, I couldn’t send my object because I only have the pointer/reference to my object but no memory size. 

Matthias


Sent from Mailbox


Chris McCormick

unread,
Jan 6, 2015, 9:32:27 PM1/6/15
to pods...@googlegroups.com
Hi,

On 04/01/15 10:36, Tho Deb wrote:
> The sleep is a bad practice (you should be polling instead of being
> idle), but you get the idea.

The sleep is there so that your application does not use 100% CPU. It
tells the OS that it is ok to idle for a bit and do other things. It has
nothing to do with frame rate and should ideally be as small as possible
(e.g. 0.0001).

---

Incidentally, I should really push PodSixNet hosting up to github and
make a proper Python setup script etc. if people are still using it. I
will try to look into doing that soon.

If someone is interested in taking over general maintenance of this
library let me know.

Chris McCormick

unread,
Jan 6, 2015, 10:10:40 PM1/6/15
to pods...@googlegroups.com
Hi,

On 06/01/15 19:05, Matthias wrote:
> As for sending my own object via Python basic types, this is what ended
> up doing in the end. I didn’t know about serialisation thing. When I did
> parallel programming with C++ MPI, I had to send the pointer to an
> object and the memory size allocated to the object. If I am
> understanding this serialisation thing correctly, I couldn’t send my
> object because I only have the pointer/reference to my object but no
> memory size.

Well not really. It's more like there is a pipe between the two
computers but you are only able to send certain types of things [list,
dict, number, string] down that pipe, but not others. So you can't send
a whole Python objects because the methods, binary code, etc. won't
serialize.

The way to think of it is that you have two computers with this pipe
between them and they will each keep their own copy of the same object
and it is the network code's job to keep the two different
representations in sync by sending updates that consist of only basic
Python types. So the object at each end should have a method for
representing itself as base types and a method for updating itself from
that same representation.

Matthias

unread,
Jan 8, 2015, 3:39:40 AM1/8/15
to pods...@googlegroups.com
Got it.!

Thanks everyone for the inputs and for helping me learn.  I have finished coding up a draft of the game and server thing. My challenge now is to put them online so my friends can connect to me via IP. That’s another business altogether which I need to learn.


Sent from Mailbox


Joseph Parker

unread,
Jan 10, 2015, 11:31:30 AM1/10/15
to pods...@googlegroups.com
On Tue, Jan 6, 2015 at 2:38 AM, Chris McCormick <ch...@mccormick.cx> wrote:

Incidentally, I should really push PodSixNet hosting up to github and
make a proper Python setup script etc. if people are still using it. I
will try to look into doing that soon.

If someone is interested in taking over general maintenance of this
library let me know.


Hi Chris,

I can volunteer to either maintain it on https://github.com/selfsame or as a collaborator .

(I'd done the py3k port a while back)

Best,

Joseph



 
Cheers,

Chris.

--
http://mccormick.cx/

Chris McCormick

unread,
Jan 13, 2015, 12:14:51 AM1/13/15
to pods...@googlegroups.com
Hi Joseph,

On 11/01/15 00:31, Joseph Parker wrote:
> On Tue, Jan 6, 2015 at 2:38 AM, Chris McCormick <ch...@mccormick.cx
> <mailto:ch...@mccormick.cx>> wrote:
> If someone is interested in taking over general maintenance of this
> library let me know.
>
> Hi Chris,
>
> I can volunteer to either maintain it on https://github.com/selfsame or
> as a collaborator .
>
> (I'd done the py3k port a while back)

Ok, great! I will push it up to my GitHub account and get the docs up on
a gh-pages branch, and then I'll add you as a core committer so you can
merge pull requests etc.

Chris McCormick

unread,
Jan 15, 2015, 4:52:04 AM1/15/15
to pods...@googlegroups.com
Hi,

Done: http://github.com/chr15m/PodSixNet/

Joseph, you should have commit access.

Cheers,

Chris.
Reply all
Reply to author
Forward
0 new messages