[CRIU] [PATCH 2/7] p.haul: split xem_rpc on server and client parts

Nikita Spiridonov nspiridonov at odin.com
Tue Oct 6 08:28:30 PDT 2015


Split xem_rpc on server (xem_rpc.py) and client (xem_rpc_client.py)
parts to simplify logic.

Signed-off-by: Nikita Spiridonov <nspiridonov at odin.com>
---
 phaul/p_haul_iters.py   |    4 +-
 phaul/xem_rpc.py        |   57 +++-------------------------------------------
 phaul/xem_rpc_client.py |   52 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 55 deletions(-)
 create mode 100644 phaul/xem_rpc_client.py

diff --git a/phaul/p_haul_iters.py b/phaul/p_haul_iters.py
index a7bcfe8..6050236 100644
--- a/phaul/p_haul_iters.py
+++ b/phaul/p_haul_iters.py
@@ -5,7 +5,7 @@
 import logging
 import images
 import mstats
-import xem_rpc
+import xem_rpc_client
 import pycriu
 import criu_api
 import criu_req
@@ -25,7 +25,7 @@ phaul_iter_grow_max = 10
 class phaul_iter_worker:
 	def __init__(self, p_type, host):
 		logging.info("Connecting to target host")
-		self.target_host = xem_rpc.rpc_proxy(host)
+		self.target_host = xem_rpc_client.rpc_proxy(host)
 		self.data_socket = self.target_host.open_socket("datask")
 
 		logging.info("Setting up local")
diff --git a/phaul/xem_rpc.py b/phaul/xem_rpc.py
index 8139b80..92963d6 100644
--- a/phaul/xem_rpc.py
+++ b/phaul/xem_rpc.py
@@ -1,3 +1,7 @@
+#
+# RPC server implementation
+#
+
 import socket
 import select
 import threading
@@ -14,59 +18,6 @@ RPC_CALL = 2
 RPC_RESP = 1
 RPC_EXC = 2
 
-#
-# Client
-#
-
-class _rpc_proxy_caller:
-	def __init__(self, sk, typ, fname):
-		self._rpc_sk = sk
-		self._fn_typ = typ
-		self._fn_name = fname
-
-	def __call__(self, *args):
-		call = (self._fn_typ, self._fn_name, args)
-		raw_data = repr(call)
-		self._rpc_sk.send(raw_data)
-		raw_data = self._rpc_sk.recv(rpc_sk_buf)
-		resp = eval(raw_data)
-
-		if resp[0] == RPC_RESP:
-			return resp[1]
-		elif resp[0] == RPC_EXC:
-			logging.info("Remote exception")
-			raise Exception(resp[1])
-		else:
-			raise Exception("Proto resp error")
-
-class rpc_proxy:
-	def __init__(self, conn, *args):
-		self._srv = conn
-		self._rpc_sk = self._make_sk()
-		util.set_cloexec(self._rpc_sk)
-		_rpc_proxy_caller(self._rpc_sk, RPC_CMD, "init_rpc")(args)
-
-	def __getattr__(self, attr):
-		return _rpc_proxy_caller(self._rpc_sk, RPC_CALL, attr)
-
-	def _make_sk(self):
-		sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-		sk.connect(self._srv)
-		return sk
-
-	def open_socket(self, uname):
-		sk = self._make_sk()
-		host = _rpc_proxy_caller(sk, RPC_CMD, "get_name")()
-		c = _rpc_proxy_caller(self._rpc_sk, RPC_CMD, "pick_channel")
-		c(host, uname)
-		return sk
-
-
-
-#
-# Server
-#
-
 class _rpc_server_sk:
 	def __init__(self, sk):
 		self._sk = sk
diff --git a/phaul/xem_rpc_client.py b/phaul/xem_rpc_client.py
new file mode 100644
index 0000000..07246c0
--- /dev/null
+++ b/phaul/xem_rpc_client.py
@@ -0,0 +1,52 @@
+#
+# RPC client implementation
+#
+
+import socket
+import logging
+import util
+import xem_rpc
+
+class _rpc_proxy_caller:
+	def __init__(self, sk, typ, fname):
+		self._rpc_sk = sk
+		self._fn_typ = typ
+		self._fn_name = fname
+
+	def __call__(self, *args):
+		call = (self._fn_typ, self._fn_name, args)
+		raw_data = repr(call)
+		self._rpc_sk.send(raw_data)
+		raw_data = self._rpc_sk.recv(xem_rpc.rpc_sk_buf)
+		resp = eval(raw_data)
+
+		if resp[0] == xem_rpc.RPC_RESP:
+			return resp[1]
+		elif resp[0] == xem_rpc.RPC_EXC:
+			logging.info("Remote exception")
+			raise Exception(resp[1])
+		else:
+			raise Exception("Proto resp error")
+
+class rpc_proxy:
+	def __init__(self, conn, *args):
+		self._srv = conn
+		self._rpc_sk = self._make_sk()
+		util.set_cloexec(self._rpc_sk)
+		c = _rpc_proxy_caller(self._rpc_sk, xem_rpc.RPC_CMD, "init_rpc")
+		c(args)
+
+	def __getattr__(self, attr):
+		return _rpc_proxy_caller(self._rpc_sk, xem_rpc.RPC_CALL, attr)
+
+	def _make_sk(self):
+		sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+		sk.connect(self._srv)
+		return sk
+
+	def open_socket(self, uname):
+		sk = self._make_sk()
+		host = _rpc_proxy_caller(sk, xem_rpc.RPC_CMD, "get_name")()
+		c = _rpc_proxy_caller(self._rpc_sk, xem_rpc.RPC_CMD, "pick_channel")
+		c(host, uname)
+		return sk
-- 
1.7.1



More information about the CRIU mailing list