3DCoat  3D-COAT 4.9.xx
3DCoat is the one application that has all the tools you need to take your 3D idea from a block of digital clay all the way to a production ready, fully textured organic or hard surface model.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
jcc::LocalServer Class Reference

#include <jcc.h>

Public Member Functions

httplib::Server & server ()
 return the reference to the httplib::Server for any advanced operations. More...
 
int port ()
 returns the port, assigned to the server More...
 
void allowConsoleOutput (bool enable=true)
 allows logging about every network action to the to cout More...
 
void open (const Html &page)
 opens the page in browser More...
 
void openFile (const std::string &filepath)
 opens the page from file in browser More...
 
void get (requestHandler f, const char *pattern=nullptr)
 assign the callback for the get request. The page may interact with the host program via the get requests. More...
 
void post (requestHandler f, const char *pattern=nullptr)
 assign the callback for the post request. The page may interact with the host program via the post requests. More...
 
void put (requestHandler f, const char *pattern=nullptr)
 assign the callback for the put request. More...
 
void exchange (std::function< json::JSON(const json::JSON &)> f)
 exchange objects, you are getting object as input and should return the object as well. The call of exchange initiated on browser's side by sendObject, see the example More...
 
void listenAsync ()
 starts to listen the port. Usually, you don't need to call it manually. Any other network interaction function calls it. And if you will call it, nothing wrong will happen. If it was already called and listening thread created, then nothing happens. More...
 
void signalToStop ()
 stop the server and exit the listen cycle. This function may be called in the response body. Server will be stopped only after the sending the response. More...
 
void stopGracefully ()
 stop and wait till it will be stopped More...
 
bool wait (int ms_amount=-1)
 Wait till the page will be closed or the connection lost. Page treated as closed when there is no ping for 500 ms. More...
 
bool alive (int ms_max=2000)
 returns true if there sas ping from the page recently More...
 
double msSincePing ()
 returns milliseconds since the last ping or < 0 if there was no pings. More...
 
Elementconsole ()
 
Elementel (const std::string &domElementID)
 
Elementeval ()
 

Static Public Member Functions

static void setServerFilesPlacement (const char *path)
 Set the path to the folder that contains /public folder The main.js should be placed into the root of that folder. If you will not specify the path, the folder that contains main.js will be taken as the root folder will be treated as the root folder for the server. More...
 
static std::string path (const std::string &filename)
 appends filename to the server files path, returns the absolute path More...
 
static void read (const std::string &filename, std::string &to)
 read the file to string, path is relative to server More...
 
static void write (const std::string &filename, std::string &from)
 write the string to file, path is relative to server More...
 
static void createPath (const std::string &path)
 create all componentes of the path, path is relative to server More...
 
static bool pathIsRelative (const std::string &path)
 detects if the path is relative to the server More...
 

Detailed Description

This is the main class for interaction between the c++ code and the browser. The typical pattern is:

jcc::LocalSelver ls;
jcc::Html h("test.html");//test.html placed in the same folder as the main.js
ls.exchange([](const json::JSON& incomingData)->json::JSON {
json::JSON response;
// handle the incoming data and send the response.
return response;
});
ls.open(h);
do {
// do something in cycle if need, if nothing need just set some sleep there
} while(ls.alive());
// End all stuff
ls.finishGracefully(); *

Member Function Documentation

bool jcc::LocalServer::alive ( int  ms_max = 2000)
inline

returns true if there sas ping from the page recently

References msSincePing().

void jcc::LocalServer::allowConsoleOutput ( bool  enable = true)
inline

allows logging about every network action to the to cout

Element & jcc::LocalServer::console ( )
inline

Print to the browser console.There is a little delay because changes passed to browser each time when it sends ping.

ls.console() << "Print this into the browser's console. " << "And this on the same line." << ls; // ls passed at the end as the signal that the data chunk ended.

References el().

void jcc::LocalServer::createPath ( const std::string &  path)
inlinestatic

create all componentes of the path, path is relative to server

References path().

Element & jcc::LocalServer::el ( const std::string &  domElementID)
inline

Set the value of the DOM's innerHTML from the server side. There is a little delay because changes passed to browser each time when it sends ping.

ls.el("main") << "This text will appear in the DOM element named <b>'main'</b>." << ls;

Referenced by console(), and eval().

Element & jcc::LocalServer::eval ( )
inline

Evaluate on the browser's side.

ls.eval() << "console.log('This is executed in the browser!') << ls;

References el().

void jcc::LocalServer::exchange ( std::function< json::JSON(const json::JSON &)>  f)
inline

exchange objects, you are getting object as input and should return the object as well. The call of exchange initiated on browser's side by sendObject, see the example

References listenAsync().

void jcc::LocalServer::get ( requestHandler  f,
const char *  pattern = nullptr 
)
inline

assign the callback for the get request. The page may interact with the host program via the get requests.

References listenAsync().

void jcc::LocalServer::listenAsync ( )
inline

starts to listen the port. Usually, you don't need to call it manually. Any other network interaction function calls it. And if you will call it, nothing wrong will happen. If it was already called and listening thread created, then nothing happens.

Referenced by exchange(), get(), open(), post(), put(), and wait().

double jcc::LocalServer::msSincePing ( )
inline

returns milliseconds since the last ping or < 0 if there was no pings.

Referenced by alive(), and wait().

void jcc::LocalServer::open ( const Html &  page)
inline

opens the page in browser

References listenAsync(), and path().

Referenced by openFile().

void jcc::LocalServer::openFile ( const std::string &  filepath)
inline

opens the page from file in browser

References open(), and path().

std::string jcc::LocalServer::path ( const std::string &  filename)
inlinestatic

appends filename to the server files path, returns the absolute path

References pathIsRelative().

Referenced by createPath(), open(), openFile(), and setServerFilesPlacement().

bool jcc::LocalServer::pathIsRelative ( const std::string &  path)
inlinestatic

detects if the path is relative to the server

Referenced by path().

int jcc::LocalServer::port ( )
inline

returns the port, assigned to the server

void jcc::LocalServer::post ( requestHandler  f,
const char *  pattern = nullptr 
)
inline

assign the callback for the post request. The page may interact with the host program via the post requests.

References listenAsync().

void jcc::LocalServer::put ( requestHandler  f,
const char *  pattern = nullptr 
)
inline

assign the callback for the put request.

References listenAsync().

void jcc::LocalServer::read ( const std::string &  filename,
std::string &  to 
)
inlinestatic

read the file to string, path is relative to server

httplib::Server& jcc::LocalServer::server ( )
inline

return the reference to the httplib::Server for any advanced operations.

void jcc::LocalServer::setServerFilesPlacement ( const char *  path)
inlinestatic

Set the path to the folder that contains /public folder The main.js should be placed into the root of that folder. If you will not specify the path, the folder that contains main.js will be taken as the root folder will be treated as the root folder for the server.

References path().

void jcc::LocalServer::signalToStop ( )
inline

stop the server and exit the listen cycle. This function may be called in the response body. Server will be stopped only after the sending the response.

void jcc::LocalServer::stopGracefully ( )
inline

stop and wait till it will be stopped

bool jcc::LocalServer::wait ( int  ms_amount = -1)
inline

Wait till the page will be closed or the connection lost. Page treated as closed when there is no ping for 500 ms.

Parameters
ms_amoutthe milliseconds to wait, -1 to wait infinitely.
Returns
true if the connection lost.

References listenAsync(), and msSincePing().

void jcc::LocalServer::write ( const std::string &  filename,
std::string &  from 
)
inlinestatic

write the string to file, path is relative to server


The documentation for this class was generated from the following file: