Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
ECE 448/528 – Application Software Design
Lecture 09 HTTP Support for IoT Simulator
Professor Jia Wang
Department of Electrical and Computer Engineering
Illinois Institute of Technology
February 9, 2022
1/13 ECE 448/528 – Application Software Design, Spring 2022, Dept. of ECE, IIT
Outline
Object Oriented HTTP Server Design
HTTP Support for IoT Simulator
2/13 ECE 448/528 – Application Software Design, Spring 2022, Dept. of ECE, IIT
Reading Assignment
I This lecture: 9, 3, Project 2
I Next lecture: Observer and Pub/Sub Patterns
3/13 ECE 448/528 – Application Software Design, Spring 2022, Dept. of ECE, IIT
Outline
Object Oriented HTTP Server Design
HTTP Support for IoT Simulator
4/13 ECE 448/528 – Application Software Design, Spring 2022, Dept. of ECE, IIT
Control IoT Simulator via Web Pages
I In our simple HTTP server design, RequestProcessor serves
local files based on the path.
I One may generate content for HTTP response dynamically,
without refering to any actual file.
I E.g. to create a report for the status of a smart plug for the
path /plugName.
I One may additionally utilize the query to instruct the server to
act differently.
I E.g. to switch a plug on by /plugName?action=on.
I Can we reuse JHTTP and RequestProcessor?
I With minimal modifications.
I Not just for our Project 2 but flexible enough for other
purposes.
5/13 ECE 448/528 – Application Software Design, Spring 2022, Dept. of ECE, IIT
Polymorphism
I Allow RequestProcessor to react differently for different
purpose.
I Possible method: Inheritance
I Move code related to file handling in RequestProcessor to a
new method.
I New classes may inherit (extends) RequestProcessor and
override this method to provide different services.
I Update the JHTTP class as needed to create objects of such
new classes instead of RequestProcessor.
I Limitations: no multiple inheritance in Java
I Each Java class can only extend one superclass.
I A choice made by Java: do not use inheritance (if possible).
6/13 ECE 448/528 – Application Software Design, Spring 2022, Dept. of ECE, IIT
Inversion of Control (IoC)
I An alternative method to support polymorphism in Java.
I Usual flow: an application uses a library by calling functions
provided by the library.
I E.g. socket programming.
I Inversion of Control (IoC): a library allows an application to
provide code that the library will call.
I A.k.a. callbacks in many other languages.
I Java interface
I Help to define what the library expects from the application.
I May contain one or more methods, each identifies one desired
input/output behavior without any implementation.
7/13 ECE 448/528 – Application Software Design, Spring 2022, Dept. of ECE, IIT
Updated HTTP Server Design
I In the ece448.iot_sim.http_server package of the project
code.
I JHTTP: allow to run in its own thread.
I We will have other things to do in the main thread so cannot
allow JHTTP to wait for clients there.
I RequestHandler: a Java interface for HTTP request
handling
I The method handleGet corresponds to a GET request: the
inputs are path and query params as key-value pairs, the
output is a HTML page in String.
I RequestProcessor
I Extract a single request. Parse path and query.
I Let RequestHandler to generate a response.
I Send the response back.
I Generate error response when necessary.
I RequestProcessor will learn what RequestHandler to use
from JHTTP.
I JHTTP will depend on the application to provide it.
8/13 ECE 448/528 – Application Software Design, Spring 2022, Dept. of ECE, IIT
Outline
Object Oriented HTTP Server Design
HTTP Support for IoT Simulator
9/13 ECE 448/528 – Application Software Design, Spring 2022, Dept. of ECE, IIT
Implementing RequestHandler
I Our IoT simulator need to implement the RequestHandler
interface for the HTTP server.
I ece448.iot_sim.HTTPCommands
I Use a TreeMap to search for a particular plug.
I List all plugs.
I Report plug status.
I Need code from you to switch plug on/off etc.
I How to write unit tests for it?
10/13 ECE 448/528 – Application Software Design, Spring 2022, Dept. of ECE, IIT
Concurrency and Synchronization
I ece448.iot_sim.MeasurePower: instruct plugs to measure
their own power consumption every one second.
I Requests to switch a plug on or off may come at the same
time.
I A single PlugSim object may need to be accessed concurrently
from multiple threads because of the above possibilities.
I Synchronization: an object shared by many threads need to be
protected by a lock.
I So that at any moment, at most one thread can access it.
I Each Java object has an intrinsic lock, and it can be activated
via synchronized methods.
I All methods of PlugSim that read and write its state are
synchronized.
11/13 ECE 448/528 – Application Software Design, Spring 2022, Dept. of ECE, IIT
Putting Everything Together
I Our simulator now begins to have many moving parts.
I PlugSim’s for the plugs.
I A MeasurePower object to control power measurements.
I A JHTTP server to process HTTP requests.
I A HTTPCommands object to actually handle HTTP requests for
our application.
I ece448.iot_sim.Main takes care to initialize these objects
properly before starting various threads.
I Indeed, we will still follow the same steps when starting a more
complex application.
12/13 ECE 448/528 – Application Software Design, Spring 2022, Dept. of ECE, IIT
Summary
I Inversion of control.
I Concurrency and synchronization.
13/13 ECE 448/528 – Application Software Design, Spring 2022, Dept. of ECE, IIT