network examples / reference?

Return to Scripting Issues

network examples / reference?

Postby blake_ludban » Sun Jan 15, 2006 1:23 pm

Looking for a little help on networking. I see in the manual it says to ask here, so here I am. I already tried a search and basically just came up with a bunch of broken links - lots of references to "Network Example" (along with Rubiks example, and one other - all references made from one page), but when I try to go there, I get a 404.

I don't think what I'm trying to do should be all that difficult. Just want a socket to listen for messages while still having an interactive window. Messages will be to change colors of objects - if I can get a string in, I think I can figure out what to do with it from there...


Any help appreciated!
--Blake
blake_ludban
 
Posts: 3
Joined: Sun Jan 15, 2006 11:43 am

Postby lvraab » Sun Jan 15, 2006 1:37 pm

lvraab
 
Posts: 96
Joined: Thu May 13, 2004 7:12 am

Networking... only different

Postby blake_ludban » Wed Feb 01, 2006 9:26 pm

Just wondering if anyone had some different input on this... I've been going through the Panda API and cross-referencing the DocumentedServer.py - looks neat, but I'm not sure it's quite what I was hoping for. From what I can tell, this is depending on Python at both ends (packaging up a datagram object, sending it over, then unpackaging it). I was hoping for something that could just use a Socket so I can use a different language to communicate with it. I guess I'm looking for a way to open a socket and create a task that will check for data.

Any takers?

Thanks!
blake_ludban
 
Posts: 3
Joined: Sun Jan 15, 2006 11:43 am

Postby drwr » Wed Feb 01, 2006 9:36 pm

If all you want is a low-level socket, try Python's "socket" module.

http://www.python.org/doc/2.4.2/lib/module-socket.html

David
drwr
 
Posts: 11253
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Postby Yellow » Mon Feb 06, 2006 1:06 pm

I understand you dont want to bother with the packing and unpacking of datagrams, but you could just send a string instead... Is that what you're looking for?
Yellow
 
Posts: 146
Joined: Fri Aug 19, 2005 8:36 pm

Postby blake_ludban » Mon Feb 06, 2006 2:38 pm

Yes, I think that would work. I'm just having a hard time figuring out how to have a TCP socket listen and handle events while still allowing Panda to do it's thing. I'm looking into the Twisted framework, but so far it seems that both Twisted and Panda have some sort of internal event loop that pretty much blocks anything else from happening.

Sorry - I'm kind of new to Python... this is definitely a learning experience for me, so if you can give me any pointers, I'd appreciate it!
blake_ludban
 
Posts: 3
Joined: Sun Jan 15, 2006 11:43 am

Postby zpavlov » Mon Feb 06, 2006 4:10 pm

This is based on the reference code here:

http://www.python.org/doc/lib/socket-example.html

Code: Select all
#Let's start with a simple echo server embedded in code
def server():
        import socket
        HOST=''
        PORT=50007
        s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        s.bind((HOST,PORT))
        s.listen(1)
        conn, addr = s.accept()
        print 'connected by', addr
        while(1):
                data = conn.recv(1024)
                if not data: break
                conn.send(data)
        conn.close()

#Now, if you run this with panda, it will cause panda
#to stop processing. You need to have this running all
#the time, but you also need panda running all the time

#One of the ways to get around this is to use threading
import thread
thread.start_new_thread(server,())

#this basically says "call this function inside of a
#thread, and let it run in parallel with my process"


#finally, here's the host code to test it with.
import socket
HOST='127.0.0.1'
PORT=50007

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((HOST,PORT))
s.send('Hello, world')
data=s.recv(1024)
s.close()
print 'Recieved', repr(data)
zpavlov
 
Posts: 60
Joined: Tue Apr 05, 2005 4:41 pm

Postby Drew » Tue Feb 21, 2006 12:18 pm

