- Socket Interface: returned objects are now serialized


git-svn-id: http://svn.sabayonlinux.org/projects/entropy/trunk@1512 cd1c1023-2f26-0410-ae45-c471fc1f0318
This commit is contained in:
(no author)
2008-03-25 11:59:31 +00:00
parent afce835433
commit b55728f7be
3 changed files with 69 additions and 8 deletions
+28 -6
View File
@@ -1,9 +1,25 @@
#!/usr/bin/python
import sys
import socket
serverHost = "localhost"
serverPort = 999
def spawn(cmd, silent = False):
import sys, os
import socket
sys.path.insert(0,'/usr/lib/entropy/libraries')
sys.path.insert(0,'/usr/lib/entropy/client')
sys.path.insert(0,'../libraries')
sys.path.insert(0,'../client')
try:
import cPickle as pickle
except ImportError:
import pickle
try:
import cStringIO as stringio
except ImportError:
import StringIO as stringio
import dumpTools
def spawn(cmd, silent = False, getobj = False):
# connect
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -28,12 +44,18 @@ def spawn(cmd, silent = False):
sys.stdout.write(x)
sys.stdout.flush()
x = s.recv(1024)
if getobj:
f = stringio.StringIO(data)
data = dumpTools.unserialize(f)
f.close()
s.close()
return data
# 1st step, get session
session = spawn("begin", silent = True)
print "session is:",session # XXX VALIDATE
print "session is:",session
# 2nd step, run the command
#s.send("reposync reponames=['sabayonlinux.org'] forceUpdate=True")
@@ -41,8 +63,8 @@ result = spawn("%s match 'x11-libs/qt'" % (session,), silent = True)
print "spawn result is:",result
# 3rd step, get rc
rc = spawn("%s rc" % (session,), silent = True)
print "REAL returned data:",rc
rc = spawn("%s rc" % (session,), silent = True, getobj = True)
print "REAL returned data:",rc,type(rc)
# 4th step, end session
rc = spawn("%s end" % (session,), silent = True)
+19
View File
@@ -47,6 +47,25 @@ def dumpobj(name, object, completePath = False):
f.close()
break
'''
@description: serialize object to f (file)
@input: object, file object
@output: file object, pointer to the beginning
'''
def serialize(object, f):
pickle.dump(object,f)
f.flush()
f.seek(0)
return f
'''
@description: unserialize file to object (file)
@input: file object
@output: object
'''
def unserialize(f):
x = pickle.load(f)
return x
'''
@description: load object from a file
+22 -2
View File
@@ -9337,6 +9337,7 @@ class SocketHostInterface:
self.progress( "Fetching "+str((round(float(self.average),1)))+"%"+kbprogress, back = True )
import socket
import dumpTools
def __init__(self, intf, *args, **kwds):
self.socketLog = LogFile(level = 2,filename = etpConst['socketlogfile'], header = "[Socket]")
@@ -9347,7 +9348,6 @@ class SocketHostInterface:
self.port = etpConst['socket_service']['port']
self.threads = etpConst['socket_service']['threads']
self.sessions = {}
self.socket.setdefaulttimeout(self.timeout)
# FIXME: add policy handling
self.valid_commands = [
@@ -9384,6 +9384,12 @@ class SocketHostInterface:
raise
self.SocketServer.listen ( self.threads )
def set_timeout(self):
self.socket.setdefaulttimeout(self.timeout)
def unset_timeout(self):
self.socket.setdefaulttimeout(0)
def get_new_session(self):
rng = str(int(random.random()*100000))
while rng in self.sessions:
@@ -9407,11 +9413,15 @@ class SocketHostInterface:
self.updateProgress('open: %s' % (details,))
self.conn_active = True
while self.conn_active:
self.set_timeout()
try:
data = self.channel.recv ( 1024 )
except self.socket.error, e:
self.conn_active = False
self.updateProgress('connection aborted: %s' % (e,))
self.unset_timeout()
break
self.updateProgress(" call: %s" % (data,))
@@ -9422,8 +9432,11 @@ class SocketHostInterface:
self.channel.close()
self.running = False
self.channel = None
self.unset_timeout()
break
self.unset_timeout()
# validate command
args = data.split()
session = args[0]
@@ -9525,7 +9538,14 @@ class SocketHostInterface:
def docmd_rc(self, session):
rc = self._get_rc(session)
self.channel.send(str(rc))
try:
import cStringIO as stringio
except ImportError:
import StringIO as stringio
f = stringio.StringIO()
self.dumpTools.serialize(rc, f)
self.channel.send(f.getvalue())
f.close()
return rc
def docmd_match(self, session, *myargs, **mykwargs):