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