Soft Patch Panel
 help / color / mirror / Atom feed
From: ogawa.yasufumi@lab.ntt.co.jp
To: ferruh.yigit@intel.com, spp@dpdk.org
Cc: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
Subject: [spp] [PATCH 2/3] controller: fix encoding for socket
Date: Tue, 15 May 2018 10:20:03 +0900	[thread overview]
Message-ID: <20180515012004.8726-3-ogawa.yasufumi@lab.ntt.co.jp> (raw)
In-Reply-To: <20180515012004.8726-1-ogawa.yasufumi@lab.ntt.co.jp>

From: Yasufumi Ogawa <ogawa.yasufumi@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@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 8b2f8cd..2ec4e2a 100644
--- a/src/controller/conn_thread.py
+++ b/src/controller/conn_thread.py
@@ -56,7 +56,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(
@@ -96,7 +96,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
 
@@ -106,7 +106,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)
@@ -139,7 +139,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
@@ -240,7 +241,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 b04cc62..137c42b 100644
--- a/src/controller/shell.py
+++ b/src/controller/shell.py
@@ -143,7 +143,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
@@ -156,7 +156,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)
@@ -234,7 +234,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

  parent reply	other threads:[~2018-05-15  1:20 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-15  1:20 [spp] [PATCH 0/3] Update for Python 3 support ogawa.yasufumi
2018-05-15  1:20 ` [spp] [PATCH 1/3] controller: change importing for python3 ogawa.yasufumi
2018-05-15  1:20 ` ogawa.yasufumi [this message]
2018-05-15  1:20 ` [spp] [PATCH 3/3] docs: add description for python 3 support ogawa.yasufumi
2018-05-23 20:11 ` [spp] [PATCH v2 0/3] Update for Python " ogawa.yasufumi
2018-05-23 20:11   ` [spp] [PATCH v2 1/3] controller: change importing for python3 ogawa.yasufumi
2018-05-23 20:11   ` [spp] [PATCH v2 2/3] controller: fix encoding for socket ogawa.yasufumi
2018-05-23 20:11   ` [spp] [PATCH v2 3/3] docs: add description for python 3 support ogawa.yasufumi
2018-05-24 13:18   ` [spp] [PATCH v2 0/3] Update for Python " Ferruh Yigit

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180515012004.8726-3-ogawa.yasufumi@lab.ntt.co.jp \
    --to=ogawa.yasufumi@lab.ntt.co.jp \
    --cc=ferruh.yigit@intel.com \
    --cc=spp@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).