Beruflich Dokumente
Kultur Dokumente
PROGRAMMING
FOR
IOT
LESSONS’ SUMMARY
1
1.4 IoT challange
With IoT we have the challenge to implement internet web services over small tiny objects
and over low bit-rate wireless networks. All these small devices are connected to a gateway
which can be a Raspberry PI and the communication can be over Wi-Fi (on 2.4 GHz or 5
GHz). The information acquired by the small devices, for example sensors, are sent to the
collector which is connected to a station and then to a data center that can be anywhere and
precesses the data received. All the sensors are connected in a local network called Personal
Area Network (PAN). PAN is a small network with a range of few meters, typically located
in a house.
Middleware is a software that manages the information and the connections to the devices
(sensors) in a way such that admins can modify these data, while users have some limitations.
It also implements some form of security and authentication. Another useful task that the
Middleware can do is translating incoming information to make them understandable to the
IoT system, so you can connect any type of device to your system.
2
value occurs, then a message is received. The subscription is possible thanks to the "Broker":
a software that keeps track of subscriptions. It is a mediator that sends only the information
of your interest, for which you have subscribed. This mechanism uses a protocol called MQTT
which will be explained more in section 5.
3
1.10.1 Logical Organization of the XML Document
An XML document may include declarations, processing instructions and comments, an
example is shown below:
4
1.10.4 DTD: element declaration
Element Model let specify name, order, optionality and multiplicity of nested elements by
a simple grammar.
(a) (b)
(a)
5
1.12 JSON - JavaScript Object Notation
JSON is a lightweight text based data exchange format, it is completely language inde-
pendent, it is easy to understand/manipulate and generate and it is NOT a markup language.
Just like XML it is made in plain text formats, hierarchical and self-describing (human read-
able). Unlike XML, JSON is lighter and faster, uses typed objects and much less syntax. This
is the structure of a JSON file:
The data types used are Booleans true or false and also Null (nothing or no value), while
for the numbers we use:
6
2 CherryPy
2.0.1 Introduction and first web application
CherryPy is a versatile web-framework that allows to eas-
ily translate object-oriented programming (made of classe) to
web applications, so we can run applications that work on
the net across multiple devices. This is how you mount a
first a application [13]. CherryPy to run this application
that returns the string "Hello World!", has to tun a web
server that uses the HTTP protocol. In our case the server is
located on the localhost at http://127.0.0.1:8080/ where
"8080" is the port. As we said before, the web application
Figure 13: First web application
was originally a class and in our case it is called "class
HelloWorld(object)", to put it on the server we have to
make it visible with the line "@cherrypy.expose". When we run the code with CherryPy,
our terminal says that the "ENGINE Bus STARTED", meaning that the web server has been
enabled for our Python application [14].
7
2.0.3 First web application with different URLs and parameters
We can also pass parameters input using the URL and the "?" means that we are pass-
ing some new values and we specify the name of the parameter and its value by typing
"http://127.0.0.1:8080/generate?lenght=16". Notice in [16] how among the function
"generate" there’s a parameter "length" so we need to specify it in the URL.
Figure 16: Example with different parameters and a value passed through URL
As we saw in [16] we are passing through the URL the value 16, but we must convert it
since it is a string, that’s why there’s a conversion of the parameter "int(length)".
8
2.0.5 Providing static resources
Web applications almost always make use of javascript, files, images or videos and CherryPy
provides support to serve static content to end-users. These resources are for example files
useful to define the UI (User Interface) of the web application that we are developing. Since
these static resources are in our hard disk, we need to specify the directory in the conf section
by writing: tools.staticdir.root. The following line is needed to get dynamically where is
running my application tools.staticdir.root:os.path.abspath(os.get.cwd()). We then
need to do the mapping so that when we type this URL: localhost:8080/static the web
application will know that it has to read the static files; to do so we add as is it presented
in [19] the lines ’tools.staticdir.on’:True and ’tools.staticdir’.dir:’./public’. In
this example we have a simple HTML file where there’s a static element which is style.css.
• PUT ——–> Update or Modify or Replace a document, it does more than GET because it can update a file;
Methods GET and POST are both used to submit data, but POST allows to transfer larger
quantities of data.
From now on, our Python application will be a web service since our class will became
REST Methods and the cherrypy.expose becomes exposed = True as we can see from [20].
Moreover we will use an HTTP compliant engine instead of the default option with CherryPy
by adding the line ’request.dispatch’:cherrypy.dispatch.MethodDispatcher(). So we
force the responses content-type to be text/plain and we finally ensure that GET requests will
only be responded to clients that accept that content-type by having an Accept: text/plain
9
header set in their request. However, we do this only for that HTTP method as it wouldn’t
have much meaning on the other methods.
2.2.2 PARAMETERS
In a URL, the section after ? is called a query-string.
? is unique for the whole URL. The query-string is used
to contextualize the URL by passing a set of (key, value)
pairs.
10
If we look at [22] we can see that the parameters can
be managed as a dictionary.
(a) Application without "/string" (b) Application with "/string" (c) Application with "/string"
and "/conf"
11
Figure 25: RESTful applications in Python
•1xx - Informational •2xx - Successful •3xx - Redirection •4xx - Client Error •5xx - Server Error
12
2.5.3 HTTP status codes: 5xx – Server Error
This class indicates cases in which the server is aware that it has erred or is incapable of
performing the request. The server SHOULD include an entity containing an explanation of
the error situation, and whether it is a temporary or permanent condition.
13
3 Web Programming
3.1 Webserver and webclient
The world of IoT is based on the web and it is crucial to focus on the difference between a
WebServer and a WebClient. With a certain URL we reach a specific machine on the web while
with a certain port we reach a specific application, for example: http://192.168.4.232:8080.
Overall, this is the mechanism we use to reach a remote point in the network. The communi-
cation between the Server and the Client happens thanks to the HTTP protocol, which is based
on REST. In all IoT applications REST is largely used, but we will focus on other alternatives
such as MQTT.
14
3.4 Web Service and IoT
The client can be, in our IoT system, a browser. If I want to visualize all the data from
my sensors, I can do it through a browser. The client will connect to a webserver, which is
the device itself and a small computer such as a Raspberry, which provides some intelligence
to the device, can be used to run the webservice. Our Raspberry will send information to
another webserver located in another device that maybe has more sensors. In general what
we have to keep in mind is that:
• A device will implement a webserver in a certain language using a web library
– E.g. python with cherrypy, Java, etc. . .
• Through the webservices the device can be:
– Configured
– Send data/commands
– Receive data/commands
• E.g. using GET/POST methods
• The client can be another device or a data collector node
• Webserver should be light
15
3.5 How POST works
1. The browser first hits the web application at /hello but it sends a GET, so our Genera-
tor.hello method runs and returns the hello_form (see Figure [33a] and [33b]).
2. You fill out the form in the browser, and the browser does what the <form> says and
sends the data as a POST (see Figure [33c]).
3. The web application then runs the Generator.reply method rather than the Genera-
tor.hello method to handle this request (see Figure [33d]).
4. This index.POST method then does what it normally does to send back the hello page
like before (see Figure [33e])
(a) (b)
(c) (d)
(e)
16
4 Connectivity
When we choose the technology for our IoT environment we have to take into account
different aspects, from battery lifetime to power consumption and costs.
In terms of frequency we have two alternatives: the Sub-GHz and the 2.4GHz transceiver.
Lots of standards, like Bluetooth and WiFi, use the 2.4GHz, so many manufacturers assume
that the 2.4GHz is the de facto transceiver frequency.
Typically, the higher the frequency, the higher the bit-rate, that is why WiFi doesn’t use
Sub-GHz frequencies. A 2.4GHz network supports data rates greater than 1Mbps. On the
other side, Bluetooth, while using 2.4GHz, supports small data rate and it is used to connect
small devices in a small range. The advantage of Bluetooth with respect to WiFi is the
lower power consumption, but in Bluetooth it is not possible to implement authentication and
protection as in WiFi. 2.4GHz is used by Bluetooth also because the size of the antenna is
smaller than the one needed for Sub-GHz networks (high frequency means small wavelength
and small wavelength means small antenna). However, a 2.4GHz radio has limited range
(so it is suited to connect devices in a small range), poor wall penetration and high power
consumption.
Sub-GHz networks require a bigger antenna, but it has less problems of compatibility with
other networks. Moreover, Sub-GHz frequencies have a higher capability of wall penetration
and are useful to setup a stable channel, without interference. These characteristics make
Sub-GHz networks ideal for IoT devices like sensors, that need to last many years on a single
battery, work with low bit-rates and need to have wide coverage.
Considering a typical situation in which the PC will work, we have a wireless network with
a gateway. The gateway is connected to the network, which is connected through an Ethernet
protocol (using the MAC layers) and, through a WAN, it is possible to reach the server.
The more complex is the protocol, the more time and energy it takes to transmit/receive a
packet. This complexity generates an overload in the transmission management, that is why
in IoT applications we need to be careful of the complexity of the network layer. With device
consuming small amount of energy and having low computational capabilities, we can’t afford
a complex protocol.
Networking concepts:
4.1 Protocols
In terms of PAN, on the data link layer, one of the most used protocols in IoT applications
is the 802.15.4. It is a physical+MAC layer protocol, used for connecting wireless nodes and
17
IoT devices in a short range. If we want to use 802.15.4 in a network of objects interconnected
to the internet, we must be able to use the Internet Protocol (IPv4 or IPv6) on top of 802.15.4.
IP is typically used for WiFi (802.11) or Ethernet (802.3), not for small sensors networks.
On top of IP we need the transport protocol, which can be either UDP, TCP or ICMP. UDP
is a lightweight protocol, but it is less reliable than TCP and it doesn’t have retransmission.
TCP is more reliable, but it is heavier in terms of protocol overhead.
For IoT applications we prefer IPv6 because of the larger number of available addresses.
In fact, one of the main problems in sensor networks is that each sensor needs to have its own
unique ID and IPv4 has not enough addresses.
The 802.15.4 protocol has a data rate of 20-25Kbps, in comparison with the 11Mbps of
Wifi or the 200Mbps of Ethernet. In terms of energy consumption there is a big difference,
because devices working with 802.15.4 have a lifetime of hundreds/thousands of days, while
WiFi nodes lasts for a time between a fraction of a day and 5 days. In terms of the physical
layer specification, this protocol works with QPSK modulation. 802.15.4 has been designed
for low power, large scale and low cost.
4.3 Fragmentation
Fragmentation is needed because we can’t fit a whole IP packet into a PAN packet, so we
need to divide the payload in slices. When performing the fragmentation, we lose information
about the sequence number of packets and we won’t be able to reconstruct them in the correct
order. This means we need additional information in order to be able to rebuild the correct
sequence, the tag information and the offset information.
The tag information is used to associate all the fragments belonging to the same packet.
The offset information is a sequence identifying the order in which the packet should be put.
Then for each fragment, we have also the size information, telling us how large the packet is.
Fragments of a packet may be lost during transmission, so we use a 60 seconds timeout in
order to avoid that the receiver waits forever for the missing fragment in the sequence. If the
timeout expires the packet is discarded.
18
4.4 Link layer routing
A functionality we need to support is the link layer routing, i.e. the capability of routing in
a mesh network. This functionality is implemented at the PAN level, because one hop at the
IP level may mean many hops at the link layer. In order to implement this we need additional
information, which are the link address of the next hop and the number of remaining hops.
5 MQTT
MQTT (Message Queuing Telemetry Transport) is a machine-to-machine connectivity pro-
tocol. It is designed as an extremely lightweight publish/subscribe messaging transport pro-
tocol used for IoT.
It is useful to send small quantities of real time data. To visualize these data it is possible
to use a REST HTTP connection. It consumes a small amount of power, the protocol stack is
simple and the code footprint (size) is very small. It supports a QoS (Quality of Service), so
it is possible to implement reliable messaging, including retransmission. This makes MQTT
suited to work on unreliable networks, like the mobile network.
The publish/subscribe paradigm is the most important feature of MQTT. While the stan-
dard request/response paradigm requires an overhead, while the pub/sub is more like the
interrupt system. The subscriber subscribes for a certain type of data it is interested in and
waits for the publisher to publish those data, without making any request. This means there
is no waste of bandwidth for implementing the polling-based mechanism.
Moreover, the MQTT protocol keeps the connection open using very little power. The
overhead is very small (2 bytes) and this reduces the power consumption needed to keep the
connection between the devices.
19
Looking at the network stack, we always have the physical/MAC layer, the IP layer and
the transport layer (TCP/UDP). If we use REST, we have the HTTP protocol on top of which
we implement REST. If we use MQTT we don’t need HTTP, MQTT takes the place of both
HTTP and REST. On top of MQTT we can put whatever data format we want (JSON, XML,
...). An alternative to HTTP is CoAP, a lightweight protocol for IoT based on 6LowPAN.
Possible stacks:
• PHY/MAC → IPv6 → TCP/UDP → HTTP → REST → JSON/XML/...
• PHY/MAC → IPv6 → TCP/UDP → MQTT → JSON/XML/...
• PHY/MAC → 6LowPAN → TCP/UDP → CoAP
5.1 Publish/Subscribe
The concept of publish/subscribe is like an interrupt-based system, while reequest/response
is a polling mechanism. Publish/subscribe is an asynchronous communication that allows the
development of loosely-coupled event-based systems.
With the pub/sub paradigm we have a direct link between client and server, so it removes
the dependencies between producer and consumer of information. This direct communication
is broken by the message broker, a third actor that is aware both of who produces the infor-
mation and who needs it. So there are at least three actors in a pub/sub system: the broker,
the publisher and the subscriber.
• Broker: central system that receives data from the sensors/publishers and forwards them
to all the interested subscribers; it’s an advanced interrupt controller
• Publisher: device (like sensor) that publishes the data
• Subscriber: client that makes a subscription to the broker for certain types of data
The different kinds of data a client can subscribe to are called topics. The publisher sends
a message with a certain topic (every message must have a topic), so that the broker can
forward it correcty to the interested clients.
The communication is event-based because we never know when a certain information is
sent by the publisher. When a publisher sends information, it creates an event, the broker
recognizes that something was raised by the publisher and forwards it to the subscribers. This
means that the pub/sub approach can react in real-time.
It is a scalable communication system, this means that new actor can come in the system.
A single broker can manage a huge number of subscribers and publishers.
MQTT was designed for real-time data, so it has a support for Quality of Service. In
MQTT QoS means how the information is sent and how it is received. The higher the level of
QoS, the higher the latency. It is possible to specifiy the type of QoS we want:
• QoS 0: the message is delivered once or never; no guarantees about the message delivery
from the publisher to the broker
• QoS 1: the message can be delivered once or more, we have to ensure that it is delivered
at least once; if the data is lost, there is a mechanism asking for retransmission; in this
case it is possible that multiple copies of the same data are received (because ACK may
be lost)
20
• QoS 2: the message is delivered exactly once; it is based on two levels of ACK (an
handshake between broker and subscriber/publisher before sending the information)
The publisher defines the QoS level each time it sends a message, the subscriber defines it only
once at the moment of subscribing for a topic.
The broker acts also as a buffer, storing the data, so that the subscriber can retrieve the
data if lost.
21
5.3 MQTT model
In the MQTT terminology, the subscribers and the publishers are the clients, while the
broker is the server.
The MQTT client is implemented by a library installed on top of CherryPy. This library
will exploit an already present transport layer network stack. On the logical network we will
have a TCP connection level and an MQTT session level.
The MQTT session identifies a connection between a client and the server. It will allow
the server to create a list of subscriptions from the client. Each time the broker receives a
messages, it forwards it to the queue corresponding to a certain topic.
A subscription is also a connection between a client and the broker, but it is defined as an
attachment of the client to a topic. The subscription can be transient or durable:
• transient: it ends with the session, so there is a 1-to-1 correspondence between the
subscription and the session; when the session is closed, the subscriber is detached from
the network; messages sent when the connection is close are lost.
• durable: it spans over multiple sessions; if the client will be disconnected from the
network, the subscription will last; the messages delivered during the disconnection
period will be received when the client reconnects.
The topic system in MQTT is hierarchical. This hierarchy is built up using "/", for
example: sensor/sensorID/information. In order to subscribe to multiple information it is
possible to use wildcards: "+" or "#".
• "+": for a single level of the hierarchy, for example sensor/1234/+ gives us all the
messages coming from the sensor with sensorID=1234; it can be used in any level of the
hierarchy
• "#": for all the remaining levels of the hierarchy; it can only end the topic string, for
example sensor/# gives us all the messages from all the sensors
When the RETAIN bit is 1, it means that the published message will be saved and when
a new client subscribes to the topic, the retain message will be sent.
22
5.5 Callback
Each method of the MQTT library is associated with a callback. A callback is a func-
tion that is executed as a consequence of some events. This means that the callback is an
asynchronous function.
Example: when the connection happens, it will trigger the on_connect() callback.
5.6 Mosquitto
The message broker Mosquitto is open source and implements two versions of MQTT:
v3.1 and v3.1.1. The client library provided to establish the communication with this broker
is called Paho.
There are seven steps to establish the communication:
1. create a client instance
2. the client instance must connect to the broker using the connect() method
3. call a loop function to maintain the network traffic flow with the broker
4. use the subscribe() method to subscribe to a topic
5. use the publish() method to publish messages to the broker
6. use the unsubscribe() method before disconnecting
7. use the disconnect() method to disconnect from the broker
The Paho Client() is the client contractor object that will manipulate and read all the
connection with the broker. The Paho Client() receives the following inputs:
• client_id: string used to identify client connecting to the broker
• client_session: boolean that determines the client type, if True the broker will remove
all information about this client when it disconnects, if False it means the subscription
is durable
The connection is managed through the connect() function, that receives four parameters:
• host: hostname or IP address of the broker
• port: default is 1883, but the connection has no authentication or any sort of protection,
a protected connection can be set on port 8883
• keepalive: maximum period in seconds allowed between two communications with the
broker, if the broker doesn’t receive anything after this interval, it will consider the client
dead
• bind_address: IP address of a local network interface to bind the client to
Paho loop functions help maintaining alive the connection between the client and the
broker. The loop function must be called, otherwise the connection could be closed in an
unpredictable way. There are different loop functions (and they should not be mixed):
• loop(timeout 6= 0, max_packets = 1): calls regularly to process network events; it
waits until the network socket is ready, then handles the incoming/outgoing data; every
timeout the loop function sends a message to the broker saying that the client is alive,
so the timeout value must be lower than the keepalive value of the connect() function
23
• loop_start() & loop_stop(): implements a multithreading approach, started by the
loop_start and killed by the loop_stop
• loop_forever(): blocks the application and never ends until the disconnect() function
is invoked
Once the connection is opened, Paho implements the publish and subscribe methods:
• if the broker is another PC in our local network, then the broker address is the IP address
of that PC
• if the broker is a public one, then we use the address of the server, like iot.eclipse.org
The line client1 = mqtt.Client() creates an instance of MQTT client and we have all
the methods related to it, like client1.on_connect=on.connect, where we attach a callback
called on_connect.
With client1.loop_start() we start processing the callbacks, the code goes on and when
the event attached to the callback happens, the callback function will be executed.
24
Figure 36: Python script that creates a connection
25
Figure 38: Python script of pub/sub with two different clients
6 Microservices
In the past (10 − 15 years ago) the application design approach was MONOLITHIC :
the application was a single executable that performed all the functionalities. When these
functionalities started to grow in number, this approach revealed itself to be not adequate.
• not scalable
26
6.1.3 Not scalable
A monolithic software had different components, one to deal with a single feature, and one
single database. To make it scalable, the whole application had to be replicated in different
servers, but here a huge problem of synchronization rises: all the databases have to work in
sync, if one crashes, the whole system is down.
In the end all the bugs and the long time to wait for an update is frustrating for the customer.
Moreover, nowadays the keywords for any application are FLEXIBILITY and SCALABIL-
ITY.
• the codes are light and can run in parallel, so they can yield the result is less time
• each code is easy to build and debug and the teams can work separately: this also cuts
the releasing and updating time of the app
• the service is easily scalable and very flexible (we can add new features just writing a
small piece of code that exchanges data with the others)
• it is clear who is the owner of the single component and security is improved because
the developer can hide some data
• the user doesn’t need to buy expensive devices (each task requires little computational
power)
• each small software can deploy a different language and technology (the developer is free
to choose the most appropriate for the single task)
27
6.4 Principles behind the microservice approach
• Each service relies on each other’s public API. Developers and users must define formats
and APIs (agree on the way they exchange data ). This is a crucial point, because if we
change format or API our software will not be able to exchange data with the others.
• Use the right tools for the specific task. Since applications communicate using public
APIs and a predefined data-format, application A doesn’t need to know what kind of
technology, language is using application B, nor if there were any updates.
The new challenge with this approach is to have a good architecture design phase, so to avoid
chaos and bottlenecks among microservices.
• Scalability : this means that the service can be scaled up or down to include different
devices, merge a huge amount of uses and data.
• The system must evolve over time rapidly and with no/small impact on the architecture.
• Modularity : services must be built so that they can be combined between them.
• Decentralization : this is the case of the distributed system. The service is broken
into different microservices localized in different parts of the world but linked through
the internet.
• Flexibility : services must be built so that new requirements can be added without
having a strong impact on the rest of the platform.
28
• Standardization : this encourages data exchange by exploiting common interfaces and
open data-formats.
8 Middleware
In the Italian dictionary we find this definition of Middleware: "Insieme di software che
fungono da intermediari fra strutture e programmi informatici, permettendo loro di comunicare
a dispetto della diversità dei protocolli o dei sistemi operativi".
A Middleware is a computer software that provides services to software applications
beyond those available in the operating system.
• it consists of different software components and it’s a distributed software: it can run in
parallel on different computers
• its purpose is to make easy the communication among protocols and operating systems
• it allows the user to retrieve informations/data from a database, know how the system
evolves, detect which devices belonging to our platform are connected to the internet.
• lies between operating system and applications on each side of our network
• Resource is the abstraction of the "real" resource (sensors,actuators) belong to the IoT
by a device and hosted by the device. Ith has a state that can be manipulated (turned
on/off)
• Service is a network service available on the network (e.g. Web Service, Message Broker)
providing a protocol endpoint
29
8.1.2 LocalConnect
LocalConnect has three components: Device Connector, Service Catalog and Re-
source Catalog.
The Device Connector is the one that provides interoperability among heterogeneous
devices. It stands at the edge of the platform and it implements the following functionalities:
• Provide communication with devices over the network via MQTT, HTTP/REST, etc.
The Device Gateway is an implementation of the Device Connector. It acts like a gate-
way between the low-level hardware access protocol and a TCP/IP network. Its components
are shown in Figure 39.
• Device Agent : implements the low-level communication with the devices using differ-
ent interface protocols (interoperability: it can translate from a protocol to another)
• Process Manager : executes the right device agent, depending on user settings (knows
which device agent is connected to a specific request)
30
8.1.5 Communication Flow
Here is described the information flow necessary to a device to become part of the network
to an application to exploit the network functionalities (see Figure 40).
The Resource Catalog can be disconnected, in this case , the Service Catalog must remove the
RC from its available resources.
8.2 GlobalConnect
Since LocalConnect works in a private (routable) network, GlobalConnect acts as a Border
Gateway that exposes the local network to the internet by enabling transparent communica-
tion of application and services (see Figure 41).
31
9 IoT software infrastructure
Today the IoT ecosystems are composed of various software stack running on host comput-
ers , such as servers or cloud systems. These systems are used for data processing,storage,network
management, authentication and as registry for services and devices.
A certain number of middlewares and platforms (either commercila or open) are available
and they support, among others, data stream processing, complex event processing, machine
learning, big data analytics.
Platform as a Service (PaaS) is a category of cloud computing services that provides an
already made platform, so that costumers can develop, run and manage applications without
having to build the whole infrastructure.
PaaS can be either a public cloud service (in this case the consumer has minimal configu-
ration options), or a private service inside a firewall.
Beside PaaS, there are other categories of similar services: Software as a Service (SaaS)
which is a software that runs on a cloud and Infrastructure as a Service (Iaas) that allows
the costumer to rent the infrastructure. PaaS lies between the Saas and IaaS.
Some examples of Paas are:
• Bluemix by IBM
• ThingSpeak by Mathworks
• Microsoft Azure
9.1 ThingSpeak
ThingSpeak allows applications and devices to send data streams on the cloud, collect and
analyse them and trigger reactions. Operations like create/update/delete a channel can be
done using the REST API calls (GET, POST, PUT, DELETE); update the channel feed can
be done using REST or MQTT-publish (the channel is given an ID).
32
Figure 42: Service Life Cycle is a loop
A Use case is a list of actions or event steps, typically defining interactions between
an actor (human or external system) and a system, to achieve a goal. Use case analysis is
fundamental in software engineering!
Services can vary in properties and level of complexity based on the applicable use case.
A general Service Life Cycle is a loop of the kind represented in Figure 42
• Service Definition : the service is described highlighting the main features and func-
tionalities
• Service Delivery : the service is continuously monitored to ensure meeting the perfor-
mance requirements; potential improvements are identified
• Service Re-Definition : after the decommission, the loop starts again for updating the
service
11 Containers
A Container is a software technology to implement microservices. As it is shown in
Figure 43 each virtual machine needs a different Operating System, while using containers it is
possible to build different applications over the same Operating System (like this a developer
doesn’t need a virtual machine for each microservice).
The original Linux container is known as LXC and it is a system-level virtualization method
for running multiple isolated systems (applications) on a single host (as explained in the
definition above). Moreover, a container can be moved across any Linux server that supports
the container environment.
33
Figure 43: Structure of a system without and with containers
11.1 Dockers
Docker is a project to build LXC containers that can run a single process and make
containers more flexible; it basically brings cloud-like flexibility to any infrastructure capa-
ble of running containers. Docker containers allow to deploy, replicate, move and back-up a
work even more quickly and easily.
Docker Container
If an environment consists of If an environment consists of
N processes, the developer needs N N processes, the developer needs 1
containers (each one with a single process) container (containing all the process)
• when a container is moved from one host to another the environment for the application
remains the same
• it’s not easy to make small changes to an application: it’s better to start a new updated
docker
In practice, the first step is to install Docker and then activate the container engine.
When the engine is running, different commands (provided by the Docker) can be launched
from the command line. The simplest thing is to try "hello-world" by typing
where "docker" is the Docker environment, a sort of interpreter; "run" is one of the possible
commands and "hello-world" is the name of the image (this is not an executable but an
image). The Docker will check if there is an "hello-world" image and run it; like this it
creates an instance of the image. The instance is a container and it’s possible to create many
containers out of the same image.
34
12 Multithreading in Python
Threads are often called "light-weight" processes because operating systems require fewer
resources to create and manage threads rather than processes. Their characteristics are:
• multiple threads within a process share the same dataspace with the main thread, there-
fore they can easily communicate with each other (more than if they were in different
chunk of memory)
• threads don’t require much memory overhead: they are cheaper than processes
• it can be interrupted (pre-empted) can be temporary put asleep (on hold); this is called
yielding
In python the interpreter controls all the threads of a program and the Global Interpreter
Lock (GIL) ensures that the interpreter runs only one thread at a time.
To use threads in Python one has to import threading library; it is also possible to use the
threading.Thread class and use its methods, which are:
• run() : entry point of a thread; it implements the tasks that the thread performs
35
A newly created thread begins its life cycle in the born state; after invoking the start
method, the thread is placed in the ready state. After that, the run method obtains the GIL
and enters the running state. When the run method returns or terminates (for any reason),
the thread enters the dead state. While waiting for a resource to be available, a thread enters
the blocked state; if a thread calls the time.sleep function, it releases the GIL and enters the
sleeping state. All these states (except the dead state) return to the ready state as soon as a
condition is met.
If thread A is running and calls thread’s B join method, it forfeits the GIL and waits
thread B to die before proceeding.
A Deadlock happens when one or more threads wait forever for an event that cannot
occur; an Indefinite postponement happens when when one or more threads is delayed for
an unpredictably long time.
36
Figure 46: Thread Example1
all the other threads can’t access them simultaneously. Module threading provides sev-
eral thread-synchronization mechanism (locks and condition variables). For example, class
threading.RLock clreates lock objects; a lock can enter the locked state by calling method
acquire (notice that only one thread can acquire lock at a time) and is unlocked when calling
method release.
12.3 Semaphores
A Semaphore is a variable that controls access to common resources or to a critical
section.
It basically maintains a counter that specifies the maximum number of threads that can
access a resource simultaneously; this counter is decreased each time the resource is accessed
and when it reaches zero no other thread can access the resource until it has been released by
another thread.
37
Figure 47: Thread Example1 Output
38
Figure 48: Thread Example2
39
Figure 51: Semaphores Example output
Artificial neurons of first generation can only give digital outputs, but they can easily deal
with data that have digital input and output. They need a hidden layer because with
40
a single layer only a threshold is used, but for some functions (like XOR) more than one
threshold is needed.
41