wcWebCamClient lib  v0.8.2
lib_test/main.cpp
/*===============================================================*/
/* This is an example of how to use the wcWebCamClient library. */
/* Simple class for parsing command line */
/* commandline_tools.h */
/* */
/* Part of wcWebCamClient library project */
/* */
/* Copyright 2022 Ilya Medvedkov */
/*===============================================================*/
#ifndef COMMLINE_TOOLS_H_INCLUDED
#define COMMLINE_TOOLS_H_INCLUDED
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
class InputParser{
public:
InputParser (int &argc, char **argv){
for (int i=1; i < argc; ++i)
this->tokens.push_back(std::string(argv[i]));
}
const std::string& getCmdOption(const std::string &option) const{
std::vector<std::string>::const_iterator itr;
itr = std::find(this->tokens.begin(), this->tokens.end(), option);
if (itr != this->tokens.end() && ++itr != this->tokens.end()){
return *itr;
}
static const std::string empty_string("");
return empty_string;
}
bool cmdOptionExists(const std::string &option) const{
return std::find(this->tokens.begin(), this->tokens.end(), option)
!= this->tokens.end();
}
const std::string& getHost() {
static const std::string CMD_HOST("--host");
static const std::string CMD_HOST_SYN("-u");
const std::string & res = getCmdOption(CMD_HOST);
if (res.empty()) {
return getCmdOption(CMD_HOST_SYN);
} else
return res;
}
const std::string& getProxy() {
static const std::string CMD_PROXY("--proxy");
static const std::string CMD_PROXY_SYN("-p");
const std::string & res = getCmdOption(CMD_PROXY);
if (res.empty()) {
return getCmdOption(CMD_PROXY_SYN);
} else
return res;
}
const std::string& getDevice() {
static const std::string CMD_DEVICE("--device");
static const std::string CMD_DEVICE_SYN("-d");
const std::string & res = getCmdOption(CMD_DEVICE);
if (res.empty()) {
return getCmdOption(CMD_DEVICE_SYN);
} else
return res;
}
const std::string& getUserName() {
static const std::string CMD_USER_NAME("--name");
static const std::string CMD_USER_NAME_SYN("-n");
const std::string & res = getCmdOption(CMD_USER_NAME);
if (res.empty()) {
return getCmdOption(CMD_USER_NAME_SYN);
} else
return res;
}
const std::string& getUserPassword() {
static const std::string CMD_USER_PWRD("--pwrd");
static const std::string CMD_USER_PWRD_SYN("-x");
const std::string & res = getCmdOption(CMD_USER_PWRD);
if (res.empty()) {
return getCmdOption(CMD_USER_PWRD_SYN);
} else
return res;
}
bool ignoreTLSCert() {
static const std::string OPT_TLS_IGNORE("-k");
return cmdOptionExists(OPT_TLS_IGNORE);
}
bool showHelp() {
static const std::string OPT_SHOW_HELP("-h");
return cmdOptionExists(OPT_SHOW_HELP);
}
virtual void printCommonHelp() {
if (tokens.size() > 0)
std::cout << "Usage:" << tokens.at(0) << " [options...]" << std::endl;
std::cout << " -u, --host URL for server host in format https://hostname:port" << std::endl;
std::cout << " -p, --proxy Proxy in format [[username][:password]@][proxyurl:port]" << std::endl;
std::cout << " -d, --device Device name" << std::endl;
std::cout << " -n, --name Login name" << std::endl;
std::cout << " -x, --pwrd Login password" << std::endl;
std::cout << " -k Ignore TLS certificate errors" << std::endl;
std::cout << " -h Show this help" << std::endl;
}
private:
std::vector <std::string> tokens;
};
#endif // COMMLINE_TOOLS_H_INCLUDED

This is an example of how to use the wcWebCamClient library. In this example, a client is created, authorized on the server, and downloads a list of active devices.

