Skip to content

Commit

Permalink
Added syntax highlight to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
yisonPylkita committed Sep 12, 2017
1 parent 4bff1a0 commit cea4265
Showing 1 changed file with 57 additions and 52 deletions.
109 changes: 57 additions & 52 deletions cpp/README.md
Expand Up @@ -15,11 +15,12 @@ Following programs are requred to build:

Configure and install in the usual way:

$ ./bootstrap # if needed
$ ./configure
$ make
$ sudo make install

```sh
$ ./bootstrap # if needed
$ ./configure
$ make
$ sudo make install
```

## Usage

Expand All @@ -36,59 +37,63 @@ Configure and install in the usual way:

### Simple client

#include <jubatus/msgpack/rpc/client.h>
#include <iostream>

int main(void)
{
msgpack::rpc::client c("127.0.0.1", 9090);
int result = c.call("add", 1, 2).get<int>();
std::cout << result << std::endl;
}
```c++
#include <jubatus/msgpack/rpc/client.h>
#include <iostream>

int main(void)
{
msgpack::rpc::client c("127.0.0.1", 9090);
int result = c.call("add", 1, 2).get<int>();
std::cout << result << std::endl;
}
```
### Simple server
#include <jubatus/msgpack/rpc/server.h>

class myserver : public msgpack::rpc::server::base {
public:
void add(msgpack::rpc::request req, int a1, int a2)
{
req.result(a1 + a2);
}

public:
void dispatch(msgpack::rpc::request req)
try {
std::string method;
req.method().convert(&method);

if(method == "add") {
msgpack::type::tuple<int, int> params;
req.params().convert(&params);
add(req, params.get<0>(), params.get<1>());

} else {
req.error(msgpack::rpc::NO_METHOD_ERROR);
}

} catch (msgpack::type_error& e) {
req.error(msgpack::rpc::ARGUMENT_ERROR);
return;

} catch (std::exception& e) {
req.error(std::string(e.what()));
return;
}
};

int main(void)
```c++
#include <jubatus/msgpack/rpc/server.h>
class myserver : public msgpack::rpc::server::base {
public:
void add(msgpack::rpc::request req, int a1, int a2)
{
myserver svr;
svr.instance.listen("0.0.0.0", 9090);
svr.instance.run(4); // run 4 threads
req.result(a1 + a2);
}
public:
void dispatch(msgpack::rpc::request req)
try {
std::string method;
req.method().convert(&method);
if(method == "add") {
msgpack::type::tuple<int, int> params;
req.params().convert(&params);
add(req, params.get<0>(), params.get<1>());
} else {
req.error(msgpack::rpc::NO_METHOD_ERROR);
}
} catch (msgpack::type_error& e) {
req.error(msgpack::rpc::ARGUMENT_ERROR);
return;
} catch (std::exception& e) {
req.error(std::string(e.what()));
return;
}
};
int main(void)
{
myserver svr;
svr.instance.listen("0.0.0.0", 9090);
svr.instance.run(4); // run 4 threads
}
```


IDL parser and code generator is under development. It will resolve the problem that users have to implement verbose dispatch() function.
Expand Down

0 comments on commit cea4265

Please sign in to comment.