2020-09-15 21:16:35 +03:00
# QuasarApp Heart
2020-09-26 20:11:52 +03:00
# 
2020-09-26 19:49:38 +03:00
QuasarApp Heart - it is base backend for C++/Qt projects. This library support work with databases and work with lite network requests.
2020-09-15 22:07:49 +03:00
## Futures
This library consists of two levels (AbstractNode level and DataBaseNode level).
2020-09-15 21:16:35 +03:00
- [X] Support ssl sockets
- [X] Support initialize database
- [X] Support work in database
2020-09-16 23:40:42 +03:00
- [ ] Support decentralized network mode
2020-09-15 21:16:35 +03:00
2020-09-16 23:40:42 +03:00
### AbstractNode level (0)
2020-09-15 21:16:35 +03:00
#### Description
2020-09-26 19:49:38 +03:00
The AbstractNode level implement only base functions of create a new work threads and parsing packages.
For more information see QuasarApp Heart documentation, QH namespace.
2020-09-15 22:13:32 +03:00
2020-09-16 23:40:42 +03:00
### DataBaseNode level (1)
2020-09-15 22:13:32 +03:00
#### Description
The DataBaseNode level implement methods and packages for work with databases. This level using Qt classes for wrking with database, so for more information about suport databases see [Qt Documentation ](https://doc.qt.io/qt-5/sql-driver.html ).
2020-09-16 23:40:42 +03:00
### NetworkNode level (2)
2020-09-15 22:13:32 +03:00
#### Description
This level is still in develop.
## Build and Include
### For cmake projects
* cd yourRepo
* git submodule add https://github.com/QuasarApp/Heart.git # add the repository of Heart into your repo like submodule
* git submodule update --init --recursive
* Include in your CMakeLists.txt file the main CMakeLists.txt file of Heart library
``` cmake
add_subdirectory(Heart)
```
* select requiriment build level for you project
```
set(HEART_BUILD_LVL 2)
```
where 1 - is code of build level
1 - AbstractNode
2 - DataBaseNode
3 - NetworkNode
* link the Heart library to your target
```cmake
target_link_libraries(yourLib PUBLIC Heart)
```
* rebuild yuor project
## Usage
Create a own package class and override some basic methods.
2020-09-15 21:16:35 +03:00
```cpp
2020-09-15 22:07:49 +03:00
class MyPackage: public QH::AbstractData
{
public:
MyPackage();
// constructor for create object from package
MyPackage(const Package& from): QH::AbstractData(from) {
};
// override this method for validation your package class
bool isValid() const {
return AbstractData::isValid();
}; /
// override this method for copy object data from other to this object
bool copyFrom(const AbstractData *other) {
if (!AbstractData::copyFrom(other))
return false;
2020-09-15 22:25:56 +03:00
auto otherObject = dynamic_cast< const MyPackage * > (other);
2020-09-15 22:07:49 +03:00
if (!otherObject)
return false;
this->_data = otherObject->_data;
return true;
};
// your data for for server of client
std::string _data = "";
protected:
// StreamBase interface override this methods for serialization your package
QDataStream & fromStream(QDataStream & stream) {
AbstractData::fromStream(stream);
stream >> _data;
return stream;
}
QDataStream & toStream(QDataStream & stream) const {
AbstractData::toStream(stream);
stream < < _data ;
return stream;
}
};
2020-09-15 22:13:32 +03:00
```
2020-09-15 22:07:49 +03:00
2020-09-15 22:13:32 +03:00
Create a server class and override parsePackage method for work with packages.
```cpp
2020-09-15 22:07:49 +03:00
class TestingServer: public QH::AbstractNode {
protected:
// override this method for processed received data.
2020-09-15 22:56:58 +03:00
ParserResult parsePackage(const Package & pkg,
2020-09-15 22:07:49 +03:00
const AbstractNodeInfo *sender) {
auto parentResult = AbstractNode::parsePackage(pkg, sender);
if (parentResult != ParserResult::NotProcessed) {
return parentResult;
}
if (H_16< MyPackage > () == pkg.hdr.command) {
MyPackage obj(pkg);
BaseId requesterId = getSender(sender, &obj);
if (!obj.isValid()) {
badRequest(sender->networkAddress(), pkg.hdr);
return ParserResult::Error;
}
obj._data = "responce for client "
SendData(& obj, sender->networkAddress(), &pkg.hdr);
return ParserResult::Processed;
}
return ParserResult::NotProcessed;
}
};
2020-09-15 22:13:32 +03:00
```
2020-09-15 22:07:49 +03:00
2020-09-15 22:13:32 +03:00
Create a client class and override parsePackage method for work with packages.
```cpp
2020-09-15 22:07:49 +03:00
class TestingClient: public QH::AbstractNode {
protected:
2020-09-15 22:13:32 +03:00
// parsing incoming packages
2020-09-15 22:56:58 +03:00
ParserResult parsePackage(const Package & pkg,
2020-09-15 22:07:49 +03:00
const AbstractNodeInfo *sender) {
auto parentResult = AbstractNode::parsePackage(pkg, sender);
if (parentResult != ParserResult::NotProcessed) {
return parentResult;
}
if (H_16< MyPackage > () == pkg.hdr.command) {
MyPackage obj(pkg);
2020-09-15 22:13:32 +03:00
// print responce of server
2020-09-15 22:07:49 +03:00
std::cout < < obj._data ;
...
return ParserResult::Processed;
}
2020-09-15 22:13:32 +03:00
// Do not forget return status of parsing packages
2020-09-15 22:07:49 +03:00
return ParserResult::NotProcessed;
}
2020-09-15 22:13:32 +03:00
// sending request to server
2020-09-15 22:07:49 +03:00
bool sendMyPackage() {
Ping cmd;
return sendData(& cmd, address);
}
};
2020-09-15 21:16:35 +03:00
```
2020-10-22 09:54:43 +03:00
For more information, see our QuasarApp Heart [Code ](https://quasarapp.ddns.net/docs/heart/html/index.html ) Documentation.