The sample server provides a package search server for checking update packages and obtaining the update package download URLs, which was previously unavailable in the real-world update service. The sample server supports update service testing and secondary development function verification, building an end-to-end environment to cater for diverse update service use cases.
## How to Develop
1. Generate an SSL certificate.
Generate the **serverKey.pem** and **serverCert.cer** files for SSL communication of the sample server.
Go to the **update_updateservice** directory and run the following commands to create a code directory:
```
mkdir server_sample // Create the server_sample folder.
touch server_sample/BUILD.gn // Create the BUILD.gn file.
mkdir server_sample/include // Create the include folder to store the header file of the sample server.
touch server_process.h // Create the server_process.h header file.
mkdir server_sample/src // Create the src folder to store the C/C++ files of the sample server.
touch server_sample/src/server_process.c // Create the server_process.c file.
touch server_sample/src/main.cpp // Create the main.cpp file.
```
4. Write the **BUILD.gn** file.
The **BUILD.gn** file contains two **ohos** components: **ohos_shared_library** file named **libserver_process.z.so** and **ohos_executable** file named **testserver**.
Declare the sample server APIs in the **server_process.h** file.
```c++
#ifndef __SERVER_PROCESS_H__
#define __SERVER_PROCESS_H__
/*
Init: creates a socket environment and presets certain attributes.
*/
int Init();
/*
SetParam: sets all plug-in parameters.
*/
int SetParam(const char *key, const char *value);
/*
GetParam: obtains all plug-in parameters.
*/
int GetParam(const char *key, char *value);
/*
ReverseSetParamCallback: callback.
*/
int ReverseSetParamCallback(int(*setParam)(const char *key, const char *value));
/*
Open: starts the service.
*/
int Open();
/*
MainLoop: invoked every 100 ms.
*/
int MainLoop();
/*
Close: stops the service and releases related resources.
*/
int Close();
#endif //__SERVER_PROCESS_H__
```
6. Write the **server_process.c** and **main.cpp** files.
In the **server_process.c** file, mainly declare **respondContent**, the format of the response message returned by the server. Write the **main.cpp** file based on instructions for the common SSL protocol server. Be sure to include related header files and load the **serverKey.pem** and **serverCert.cer** files.
```c
#include "server_process.h"
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "openssl/err.h"
#include "openssl/ssl.h"
#define SERVER_PEM "/data/sdcard/serverKey.pem" // Use an absolute path.
#define SERVER_CER "/data/sdcard/serverCert.cer" // Use an absolute path.
"\"content\": \"This package message is used for sampleContent\""
"}]"
"}";
```
7. Start building.
The **testserver** and **libserver_process.z.so** files are added to the build output directory.
8. Develop an update package.
For details, see the [update_packaging_tools repository](https://gitee.com/openharmony/update_packaging_tools).
9. Start the package search server.
Create a directory that contains only English characters on the development board. Place the **testserver**, **libserver_process.z.so**, **serverCert.cer**, and **serverKey.pem** files in the directory, go to the directory, and run the following command to start the package search server:
The sample server provides a simple server instance for deploying update packages. It can be used as an auxiliary test environment for the UpdateService subsystem.
## Basic Concepts
- Package search service: one of the service capabilities provided by the UpdateService. It depends on the server that supports the TCP and SSL protocols.
- Package search server: a server that provisions the package search service through the TCP connection and the SSL protocol. The sample server mentioned in this document is such a package search server.
- Download server: an HTTP server.
- update.serverip.search: a system parameter that indicates the IP address of the package search server configured on the UpdateService. The default value is **127.0.0.1**.
## Constraints
The following is an example of the JSON response returned by the server. Note that the **verifyInfo** field indicates the SHA-256 value of the update package, and the **size** field indicates the size of the update package, in bytes.