1# Sample Server Development 2 3## When to Use 4 5The 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 functional verification for secondary development, building an end-to-end environment to cater for diverse update service use cases. 6 7## How to Develop 8 91. Generate an SSL certificate. 10 11 Generate the **serverKey.pem** and **serverCert.cer** files for SSL communication of the sample server. 12 13 ``` 14 openssl req -newkey rsa:2048 -nodes -keyout serverKey.pem -x509 -days 365 -out serverCert.cer -subj "/C=CN/ST=GD/L=GZ/O=abc/OU=defg/CN=hijk/emailAddress=test.com" 15 ``` 16 17 18 192. Modify the **bundle.json** file. 20 21 Add **sub_component** to the **build** field. 22 23 ``` 24 "sub_component": [ 25 "//base/update/updateservice/server_sample:testserver", 26 ... 27 ], 28 ``` 29 303. Create a code directory. 31 32 Go to the **update_updateservice** directory and run the following commands to create a code directory: 33 34 ``` 35 mkdir server_sample // Create the server_sample folder. 36 touch server_sample/BUILD.gn // Create the BUILD.gn file. 37 mkdir server_sample/include // Create the include folder to store the header file of the sample server. 38 touch server_process.h // Create the server_process.h header file. 39 mkdir server_sample/src // Create the src folder to store the C/C++ files of the sample server. 40 touch server_sample/src/server_process.c // Create the server_process.c file. 41 touch server_sample/src/main.cpp // Create the main.cpp file. 42 ``` 43 444. Write the **BUILD.gn** file. 45 46 The **BUILD.gn** file contains two **ohos** components: **ohos_shared_library** file named **libserver_process.z.so** and **ohos_executable** file named **testserver**. 47 48 ``` 49 import("//build/ohos.gni") 50 51 ohos_shared_library("server_process") { 52 sources = [ 53 "//base/update/updateservice/server_sample/src/server_process.c", 54 ] 55 56 include_dirs = [ 57 "//base/update/updateservice/server_sample/include", 58 "//third_party/openssl/include", 59 ] 60 61 deps = [ 62 "//base/update/updater/services/log:libupdaterlog", 63 "//third_party/bounds_checking_function:libsec_static", 64 "//third_party/openssl:crypto_source", 65 "//third_party/openssl:ssl_source", 66 "//utils/native/base:utils", 67 ] 68 69 part_name = "update_service" 70 } 71 72 ohos_executable("testserver") { 73 sources = [ 74 "//base/update/updateservice/server_sample/src/main.cpp", 75 ] 76 77 include_dirs = [ 78 "//base/update/updateservice/server_sample/include", 79 ] 80 81 deps = [ 82 "//base/update/updateservice/server_sample:server_process", 83 ] 84 85 part_name = "update_service" 86 } 87 ``` 88 895. Write the **server_process.h** file. 90 91 Declare the sample server APIs in the **server_process.h** file. 92 93 ```c++ 94 #ifndef __SERVER_PROCESS_H__ 95 #define __SERVER_PROCESS_H__ 96 97 /* 98 Init: creates a socket environment and presets certain attributes. 99 */ 100 int Init(); 101 102 /* 103 SetParam: sets all plug-in parameters. 104 */ 105 int SetParam(const char *key, const char *value); 106 107 /* 108 GetParam: obtains all plug-in parameters. 109 */ 110 int GetParam(const char *key, char *value); 111 112 /* 113 ReverseSetParamCallback: callback. 114 */ 115 int ReverseSetParamCallback(int(*setParam)(const char *key, const char *value)); 116 117 /* 118 Open: starts the service. 119 */ 120 int Open(); 121 122 /* 123 MainLoop: invoked every 100 ms. 124 */ 125 int MainLoop(); 126 127 /* 128 Close: stops the service and releases related resources. 129 */ 130 int Close(); 131 132 #endif //__SERVER_PROCESS_H__ 133 ``` 134 1356. Write the **server_process.c** and **main.cpp** files. 136 137 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. 138 139 ```c 140 #include "server_process.h" 141 142 #include <netinet/in.h> 143 #include <sys/types.h> 144 #include <sys/socket.h> 145 #include <arpa/inet.h> 146 #include <unistd.h> 147 #include <stdlib.h> 148 #include <string.h> 149 #include <stdio.h> 150 151 #include "openssl/err.h" 152 #include "openssl/ssl.h" 153 154 #define SERVER_PEM "/data/sdcard/serverKey.pem" // Use an absolute path. 155 #define SERVER_CER "/data/sdcard/serverCert.cer" // Use an absolute path. 156 157 #define LOG_PRINT(fmt, ...) printf("[ServerProcess][%s:%d] " fmt "\n", __func__, __LINE__, ##__VA_ARGS__) 158 #define DO_CHECK(cond, log, ...) \ 159 if (!(cond)) {\ 160 LOG_PRINT(log);\ 161 __VA_ARGS__;\ 162 return -1;\ 163 } 164 165 // Implement the function by referring to the APIs in the server_process.h file. Pay attention to the format of the response message from the server. 166 respondContent = "{" 167 "\"searchStatus\": 0," 168 "\"errMsg\": \"success\"," 169 "\"checkResults\": [{" 170 "\"versionName\": \"sampleVersionName\"," 171 "\"versionCode\": \"sampleVersionCode\"," 172 "\"verifyInfo\": \"sampleVerifyInfoSha256Value\"," 173 "\"size\": 1234567," 174 "\"packageType\": 1," 175 "\"descriptPackageId\": \"abcdefg1234567ABCDEFG\"," 176 "}]," 177 "\"descriptInfo\": [{" 178 "\"descriptionType\": 0," 179 "\"content\": \"This package message is used for sampleContent\"" 180 "}]" 181 "}"; 182 ``` 183 1847. Start building. 185 186 The **testserver** and **libserver_process.z.so** files are added to the build output directory. 187 1888. Develop an update package. 189 190 For details, see the [update_packaging_tools repository](https://gitee.com/openharmony/update_packaging_tools). 191 1929. Start the package search server. 193 194 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: 195 196 ``` 197 ./testserver ./libserver_process.z.so & 198 ``` 199