| Line |  | 
|---|
| 1 | ############################################################################### | 
|---|
| 2 | # The communication module (communication.py) | 
|---|
| 3 | ############################################################################### | 
|---|
| 4 | import cPickle | 
|---|
| 5 | import socket | 
|---|
| 6 | import struct | 
|---|
| 7 |  | 
|---|
| 8 | marshall = cPickle.dumps | 
|---|
| 9 | unmarshall = cPickle.loads | 
|---|
| 10 |  | 
|---|
| 11 | def send(channel, args): | 
|---|
| 12 | #print "will send: ", args | 
|---|
| 13 | args+="\n" | 
|---|
| 14 | buf = args #marshall(args) | 
|---|
| 15 | #value = socket.htonl(len(buf)) | 
|---|
| 16 | value = str(len(buf)) | 
|---|
| 17 | #size = struct.pack("i",value) | 
|---|
| 18 | #rint "length: ",len(buf) | 
|---|
| 19 | #print "SIZE: ",repr(size) | 
|---|
| 20 | #channel.send(value) | 
|---|
| 21 | buf+="\0" | 
|---|
| 22 | channel.send(buf) | 
|---|
| 23 | #print 'sending: ',buf | 
|---|
| 24 | #print 'sent' | 
|---|
| 25 | def receive(channel): | 
|---|
| 26 |  | 
|---|
| 27 | size = struct.calcsize("L") | 
|---|
| 28 | size = channel.recv(size) | 
|---|
| 29 | print "rec size: ",size | 
|---|
| 30 | try: | 
|---|
| 31 | size = socket.ntohl(struct.unpack("i", size)[0]) | 
|---|
| 32 | #print "unpacked: ",size | 
|---|
| 33 | except struct.error, e: | 
|---|
| 34 | return '' | 
|---|
| 35 |  | 
|---|
| 36 | buf = "" | 
|---|
| 37 |  | 
|---|
| 38 | while len(buf) < size: | 
|---|
| 39 | buf = channel.recv(size - len(buf)) | 
|---|
| 40 |  | 
|---|
| 41 | return unmarshall(buf)[0] | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.