[DEV] start work with the websocket interface

This commit is contained in:
Edouard DUPIN 2016-06-15 23:00:54 +02:00
parent 331c5f941b
commit ef853b106e
7 changed files with 83 additions and 55 deletions

View File

@ -408,14 +408,7 @@ bool jus::AbstractFunction::checkCompatibility(const ParamType& _type, const std
|| _params == "vector:empty";
}
if (createType<jus::File>() == _type) {
/*
if (_params.isObject()) {
if (_params.toObject()["type"].toString().get() == "file") {
return true;
}
}
*/
return false;
return _params == "file";
}
if (createType<std::string>() == _type) {
return _params == "string";

View File

@ -45,7 +45,6 @@ namespace jus {
return true;
});
}
/*
class SendFile {
private:
jus::FileServer m_data;
@ -70,22 +69,20 @@ namespace jus {
//m_node.fileClose();
}
bool operator() (TcpString* _interface) {
ejson::Object answer;
answer.add("id", ejson::Number(m_transactionId));
answer.add("client-id", ejson::Number(m_clientId));
answer.add("part", ejson::Number(m_partId));
jus::Buffer answer;
answer.setTransactionId(m_transactionId);
answer.setClientId(m_clientId);
answer.setPartId(m_partId);
answer.setPartFinish(false);
if (m_partId == 0) {
m_node.fileOpenRead();
ejson::Object file;
file.add("type", ejson::String("file"));
std::string extention = std::string(m_data.getFileName().begin()+m_data.getFileName().size() -3, m_data.getFileName().end());
JUS_WARNING("send file: '" << m_data.getFileName() << "' with extention: '" << extention << "'");
file.add("mine-type", ejson::String(jus::getMineType(extention)));
m_size = m_node.fileSize();
file.add("size", ejson::Number(m_size));
answer.add("return", file);
JUS_INFO("Answer: " << answer.generateHumanString());
_interface->write(answer.generateMachineString());
jus::File tmpFile(jus::getMineType(extention), std::vector<uint8_t>(), m_size);
answer.addAnswer(tmpFile);
// TODO : Manage JSON ...
_interface->writeBinary(answer);
m_partId++;
return false;
}
@ -95,14 +92,15 @@ namespace jus {
}
uint8_t tmpData[1024];
m_node.fileRead(tmpData, 1, tmpSize);
answer.add("data", ejson::String(ejson::base64::encode(tmpData, tmpSize)));
answer.addData(tmpData, tmpSize);
//answer.add("data", ejson::String(ejson::base64::encode(tmpData, tmpSize)));
m_size -= tmpSize;
if (m_size <= 0) {
answer.add("finish", ejson::Boolean(true));
answer.setPartFinish(true);
m_node.fileClose();
}
JUS_INFO("Answer: " << answer.generateHumanString());
_interface->write(answer.generateMachineString());
//JUS_INFO("Answer: " << answer.generateHumanString());
_interface->writeBinary(answer);;
m_partId++;
if (m_size <= 0) {
return true;
@ -132,7 +130,6 @@ namespace jus {
#endif
_interfaceClient->addAsync(SendFile(tmpElem, _transactionId, _clientId));
}
*/
template <class JUS_CLASS_TYPE, class... JUS_TYPES>
void executeClassCall(const ememory::SharedPtr<jus::TcpString>& _interfaceClient,

View File

@ -20,6 +20,8 @@ namespace etk {
return "answer";
case jus::Buffer::typeMessage::event:
return "event";
case jus::Buffer::typeMessage::data:
return "event";
}
return "???";
}
@ -37,6 +39,8 @@ static enum jus::Buffer::typeMessage getTypeType(uint16_t _value) {
return jus::Buffer::typeMessage::answer;
case 4:
return jus::Buffer::typeMessage::event;
case 8:
return jus::Buffer::typeMessage::data;
}
return jus::Buffer::typeMessage::call;
}
@ -111,6 +115,9 @@ std::string jus::Buffer::generateHumanString() {
break;
case jus::Buffer::typeMessage::event:
break;
case jus::Buffer::typeMessage::data:
break;
}
if (getNumberParameter() != 0) {
@ -248,6 +255,16 @@ uint32_t jus::Buffer::internalGetParameterSize(int32_t _id) const {
return out;
}
void jus::Buffer::addData(void* _data, uint32_t _size) {
m_paramOffset.clear();
setType(jus::Buffer::typeMessage::data);
m_data.resize(_size);
memcpy(&m_data[0], _data, _size);
}
uint32_t jus::Buffer::getParameterSize(int32_t _id) const {
return internalGetParameterSize(_id + 1);
}
@ -2160,32 +2177,6 @@ std::vector<std::string> jus::Buffer::internalGetParameter<std::vector<std::stri
return out;
}
void jus::Buffer::addError(const std::string& _value, const std::string& _comment) {
addParameter(_value);
addParameter(_comment);

View File

@ -134,6 +134,7 @@ namespace jus {
call = 0x0001,
answer = 0x0002,
event = 0x0004,
data = 0x0008,
};
enum typeMessage getType() const;
void setType(enum typeMessage _value);
@ -188,6 +189,9 @@ namespace jus {
std::string getError();
std::string getErrorHelp();
public:
//multiple section of data (part ...)
void addData(void* _data, uint32_t _size);
void prepare();
ejson::Object toJson() const;

View File

@ -27,10 +27,14 @@ void jus::File::storeIn(const std::string& _filename) const {
etk::FSNodeWriteAllDataType(_filename, m_data);
}
jus::File::File(const std::string& _mineType, std::vector<uint8_t> _data):
jus::File::File(const std::string& _mineType, std::vector<uint8_t> _data, int32_t _fileSize):
m_mineType(_mineType),
m_data(_data) {
if (_fileSize < 0){
m_fileSize = m_data.size();
} else {
m_fileSize = _fileSize;
}
}
void jus::File::setData(uint64_t _offset, const std::vector<uint8_t>& _data) {

View File

@ -10,11 +10,12 @@ namespace jus {
class File {
private:
std::string m_mineType;
int32_t m_fileSize;
std::vector<uint8_t> m_data;
public:
File();
File(const std::string& _filename);
File(const std::string& _mineType, std::vector<uint8_t> _data);
File(const std::string& _mineType, std::vector<uint8_t> _data, int32_t _fileSize = -1);
void storeIn(const std::string& _filename) const;
const std::string& getMineType() const {
return m_mineType;

View File

@ -163,7 +163,45 @@ void jus::TcpString::read() {
if (len == 0) {
JUS_ERROR("Protocol error occured ==> No datas ...");
} else {
if (type == 'B') { // binary
if (type == 'G') { // Get (This is a websocket first connection
std::string out = "G";
JUS_VERBOSE("Read HTTP first connection of a websocket [START]");
// get all data while we find "User-Agent **********\n" ==> After this is a TCP connection (need to answear)
while (true) {
int32_t len = m_connection.read(&type, 1);
if (len == 0) {
continue;
}
out += char(type);
JUS_INFO(" ** " << out);
if ( out[out.size()-1] == '\n'
&& out[out.size()-2] == '\n') {
break;
}
if (char(type) == '\n') {
if (out.find("User-Agent") != std::string::npos) {
break;
}
}
}
JUS_INFO("Find WebSocket ...");
JUS_INFO("data='" << out << "'");
if ( out[0] != 'G'
&& out[1] != 'E'
&& out[2] != 'T') {
std::string ret = "HTTP/1.0 400 Bad Request\n";
ret += "Content-Type : application/octet-stream\n";
ret += "\n";
m_connection.write(&ret[0], ret.size());
disconnect(true);
} else {
std::string ret = "HTTP/1.0 200 OK\n";
ret += "Content-Type : application/octet-stream\n";
ret += "\n";
m_connection.write(&ret[0], ret.size());
}
JUS_VERBOSE("Read HTTP first connection of a websocket [STOP]");
} else if (type == 'B') { // binary
// Binary mode ... start with the lenght of the stream
JUS_VERBOSE("Read Binary [START]");
uint32_t size = 0;