Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using the same service on 2 protocols #64

Open
sancelot opened this issue Jan 21, 2021 · 0 comments
Open

using the same service on 2 protocols #64

sancelot opened this issue Jan 21, 2021 · 0 comments

Comments

@sancelot
Copy link

sancelot commented Jan 21, 2021

For technical reasons, I would like to use the same service with two different procols
one with tcp (for python2 clients, where I don't have any json rpc websocket available)
and one with websocket , for web application .
I used different port for the tcp and websocket service

unfortunately, this does not seem to work, I can not work with the websocket service, but the tcp service is ok

I know I have to setup some mutexes in service, if I want to share the same fonctions between the two protocols.

here is my server code :

#include "ControlerServiceApi.h"
#include <jcon/json_rpc_tcp_server.h>
#include <jcon/json_rpc_websocket_server.h>
#include "jcon/json_rpc_file_logger.h"
#include <QCoreApplication>
#include <QUrl>

#include <ctime>
#include <iostream>
#include <memory>
#include <unistd.h>

ControlerService *service;

enum class SocketType {tcp, websocket};

jcon::JsonRpcServer* startServer(QObject* parent,
                                 SocketType socket_type = SocketType::tcp)
{
    int port;
    jcon::JsonRpcServer* rpc_server;
    if (socket_type == SocketType::tcp) {
        qDebug() << "Creating TCP server";
        auto logger = std::make_shared<jcon::JsonRpcFileLogger>("e:\\rpc_log.txt");
        rpc_server = new jcon::JsonRpcTcpServer(parent, logger);
        port = 6004;
    } else {
        qDebug() << "Creating WebSocket server";
        rpc_server = new jcon::JsonRpcWebSocketServer(parent);
        port = 6003;
    }
    rpc_server->registerServices({ service });
    rpc_server->listen(port);
    return rpc_server;
}



int main(int argc, char* argv[])
{

    QCoreApplication app(argc, argv);

    service = new ControlerService();

    auto server1 = startServer(nullptr, SocketType::websocket);
    // pour compatibilité python2, obligé de garder l acces en mode socket tcp
    auto server2 = startServer(nullptr, SocketType::tcp);

#ifdef _WIN32
	{
		std::string path;
        path.assign(tmpdir());
		path += "\\controler_service_ok";
		touch(path.c_str());
	}
#else
	int ret  __attribute__((unused))=system("touch /tmp/controler_service_ok");
#endif
    app.exec(); // <- here 
    delete server1;
    delete server2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant