Traffic management provides the capability of collecting traffic data based on the physical network, and supports traffic statistics based on NIC/UID.
Its functions include:
Supports traffic statistics based on NIC.
Supports traffic statistics based on app UID.
The following describes the development procedure specific to each application scenario.
Import the statistics, socket, and error code modules.
import { socket, statistics } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit'; Get the traffic data of the specified NIC.
Call getIfaceRxBytes and getIfaceTxBytes separately, and pass the NIC name to get the downlink and uplink traffic data of the specified NIC since the last startup.
// Obtain the real-time downlink data traffic of the primary Wi-Fi NIC. wlan0 is the name of the primary Wi-Fi NIC.
statistics.getIfaceRxBytes('wlan0').then((stats: number) => {
hilog.info(0x0000, 'testTag', JSON.stringify(stats));
// ...
})
.catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', JSON.stringify(err));
// ...
});
// ...
// Obtain the real-time uplink data traffic of the primary Wi-Fi NIC. wlan0 is the name of the primary Wi-Fi NIC.
statistics.getIfaceTxBytes('wlan0').then((stats: number) => {
hilog.info(0x0000, 'testTag', JSON.stringify(stats));
// ...
})
.catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', JSON.stringify(err));
// ...
});
// ... Obtain the cellular traffic data.
Call getCellularRxBytes and getCellularTxBytes separately to obtain the cellular downlink and uplink traffic data since the last startup.
// Obtain the real-time downlink data traffic of the cellular network.
statistics.getCellularRxBytes().then((stats: number) => {
hilog.info(0x0000, 'testTag', JSON.stringify(stats));
// ...
})
// ...
// Obtain the real-time uplink data traffic of the cellular network.
statistics.getCellularTxBytes().then((stats: number) => {
hilog.info(0x0000, 'testTag', JSON.stringify(stats));
// ...
})
// ... Obtain the traffic data of all NICs.
Call getAllRxBytes and getAllTxBytes separately to obtain the downlink and uplink traffic data of all NICs since the last startup.
// Obtain the real-time downlink data traffic of all NICs.
statistics.getAllRxBytes().then((stats: number) => {
hilog.info(0x0000, 'testTag', JSON.stringify(stats));
// ...
})
// ...
// Obtain the real-time uplink data traffic of all NICs.
statistics.getAllTxBytes().then((stats: number) => {
hilog.info(0x0000, 'testTag', JSON.stringify(stats));
// ...
})
// ... Get the traffic data of the specified app.
Call getUidRxBytes and getUidTxBytes separately, and pass the UID to get the downlink and uplink traffic data of the specified app since the last startup.
Here, the app UID 20010038 is used as an example. Replace it with the actual UID when calling.
let UID = 20010038;
// Obtain the real-time downlink data traffic of the specified application.
// ...
statistics.getUidRxBytes(UID).then((stats: number) => {
hilog.info(0x0000, 'testTag', JSON.stringify(stats));
// ...
})
// ...
// Obtain the real-time uplink data traffic of the specified application.
// ...
statistics.getUidTxBytes(UID).then((stats: number) => {
hilog.info(0x0000, 'testTag', JSON.stringify(stats));
// ...
})
// ... Obtain the traffic data of the specified socket.
Call getSockfdRxBytes and getSockfdTxBytes separately, and pass the socket FD to get the downlink and uplink traffic data of the specified socket.
// Obtain the real-time downlink data traffic of the specified socket.
let tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
// ...
tcp.getSocketFd().then((sockfd: number) => {
statistics.getSockfdRxBytes(sockfd).then((stats: number) => {
hilog.info(0x0000, 'testTag', JSON.stringify(stats));
// ...
}).catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', JSON.stringify(err));
// ...
});
})
// ...
// Obtain the real-time uplink data traffic of the specified socket.
tcp.getSocketFd().then((sockfd: number) => {
statistics.getSockfdTxBytes(sockfd).then((stats: number) => {
hilog.info(0x0000, 'testTag', JSON.stringify(stats));
// ...
}).catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', JSON.stringify(err));
// ...
});
})
// ...