disaggregate templates
This commit is contained in:
@@ -1,38 +1,20 @@
|
||||
#include "{{ cls.name }}.proxy.h"
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sstream>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include "rpc/RpcClient.h"
|
||||
|
||||
{{ cls.name }}Proxy::{{ cls.name }}Proxy(const char* pipeIn, const char* pipeOut) {
|
||||
fdIn = open(pipeIn, O_WRONLY);
|
||||
if (fdIn < 0) {
|
||||
perror("open pipeIn");
|
||||
}
|
||||
fdOut = open(pipeOut, O_RDONLY);
|
||||
if (fdOut < 0) {
|
||||
perror("open pipeOut");
|
||||
}
|
||||
}
|
||||
class {{ cls.name }}Proxy::Impl {
|
||||
public:
|
||||
explicit Impl(RpcChannel& ch)
|
||||
: client(ch) {}
|
||||
|
||||
RpcClient client;
|
||||
};
|
||||
|
||||
{{ cls.name }}Proxy::{{ cls.name }}Proxy(RpcChannel& ch)
|
||||
: impl(new Impl(ch)) {}
|
||||
|
||||
{% for m in cls.methods %}
|
||||
{{ m.return_type }} {{ cls.name }}Proxy::{{ m.name }}({% for a in m.args %}{{ a.type }} {{ a.name }}{% if not loop.last %}, {% endif %}{% endfor %}) {
|
||||
std::ostringstream out;
|
||||
out << "{{ m.name }}";
|
||||
{% for a in m.args %}
|
||||
out << " " << {{ a.name }};
|
||||
{% endfor %}
|
||||
out << "\n";
|
||||
std::string s = out.str();
|
||||
write(fdIn, s.c_str(), (ssize_t)s.size());
|
||||
|
||||
char buf[256];
|
||||
ssize_t n = read(fdOut, buf, sizeof(buf)-1);
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
}
|
||||
buf[n] = '\0';
|
||||
return std::atoi(buf);
|
||||
return impl->client.call<{{ m.return_type }}>("{{ cls.name }}.{{ m.name }}"{% for a in m.args %}, {{ a.name }}{% endfor %});
|
||||
}
|
||||
{% endfor %}
|
||||
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
|
||||
#include "rpc/RpcChannel.h"
|
||||
|
||||
class {{ cls.name }}Proxy {
|
||||
public:
|
||||
{{ cls.name }}Proxy(const char* pipeIn, const char* pipeOut);
|
||||
explicit {{ cls.name }}Proxy(RpcChannel& ch);
|
||||
{% for m in cls.methods %}
|
||||
{{ m.return_type }} {{ m.name }}({% for a in m.args %}{{ a.type }} {{ a.name }}{% if not loop.last %}, {% endif %}{% endfor %});
|
||||
{% endfor %}
|
||||
|
||||
private:
|
||||
int fdIn;
|
||||
int fdOut;
|
||||
class Impl;
|
||||
Impl* impl;
|
||||
};
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
#include "{{ cls.name }}.skeleton.h"
|
||||
#include <sstream>
|
||||
|
||||
{{ cls.name }}Skeleton::{{ cls.name }}Skeleton({{ cls.name }}& o) : obj(o) {}
|
||||
|
||||
std::string {{ cls.name }}Skeleton::handleRequest(const std::string& req) {
|
||||
std::istringstream in(req);
|
||||
std::string method;
|
||||
in >> method;
|
||||
{% for m in cls.methods %}
|
||||
if (method == "{{ m.name }}") {
|
||||
{% for a in m.args %}int {{ a.name }}; in >> {{ a.name }};
|
||||
{% endfor %}
|
||||
int res = obj.{{ m.name }}({% for a in m.args %}{{ a.name }}{% if not loop.last %}, {% endif %}{% endfor %});
|
||||
return std::to_string(res);
|
||||
}
|
||||
{% endfor %}
|
||||
return std::string("ERR");
|
||||
{{ cls.name }}Skeleton::{{ cls.name }}Skeleton({{ cls.name }}& obj)
|
||||
{
|
||||
{% for m in cls.methods %}
|
||||
invoker.registerMethod(&obj,
|
||||
"{{ cls.name }}.{{ m.name }}",
|
||||
&{{ cls.name }}::{{ m.name }});
|
||||
{% endfor %}
|
||||
}
|
||||
|
||||
std::string {{ cls.name }}Skeleton::dispatch(const std::string& req) {
|
||||
return invoker.dispatch(req);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "{{ cls.name }}.h"
|
||||
#include "rpc/RpcInvoker.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
class {{ cls.name }}Skeleton {
|
||||
public:
|
||||
{{ cls.name }}Skeleton({{ cls.name }}& obj);
|
||||
std::string handleRequest(const std::string& req);
|
||||
explicit {{ cls.name }}Skeleton({{ cls.name }}& obj);
|
||||
|
||||
std::string dispatch(const std::string& req);
|
||||
|
||||
private:
|
||||
{{ cls.name }}& obj;
|
||||
RpcInvoker invoker;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user