[spp] [PATCH v2 2/3] controller: fix encoding for socket

ogawa.yasufumi at lab.ntt.co.jp ogawa.yasufumi at lab.ntt.co.jp
Wed May 23 22:11:49 CEST 2018


From: Yasufumi Ogawa <ogawa.yasufumi at lab.ntt.co.jp>

For python3, it is required to treat str and bytes explicitly for
sending and receiving messages via socket.

SPP controller has several socket interfaces between primary and
secondary processes. It means that messages must be encoded before
sending or decoded after receiving vise versa.

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi at lab.ntt.co.jp>
---
 src/controller/conn_thread.py | 11 ++++++-----
 src/controller/shell.py       |  6 +++---
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/controller/conn_thread.py b/src/controller/conn_thread.py
index bffdcee..e524e84 100644
--- a/src/controller/conn_thread.py
+++ b/src/controller/conn_thread.py
@@ -55,7 +55,7 @@ class ConnectionThread(threading.Thread):
                 # 1024 stands for bytes of data to be received
                 data = self.conn.recv(1024)
                 if data:
-                    msg = "%s" % data
+                    msg = "%s" % data.decode('utf-8')
                     spp_common.SEC2MAIN[self.client_id].put(msg)
                 else:
                     spp_common.SEC2MAIN[self.client_id].put(
@@ -95,7 +95,7 @@ class AcceptThread(threading.Thread):
         """Get client_id from client"""
 
         try:
-            conn.send("_get_client_id")
+            conn.send(b'_get_client_id')
         except KeyError:
             return -1
 
@@ -105,7 +105,7 @@ class AcceptThread(threading.Thread):
 
         if logger is not None:
             logger.debug("data: %s" % data)
-        client_id = int(data.strip('\0'))
+        client_id = int(data.decode('utf-8').strip('\0'))
 
         if client_id < 0 or client_id > spp_common.MAX_SECONDARY:
             logger.debug("Failed to get client_id: %d" % client_id)
@@ -138,7 +138,8 @@ class AcceptThread(threading.Thread):
         if free_client_id < 0:
             return -1
 
-        conn.send("_set_client_id %u" % free_client_id)
+        msg = "_set_client_id %u" % free_client_id
+        conn.send(msg.encode('utf-8'))
         data = conn.recv(1024)
 
         return free_client_id
@@ -239,7 +240,7 @@ class PrimaryThread(threading.Thread):
                     data = conn.recv(1024)
                     if data:
                         spp_common.PRIMARY2MAIN.put(
-                            "recv:%s:{%s}" % (str(addr), data))
+                            "recv:%s:{%s}" % (str(addr), data.decode('utf-8')))
                     else:
                         spp_common.PRIMARY2MAIN.put("closing:" + str(addr))
                         conn.close()
diff --git a/src/controller/shell.py b/src/controller/shell.py
index 6bd9cb0..dc6eb34 100644
--- a/src/controller/shell.py
+++ b/src/controller/shell.py
@@ -142,7 +142,7 @@ class Shell(cmd.Cmd, object):
         """Send command to primary process"""
 
         if spp_common.PRIMARY:
-            spp_common.MAIN2PRIMARY.put(command)
+            spp_common.MAIN2PRIMARY.put(command.encode('utf-8'))
             recv = spp_common.PRIMARY2MAIN.get(True)
             print(recv)
             return self.CMD_OK, recv
@@ -155,7 +155,7 @@ class Shell(cmd.Cmd, object):
         """Send command to secondary process with sec_id"""
 
         if sec_id in spp_common.SECONDARY_LIST:
-            spp_common.MAIN2SEC[sec_id].put(command)
+            spp_common.MAIN2SEC[sec_id].put(command.encode('utf-8'))
             recv = spp_common.SEC2MAIN[sec_id].get(True)
             if command == 'status':
                 self.print_sec_status(recv)
@@ -233,7 +233,7 @@ class Shell(cmd.Cmd, object):
 
         if (rcmd == spp_common.REMOTE_COMMAND):
             param = result + '\n' + message
-            spp_common.RCMD_RESULT_QUEUE.put(param)
+            spp_common.RCMD_RESULT_QUEUE.put(param.encode('utf-8'))
         else:
             if logger is not None:
                 logger.debug("unknown remote command = %s" % rcmd)
-- 
2.17.0



More information about the spp mailing list