iot communication design - cool.ntu.edu.tw

41
IoT Communication Design Lecturer: Cheng-Jen Liu

Upload: others

Post on 27-Dec-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IoT Communication Design - cool.ntu.edu.tw

IoT Communication DesignLecturer: Cheng-Jen Liu

Page 2: IoT Communication Design - cool.ntu.edu.tw

Outline● Introdcution of REST

● Introduction of gRPC

● Introduction of MQTT

● Homework Description (5%)

Page 3: IoT Communication Design - cool.ntu.edu.tw

Communication Pattern (1) -- Client Server Paradigm

● Simple communication but heavy overhead

● Stateless communication

● General used in web technologies

Page 4: IoT Communication Design - cool.ntu.edu.tw

Communication Pattern (2) -- Peep-To-Peer Paradigm

● Flexible communication model (Peer can be either client/server)

● Stateful communication

● Hard to scalable

● Remote function call pattern

Page 5: IoT Communication Design - cool.ntu.edu.tw

Communication Pattern (3) -- Publish Subscribe Paradigm

● Decouple communication parties with central message broker

● Scalable Desgin

● Flexible Communication

Page 6: IoT Communication Design - cool.ntu.edu.tw

How Youtube Works (Discussion) ??● Web Service

○ Client Server Model

● Video Processing○ Peer-to-Peer Model

○ Pub-Sub Model

● Notification System○ Pub-Sub Model

Page 7: IoT Communication Design - cool.ntu.edu.tw

Q & AIs there any specific design/communication pattern fit all the scenarios ?

There is no one-solution-fit-all approach that can fit all the scenarios.

Usually, a project will combine lots of different communication patterns

to realize the application goal.

Page 8: IoT Communication Design - cool.ntu.edu.tw

Introduction of RESTREST (Rest API) is a common application interface used for softwares to export

their services to other softwares. It is not a protocol but an architectural style.

Generally, developers use REST to create website and export CRUD operations

to manipulate resource on the website.

Page 9: IoT Communication Design - cool.ntu.edu.tw

Let’s build a simple web service with Django and RESTWe are going to use a python web backend framework, called Django, to

realize a simple web service that allows clients to send GET requests and get

echoing response.

Page 10: IoT Communication Design - cool.ntu.edu.tw

Anatomy of Django Framework

Web Server (e.g. NGINX)

● Security Check● Authentication● Preprocessing

Route requests to associated function

Backend Logics (functions)

Page 11: IoT Communication Design - cool.ntu.edu.tw

Clone Django Tutorial ProjectType cmd `git clone https://github.com/johnnylord/django-rest-tutorial.git`.

Follow the instructions in the README file to run the project.

Page 12: IoT Communication Design - cool.ntu.edu.tw

How to check Django project (1) -- settings.py

Installed or defines apps

Rest API Framework Related Library

tutorial app containing backend logics

Page 13: IoT Communication Design - cool.ntu.edu.tw

How to check Django project (2) -- urls.py

mysite/urls.py

tutorial/urls.py

Target URL: http://localhost:8000/rest/tutorial

Start parsing

Reque

st will

be ro

ute to

here

Page 14: IoT Communication Design - cool.ntu.edu.tw

How to check Django project (3) -- views.py

tutorial/views.pyHandling GET request from client

Page 15: IoT Communication Design - cool.ntu.edu.tw

Runtime Snapshots (Server Logging)

GET Request from client

Page 16: IoT Communication Design - cool.ntu.edu.tw

Runtime Snapshots (Client Request)

Server Response Header

Server Response Body

Page 17: IoT Communication Design - cool.ntu.edu.tw

Q & A● How does Django route the client request to the assoicate view function ?

You need to define routing rules with regular experssions in the

urls.py file and specify associated view functions to the defined

rules.

● How to send file to the Django backend through REST API ?

Ask Google !!!

Page 18: IoT Communication Design - cool.ntu.edu.tw

Introduction of gRPCgRPC is a language agnostic, high-performance Remote Procedure Call (RPC)

framework. The main benefits of gRPC are: Modern, high-performance,

lightweight RPC framework. Contract-first API development, using Protocol

Buffers by default, allowing for language agnostic implementations

Page 19: IoT Communication Design - cool.ntu.edu.tw

What is Remote Procedure Call ?Remote Procedure Call (RPC) is a mechanism/concept that allow a process

running locally to be able to call functions provided by a remote process

running on another machine.

Page 20: IoT Communication Design - cool.ntu.edu.tw

What is Serialization ?Serialization is a process that convert object into a stream of bytes that can be

used to send between two running processes through network communication.

Page 21: IoT Communication Design - cool.ntu.edu.tw

What is Protobuf ?Protocol buffers (Protobuf) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data.

● It is a schema-based serialization method

● It has its own compiler to compile schema code to language-sepcific code

Page 22: IoT Communication Design - cool.ntu.edu.tw

Install the Protobuf CompilerVisit the installation webpage https://grpc.io/docs/protoc-installation/

Page 23: IoT Communication Design - cool.ntu.edu.tw