/*===============================================================*/
/* This is an example of how to use the wcWebCamClient library. */
/* In this example, a client is created, authorized on the */
/* server, and downloads a list of active devices. */
/* */
/* Part of wcWebCamClient library project */
/* */
/* Copyright 2022 Ilya Medvedkov */
/*===============================================================*/
#include <iostream>
#include <wcwebcamclient.h>
#include <chrono>
#include <thread>
#include "../commline_tools.h"
using namespace std;
static uint8_t running = 0;
/* Callback. CURL was initialized successfully. */
void onCURLinit(wcHandle self, wcTask task)
{
cout << "CURL initialized" << endl;
}
/* Callback. Authorization was successful. */
void onAuth(wcHandle self, wcTask task)
{
char * res = NULL;
/* Get a new session id and display it on the screen. */
if (wcClientGetStrValue(self, wcstSID, &res) == WC_OK) {
if (res) {
cout << res << endl;
free(res);
}
}
running = 2;
}
/* Callback. The client has been disabled. */
void onDisconnect(wcHandle self, wcTask task)
{
cout << "Client disconnected" << endl;
running = 5;
}
/* Callback. Connection state changed. */
void onConnChanged(wcHandle self, int state)
{
cout << "Client connection changed " << state << endl;
}
/* Callback. The list of devices was changed. */
void getDevices(wcHandle self, wcTask task, const char * jArr)
{
/* Display a list of online devices on the screen. */
cout << jArr << endl;
running = 4;
}
/* Callback. Log was changed. */
void onLog(wcHandle self, const char * str)
{
/* Display new log entry. */
cout << str << endl;
}
int main(int argc, char * argv[])
{
/* Parse command line */
InputParser input(argc, argv);
const std::string &host = input.getHost();
const std::string &proxy = input.getProxy();
const std::string &user = input.getUserName();
const std::string &pwrd = input.getUserPassword();
std::string device = input.getDevice();
if (device.empty()) device = "device_test001";
if (input.showHelp()) {
/* Show help doc */
input.printCommonHelp();
return 0;
}
if (!host.empty()){
/* Main part */
/* Configure certificate */
if (input.ignoreTLSCert())
/* Configure proxy */
if (!proxy.empty())
wcClientSetStrValue(client, wcstProxy, proxy.c_str());
/* Configure host */
wcClientSetStrValue(client, wcstHostName, host.c_str());
/* Configure callbacks */
wcSetNotifyCallback(client, wccbkInitCURL, onCURLinit);
wcSetNotifyCallback(client, wccbkDisconnect, onDisconnect);
wcSetConnCallback(client, onConnChanged);
/* Start client */
wcClientStart(client);
int time_out = 100;
while (running < 4)
{
switch (running)
{
case 0:
{
/* Authorize client */
wcClientSetStrValue(client, wcstDeviceName, device.c_str());
wcClientAuth(client, user.c_str(), pwrd.c_str());
running = 1;
break;
}
case 2:
{
/* Update list of active devices */
running = 3;
break;
}
default:
{
break;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
/* Proceed client */
wcClientProceed(client);
time_out--;
if (time_out <= 0) {
cout << "timeout" << endl;
break;
}
}
/* Disconnect client */
/* Destroy client */
wcClientDestroy(client);
return 0;
} else {
cout << "should contain at least --host (host-name) option";
}
}
wcRCode DLLEXPORT wcSetTaskCallback(wcHandle client, wcCallback callbackId, TaskNotifyLibFunc func)
Set specified task notify callback for client.
wcRCode DLLEXPORT wcSetNotifyCallback(wcHandle client, wcCallback callbackId, NotifyEventLibFunc func)
Set specified notify callback for client.
wcRCode DLLEXPORT wcSetCStringCallback(wcHandle client, wcCallback callbackId, CStringNotifyLibFunc func)
Set specified notify callback for client to handling the C-style string values.
wcRCode DLLEXPORT wcSetJSONStrCallback(wcHandle client, wcCallback callbackId, JSONStrNotifyEventLibFunc func)
Set specified notify callback for client to process the JSON-formatted response results represented a...
wcRCode DLLEXPORT wcSetConnCallback(wcHandle client, ConnNotifyEventLibFunc func)
Set specified connection notify callback for client.
@ wccbkAddLog
Added new log entry.
Definition: wcwebcamclient.h:140
@ wccbkInitCURL
Successful initialization of the multiCURL handle.
Definition: wcwebcamclient.h:135
@ wccbkSuccessAuth
Successful authorization.
Definition: wcwebcamclient.h:136
@ wccbkSuccessUpdateDevices
The request to update list of online devices has been completed.
Definition: wcwebcamclient.h:154
@ wccbkDisconnect
Client has been disconnected.
Definition: wcwebcamclient.h:138
wcRCode DLLEXPORT wcClientGetStrValue(wcHandle client, wcStateId aStateId, char **aStateVal)
Get a C-style string value for the selected client state.
wcRCode DLLEXPORT wcClientInvalidateState(wcHandle client, wcStateId aStateId)
Reset the selected client state.
wcRCode DLLEXPORT wcClientSetStrValue(wcHandle client, wcStateId aStateId, const char *aStateVal)
Set a C-style string value to the selected client state.
wcRCode DLLEXPORT wcClientSetBoolState(wcHandle client, wcStateId aStateId, wcStateVal aStateVal)
Set the boolean value to the selected client state.
@ wcstDevices
Definition: wcwebcamclient.h:180
@ wcstDeviceName
Definition: wcwebcamclient.h:187
@ wcstSID
Definition: wcwebcamclient.h:188
@ wcstProxy
Definition: wcwebcamclient.h:190
@ wcstHostName
Definition: wcwebcamclient.h:189
@ wcstVerifyTLS
Definition: wcwebcamclient.h:175
wcHandle DLLEXPORT wcClientCreate()
Create client.
wcRCode DLLEXPORT wcClientTasksProceed(wcHandle client)
Call the synchronous client update stage.
wcRCode DLLEXPORT wcClientDisconnect(wcHandle client)
Disconnect client from the server host.
wcRCode DLLEXPORT wcClientProceed(wcHandle client)
Call the asynchronous client update stage.
wcRCode DLLEXPORT wcClientAuth(wcHandle client, const char *aLogin, const char *aPwrd)
Authorize client on the server host.
wcRCode DLLEXPORT wcClientDestroy(wcHandle client)
Destroy client.
wcRCode DLLEXPORT wcClientStart(wcHandle client)
Launch client.
const wcRCode WC_OK
Definition: wcwebcamclient.h:74
const wcRCode WC_FALSE
Definition: wcwebcamclient.h:71
int wcHandle
Client handle.
Definition: wcwebcamclient.h:99
wcCallbackTask wcTask
Pointer to task.
Definition: wcwebcamclient.h:102