blake_ludban wrote:Yes, I think that would work. I'm just having a hard time figuring out how to have a TCP socket listen and handle events while still allowing Panda to do it's thing. I'm looking into the Twisted framework, but so far it seems that both Twisted and Panda have some sort of internal event loop that pretty much blocks anything else from happening.

Panda and Twisted both have their own event loops that want to be in control of program flow, and need to be able to run independantly of each other. In the code I'm working on I was able to solve this problem by creating two program threads, one for Panda and one for Twisted, and using two Queue structures to pass messages between the two threads.

In Panda3D I have a task which runs once per frame, which checks the incoming Queue for messages from the server and updates world objects in Panda as needed. Player inputs call event handlers which place messages on the outgoing queue. In Twisted I have a loop which periodically checks the outgoing queue for messages, and sends them to the server as needed. Messages from the server are places on the incoming queue and sent to the Panda task as they happen.

This may not be the most optimal way to do it and still needs a lot of development, but it does solve the problem of running Twisted and Panda simultaneously.
Drew
 
Posts: 7
Joined: Mon Feb 20, 2006 7:14 pm

Postby drwr » Tue Feb 21, 2006 12:59 pm

Another, simpler option is not to call run() at all. Panda doesn't really need to own the main loop.

Instead, simply call Task.step() from time to time, which will run through one iteration of Panda's loop. In fact, run() is basically just an infinite loop that calls Task.step() repeatedly.

Of course, you have to ensure that Task.step() is called frequently enough to keep your frame rate up.

David
drwr
 
Posts: 11253
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Postby bigfoot29 » Sun Feb 26, 2006 6:06 am

Hi :) zpawlov, I tried your example above, but I get constantly the following error message:

Code: Select all
 s.connect((HOST,PORT))
  File "<string>", line 1, in connect
socket.error: (111, 'Connection refused')

What goes wrong there? (Using Python 2.3.5)

He says that he can't connect to the socket, that I see as well, but I wonder why he can't connect... however, when running the server part alone, it opens the port and waits for a connection.

But when starting the client part, I get the message mentioned above :(

Where is the error? At my place may be?

Regards, Bigfoot29
User avatar
bigfoot29
 
Posts: 617
Joined: Thu Jun 30, 2005 4:22 am
Location: Germany

Postby bigfoot29 » Tue Feb 28, 2006 11:19 am

Small change to drwr's post:

Code: Select all
Task.step()
doesn't work. It has to be
Code: Select all
taskMgr.step()


Regards, Bigfoot29
User avatar
bigfoot29
 
Posts: 617
Joined: Thu Jun 30, 2005 4:22 am
Location: Germany

Postby drwr » Tue Feb 28, 2006 11:26 am

That's what I get for typing in code suggestions off the top of my head! :oops:
drwr
 
Posts: 11253
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Postby bigfoot29 » Tue Feb 28, 2006 11:41 am

Hehe... the "cloaky" Mr Cloak gave me that advice in IRC, just wanted to provide the results here too :D

He seem to know you, David *wonders* ;)

Regards, Bigfoot29
User avatar
bigfoot29
 
Posts: 617
Joined: Thu Jun 30, 2005 4:22 am
Location: Germany

Postby Pyked » Mon Apr 24, 2006 8:07 am

im sure this is a really bad way to do it but...


Code: Select all
def runEverySecond():
        taskMgr.step()   
w = World()
l = task.LoopingCall(runEverySecond)
l.start(0)
pykeClient = pykeFactory(w)
reactor.connectTCP("localhost", 8007, pykeClient)#(ipaddress,port,factory)
reactor.run()


then in your factory...
Code: Select all
class pykeFactory(ClientFactory):
    def __init__(self,wint):
        self.wor = wint

the client factory doesn't have an init so don't call it heh
so this way you can access stuff in your world object... like uh a queue:P

Edit: As for a server you could just write that all in twisted:)
Pyked
 
Posts: 2
Joined: Fri Apr 21, 2006 10:54 pm


Return to Scripting Issues

Who is online

Users browsing this forum: Google [Bot], Stein and 1 guest