This sample application demonstrates how to change the TCU activity state of all the application processors (machines) in the system
1. Implement a command response callback method
void commandResponse(ErrorCode error) {
if (error == ErrorCode::SUCCESS) {
std::cout << " Command executed successfully" << std::endl;
} else {
std::cout << " Command failed, errorCode: " << static_cast<int>(error) << std::endl;
}
}
2. Implement ITcuActivityListener and IServiceStatusListener interface
class MyTcuActivityStateListener : public ITcuActivityListener,
public IServiceStatusListener {
public:
void onMachineUpdate(const std::string machineName, const MachineEvent machineEvent);
void onSlaveAckStatusUpdate(const telux::common::Status status,
const std::string machineName, const std::vector<ClientInfo> unresponsiveClients,
const std::vector<ClientInfo> nackResponseClients) override;
void onServiceStatusChange(ServiceStatus status) override;
};
3. Get an instance of PowerFactory, and then obtain a TCU-activity manager instance with ClientType as MASTER
auto &powerFactory = PowerFactory::getInstance();
std::promise<telux::common::ServiceStatus> prom = std::promise<telux::common::ServiceStatus>();
ClientInstanceConfig config;
config.clientType = ClientType::MASTER;
config.clientName = "client_name_" + std::to_string(getpid());
config.machineName = ALL_MACHINES;
auto tcuActivityManager = powerFactory.getTcuActivityManager(config,
[&](telux::common::ServiceStatus status) {
std::cout << " Init Callback called " << std::endl;
prom.set_value(status);
});
4. Wait for the TCU-activity management services to be initialized and ready
if (tcuActivityStateMgr_ == nullptr) {
std::cout << " ERROR - Failed to get manager instance" << std::endl;
}
std::cout << " Waiting for TCU Activity Manager to be ready" << std::endl;
telux::common::ServiceStatus serviceStatus = prom.get_future().get();
5. Exit the application, if SDK is unable to initialize TCU-activity management service
if (serviceStatus == telux::common::ServiceStatus::SERVICE_AVAILABLE) {
std::cout << " *** TCU-activity management service is Ready *** " << std::endl;
} else {
std::cout << " *** ERROR - Unable to initialize TCU-activity management service" << std::endl;
return 1;
}
6. Instantiate MyTcuActivityStateListener and register for updates on the triggered TCU-activity state and its management service status
auto myTcuStateListener = std::make_shared<MyTcuActivityStateListener>();
tcuActivityManager->registerListener(myTcuStateListener);
tcuActivityManager->registerServiceStateListener(myTcuStateListener);
7. Set the TCU activity state (SUSPEND here) of all the machines in the system
telux::common::Status status = tcuActivityManager->setActivityState(TcuActivityState::SUSPEND, ALL_MACHINES, &commandResponse);
if (status == telux::common::Status::SUCCESS) {
} else {
std::cout << " *** ERROR - Failed to trigger TCU activity state change " << std::endl;
}
8. The below listener API is invoked to notify the consolidated acknowledgement status of all the SLAVE clients, corresponding to the machine that is undergoing the transition
void void MyTcuActivityStateListener::onSlaveAckStatusUpdate(const telux::common::Status status,
const std::string machineName, const std::vector<ClientInfo> unresponsiveClients,
const std::vector<ClientInfo> nackResponseClients) {
std::cout << "********* TCU-activity state update status and consolidate slave acknowledgement information *********" << std::endl;
if (status != telux::common::Status::SUCCESS) {
std::cout << " Analyze which clients have not responded or responded with nack " << std::endl;
if(unresponsiveClients.size() > 0) {
std::cout << " Number of unresponsive clients : "<< unresponsiveClients.size() << std::endl;
for (size_t i = 0; i < unresponsiveClients.size(); i++) {
std::cout << " client name : "<< unresponsiveClients[i].first
<< " , machine name : "<< unresponsiveClients[i].second << std::endl;
}
}
if(nackResponseClients.size() > 0) {
std::cout << " Number of clients responded with nack : "<< nackResponseClients.size()
<< std::endl;
for (size_t i = 0; i < nackResponseClients.size(); i++) {
std::cout << " client name : "<< nackResponseClients[i].first
<< " , machine name : "<< nackResponseClients[i].second << std::endl;
}
}
tcuActivityManager->setActivityState(TcuActivityState::RESUME, desiredVMName, &commandResponse);
}
}
9. Implement onServiceStatusChange callback to know when TCU-activity management service goes down
void MyTcuActivityStateListener::onServiceStatusChange(ServiceStatus status) {
std::cout << std::endl << "****** TCU-activity management service status update ******" << std::endl;
}