[Solved] Another Networking Question

Found this thread, and it is completely great!

discourse.panda3d.org/viewtopic.php?t=4881

I have a question about moving data through the connection, though. I have a dictionary that I want to send, containing the XYZHpr data for several objects. I cannot seem to get it straight, though. On the sending side, I use:

sendData={'p':0}
sendData['clientID']=name
sendData['X']=str(self.player.ship.getX())
sendData['Y']=str(self.player.ship.getY())
sendData['Z']=str(self.player.ship.getZ())
sendData['H']=str(self.player.ship.getH())
sendData['P']=str(self.player.ship.getP())
sendData['R']=str(self.player.ship.getR())

self.client.sendData(sendData)

so far, so good.

On the receiving end, I use:

        data={'p':0}
        data = self.server.getData()
        print data

        testData = dict(data)
        print testData

it prints out the value for data, but cannot create the dictionary on the receiving end. The data coming across is:


[{'p': 0, 'H': '-90.0', 'ClientID': '123', 'P': '0.0', 'R': '0.0', 'Y': '0.0', '
X': '0.0', 'Z': '0.0'}]

and the error I get is:


    testData = dict(data)
ValueError: dictionary update sequence element #0 has length 8; 2 is required

I understand that I am not using the .dict() function properly, but I am at a loss for how to move a dictionary through the communication channel. With the amount of dat I am trying to move, I would hate to have to manually convert it to a string, and then convert it back on the other end. I tried the pickling and unpickling the data, but that didn’t work either.

Any suggestions from the forum wizards?

Thanks!

Hi. It looks like self.server.getData() is returning a list of all the data items that have come across the wire. You may be sending the items only one at a time, but there is still a chance for a lot of packets to arrive at once. So you are calling dict() on a sequence instead of on the actual dictionary.

It looks like the data that you are getting is ALREADY a dictionary, so there should be no conversion needed. Try this code and see if it works better:

#Look at each data that arrives
for data in self.server.getData():
    print data  #Should already be a dictionary
    data = dict(data)    #Might not be needed

I will try that when I get home from work tonight. A couple notes though:

  1. The first code block is where I build the dict. it is sent across the wire in a single shot. Should I try sending each value separately? For a demo that might work, but I think it would be impractical to scale up.

  2. in the 2nd code block, I am trying to apply the dict(data) to create the dictionary. If you look in the 3rd code block, you will see the result of printing after a single execution of getData(). I think the problem is that the [ ] around the dictionary is making python view it as a list of a single item. How can I break the dictionary out of the list, so to speak. If I could get the data representation to not include the [ ] brackets around it, I think calling dict(data) in the second code block would work. Unfortunately, trying to use string operators to strip them off did not work.

Any thoughts?

Thanks

Excellent. I finally got a chance to get back on the game, and this did the trick perfectly.

Thanks!