Error while posting message to ioMessage


(Josemaria Thomas) #1

While trying to post a message via web socket to ioMessage, an exception is raised which says “an integer is required”.
Below is the code snippet used to post the message

msg = IoMessage()
msg.infotype = ‘temperature’
msg.infoformat = ‘decimal/celsius’
msg.version = 4
contentdata = json.dumps(random.uniform(0, 100))
msg.contentdata = bytearray(contentdata.encode())
print(msg.contentdata)
try:
client.post_message_via_socket(msg)
except IoFogException as ex:
print(“Exception while posting message : {}”.format(ex))

The output is given below:

bytearray(b’89.45243235579701’)
Exception while posting message : an integer is required


#2

Could you please provide some more details?
This code looks valid, probably there’s some issue with client/config initialization.
Also you can try to get traceback and post it here which would be helpful:

import traceback

...

try:
    client.post_message_via_socket(msg)
except IoFogException as ex:
    print(“Exception while posting message : {}”.format(ex))
    traceback.print_exc()

(Josemaria Thomas) #3

Traceback (most recent call last):
File “main.py”, line 57, in simulate_temperature
client.post_message_via_socket(msg)
File “/usr/local/lib/python3.6/site-packages/iofog_python_sdk/client.py”, line 132, in post_message_via_socket
raise IoFogException(e)
iofog_python_sdk.exception.IoFogException: an integer is required
Traceback (most recent call last):
File “/usr/local/lib/python3.6/site-packages/iofog_python_sdk/client.py”, line 130, in post_message_via_socket
self.message_ws_client.send_message(io_msg)
File “/usr/local/lib/python3.6/site-packages/iofog_python_sdk/wsclient.py”, line 161, in send_message
self.send(util.prepare_iomessage_for_sending_via_socket(io_msg), binary=True)
File “/usr/local/lib/python3.6/site-packages/iofog_python_sdk/util.py”, line 53, in prepare_iomessage_for_sending_via_socket
msg_bytes = io_msg.to_bytearray()
File “/usr/local/lib/python3.6/site-packages/iofog_python_sdk/iomessage.py”, line 81, in to_bytearray
body.extend(self.publisher)
TypeError: an integer is required


#4

It looks like SELFNAME environment variable has non-integer type. This environment variable is generated inside ioFog and should always be integer. Try to change SELFNAME value to some integer if you are testing microservice outside running ioFog.


(Josemaria Thomas) #5

I am testing this microservice inside ioFog env.
Will adding an integer value to the publisher in IoMessage solves the problem?
msg = IoMessage()
msg.infotype = ‘temperature’
msg.infoformat = ‘decimal/celsius’
msg.version = 4
msg.publisher = 1


#6

This one may work, but it will break messaging between microservices inside ioFog because ioFog uses this internal microservice identifier for communication between microservices.
ioFog appends environment variable SELFNAME to each new docker container during its start, so it should be some configuration-related issue.
You may try to enter running microservice using docker exec -ti sh and check this SELFNAME environment variable using printenv command.


(Josemaria Thomas) #7

Adding msg.publisher = 1 didn’t work.
SELFNAME env variable has microservice_id as its value.

docker ps
CONTAINER ID        IMAGE                             COMMAND             CREATED             STATUS              PORTS                      NAMES
bec4b1063118        josemaria02/iofogsender:4         "python main.py"    11 minutes ago      Up 11 minutes       0.0.0.0:5000->5000/tcp     9JF9LTfG3bvGT3t7KzT6TQHj93FY6FcQ

Entered microservice container using below command.
docker exec -it bec4b1063118 sh

Checked SELFNAME env variable.
/home/iofog # printenv SELFNAME
9JF9LTfG3bvGT3t7KzT6TQHj93FY6FcQ


#8

Looks valid too.
Should be related to ioFog python SDK client initialization, it converts SELFNAME into publisher field inside.

Need more info here, probably you can upload this docker image to docker hub and then we’ll debug this image locally.


(Josemaria Thomas) #9

Please try pulling josemaria02/iofogsender:4


#10

It works for me (sent POST request to http://localhost:5000/message with such body {“message”:“testMessage”} and got 200 response code and “Success”).

Steps:

  1. iofog-controller catalog add -u <user_id> -n testPythonSdk -x josemaria02/iofogsender -g 1
  2. iofog-controller microservice add -n pythonSdkTest -c <catalog_id> -F <flow_id> -I <iofog_uuid> -g ‘{}’ -r -u <user_id> // enabled host mode using “r” argument

Code works fine, so you may try some of these options:

  1. restart ioFog Agent
  2. stop microservice using docker stop <microservice_uuid> and ioFog Agent will launch it again
  3. deactivate flow using iofog-controller flow update -i <flow_id> -D , wait till ioFog Agent stops all microservices (could be visible using docker ps or in ioFog Agent logs) and then activate flow again using iofog-controller flow update -i <flow_id> -a

(Josemaria Thomas) #11

These options didn’t work for me.
After debugging, found that bytearray().extend("string") doesn’t work work in python3.
bytearray().extend() always expects integer.
body.extend(self.publisher.encode()) worked perfectly.

With this change, I was able to send and receive a message successfully through ioMessage channel.


#12

Great, also thanks for this catch, we’ve added python3 support to ioFog pythonSDK recently and we’ll fix this issue soon.


#13

Fixed this issue and released new version of ioFog pythonSDK