Let’s build a fibonacci service with gRPC and ProtobufType cmd `git clone https://github.com/johnnylord/gRPC-with-protobuf.git` and follow the README instruction.

Page 24: IoT Communication Design - cool.ntu.edu.tw

Protobuf & gRPC Schema File

service/fib.proto

gRPC Service interface

Request Data structure

Response Data structure

Page 25: IoT Communication Design - cool.ntu.edu.tw

Compile the schema fileAfter the compilation (`$ make`), the generated python wrapper of fibonacci grpc modules.

build/service/fib_pb2_grpc.py build/service/fib_pb2.py

Page 26: IoT Communication Design - cool.ntu.edu.tw

gRPC Servicer ImplementationThe generated gRPC Servicer is just an

abstract class, you need to define the

functionality by yourself.Interface needs to be implemented.

server.py

Page 27: IoT Communication Design - cool.ntu.edu.tw

gRPC Stub(client) to use remote service Typically, we use `stub` to indicate the access point of the remote service.

client.py

Remote Procedure Call

Instantiate request

Establish connection

Page 28: IoT Communication Design - cool.ntu.edu.tw

Q & A● What’s the difference between gRPC and JsonRPC ?

gRPC uses protobuf and JsonRPC uses json. The underlying

serialization method is different.

● What’s the advantage of schema-based serialization method ?

Schema-based serialization can compile the object into smaller

bytestreams. It doesn’t need to include field information in the

payload.

Page 29: IoT Communication Design - cool.ntu.edu.tw

What is MQTT ?MQTT (Message Queuing Telemetry Transport) is a publish/subscribe messaging protocol that works on top of the TCP/IP protocol. The first version of the protocol was developed by Andy Stanford-Clark of IBM and Arlen Nipper of Cirrus Link in 1999.

Page 30: IoT Communication Design - cool.ntu.edu.tw

AWS IoT Core MQTTMQTT is widely used in IoT Application. AWS even have dedicated services based on MQTT messaging protocol.

IoT Application System

Page 31: IoT Communication Design - cool.ntu.edu.tw

Eclispse Mosquitto MQTT BrokerEclispse Mosquitto is an open source project that implements the MQTT protocol and can be used as the message broker.

You can consider this as message broker.

Page 32: IoT Communication Design - cool.ntu.edu.tw

Possible IoT System Design with MQTT Broker (1)

HEAT

TEMP

MOIST

Data

Page 33: IoT Communication Design - cool.ntu.edu.tw

Possible IoT System Design with MQTT Broker (2)

HEAT

HEAT

HEAT

HEAT

Page 34: IoT Communication Design - cool.ntu.edu.tw

Let’s build a logging system with Eclipse MosquittoType cmd `git clone https://github.com/johnnylord/eclipse-mosquitto.git` and

follow the README instruction.

Page 35: IoT Communication Design - cool.ntu.edu.tw

MQTT CPU Publisher

publisher.py

Connect to MQTT Broker

Get system CPU usage

Publish CPU usage to MQTT broker

Page 36: IoT Communication Design - cool.ntu.edu.tw

MQTT Subscriber

subscriber.py

Connect to MQTT Broker

Subscribe topics of interest

Callback function when recive message from MQTT

broker

Page 37: IoT Communication Design - cool.ntu.edu.tw

Homework (5%): Mixture of Communication PatternYou have to implement a system with a simple fibonacci caculator capability and a logging feature as following:

Page 38: IoT Communication Design - cool.ntu.edu.tw

Server RequirementThe server is a backend web server implemented with the python Django

Framework. It must exports two REST Interfaces:

1. [POST] - /rest/fibonacci/

Client can launch POST request to ask for the result of fibonacci at N

order. The body content of POST is like { “order”: 10 }

2. [GET] - /rest/logs/

Client can launc GET request and get a list of history fibonacci requests

sent before. The result will be something like { “history”: [ 10, 5, …, 1 ] }

Page 39: IoT Communication Design - cool.ntu.edu.tw

Fibonacci RequirementYou have to implement a fibonacci caculator service with gRPC interface. The

caculator will accept an integer number from the server, and return the

caculated result to the server. Following is the formula of fibonacci function:

Page 40: IoT Communication Design - cool.ntu.edu.tw

Logging Requirement:The logging module will save the sent fibonacci order from the client request

through the subscribed topic on the message broker.

Whenever the server asks for the history information of fibonacci requests,

the logging module will respond the server with all the history of fibonacci

requests containing the order values.

Page 41: IoT Communication Design - cool.ntu.edu.tw

Handover rules (Deadline: 11/17)Please open a new repo on github, and finish the homework requirements. Fill your repo’s url into https://docs.google.com/spreadsheets/d/1HHp0L4ObcaZAn29K8CozpH3JXVHJLZG7j3EPqhUTBSg/edit?usp=sharing

1. You need to provide README to tell TA how to run your code2. A recorded video demonstrates the workflow of how the system (1)

process the fibonacci request, and (2) how the logging module logs all the history request at runtime.

(YOU NEED TO OPEN SEVREAL TERMINALS, AND SHOW THE SYSTEM WORKS IN RUNTIME)