// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
#include <iostream>
#include <string>
#include <memory>
#include <ctime>

#include <grpcpp/grpcpp.h>

#include "test.pb.h"
#include "test.grpc.pb.h"

using namespace std;

using namespace google::protobuf;

using namespace grpc;

void check(Status stat)
{
    if(!stat.ok())
    {
        cout << stat.error_message() << endl;
        exit(0);
    }
}

void test_functional(const char *srv)
{
    shared_ptr<Channel> ch = CreateChannel(srv, InsecureChannelCredentials());
    unique_ptr<Test::Stub> tst(Test::NewStub(ch));
    Status stat;
    ClientContext addctx;
    int a = 123;
    int b = 456;
    AddRequest addreq;
    addreq.set_a(a);
    addreq.set_b(b);
    AddReply addresp;
    stat = tst->Add(&addctx, addreq, &addresp);
    check(stat);
    int c = addresp.c();
    cout << c << endl;
    ClientContext dupctx;
    string s = "ABC";
    DupRequest dupreq;
    dupreq.set_s(s);
    DupReply dupresp;
    stat = tst->Dup(&dupctx, dupreq, &dupresp);
    check(stat);
    string s2 = dupresp.s2();
    cout << s2 << endl;
    ClientContext procctx;
    Data d;
    d.set_iv(123);
    d.set_sv("ABC");
    Data d2;
    stat = tst->Process(&procctx, d, &d2);
    check(stat);
    cout << d2.iv() << " " << d2.sv() << endl;
}

void test_instantiation(const char *srv)
{
    Status stat;
    shared_ptr<Channel> ch1 = CreateChannel(srv, InsecureChannelCredentials());
    unique_ptr<Test::Stub> tst1(Test::NewStub(ch1));
    for(int i = 0; i < 2; i++)
    {
        ClientContext ctx;
        Empty gcreq;
        GetCounterReply gcresp;
        stat = tst1->GetCounter(&ctx, gcreq, &gcresp);
        check(stat);
        int n = gcresp.counter();
        cout << n << endl;
    }
    shared_ptr<Channel> ch2 = CreateChannel(srv, InsecureChannelCredentials());
    unique_ptr<Test::Stub> tst2(Test::NewStub(ch2));
    for(int i = 0; i < 2; i++)
    {
        ClientContext ctx;
        Empty gcreq;
        GetCounterReply gcresp;
        stat = tst2->GetCounter(&ctx, gcreq, &gcresp);
        check(stat);
        int n = gcresp.counter();
        cout << n << endl;
    }
}


const int REP = 100000;

void test_performance(const char *srv)
{
    shared_ptr<Channel> ch = CreateChannel(srv, InsecureChannelCredentials());
    unique_ptr<Test::Stub> tst(Test::NewStub(ch));
    Status stat;
    time_t t1 = time(NULL);
    for(int i = 0; i < REP; i++)
    {
        ClientContext ctx;
        Empty noreq;
        Empty noresp;
        stat = tst->Noop(&ctx, noreq, &noresp);
        check(stat);
    }
    time_t t2 = time(NULL);
    cout << (REP / (t2 - t1)) << " requests per second" << endl;
}

void test_stream(const char *srv)
{
    shared_ptr<Channel> ch = CreateChannel(srv, InsecureChannelCredentials());
    unique_ptr<Test::Stub> tst(Test::NewStub(ch));
    Status stat;
    ClientContext ctx;
    shared_ptr<ClientReaderWriter<Data, Data>> stm(tst->StreamProcess(&ctx));
    for(int i = 0; i < 10; i++)
    {
        Data d;
        d.set_iv(i);
        d.set_sv(string("") + char(65 + i));
        stm->Write(d);
    }
    stm->WritesDone();
    Data d2;
    while(stm->Read(&d2))
    {
        cout << d2.iv() << " " << d2.sv() << endl;
    }
    stat = stm->Finish();
    check(stat);
}

void test_stream_performance(const char *srv)
{
    shared_ptr<Channel> ch = CreateChannel(srv, InsecureChannelCredentials());
    unique_ptr<Test::Stub> tst(Test::NewStub(ch));
    Status stat;
    time_t t1 = time(NULL);
    ClientContext ctx;
    shared_ptr<ClientReaderWriter<Empty, Empty>> stm(tst->StreamNoop(&ctx));
    for(int i = 0; i < REP; i++)
    {
        Empty noreq;
        stm->Write(noreq);
    }
    stm->WritesDone();
    Empty noresp;
    while(stm->Read(&noresp))
    {
        // nothing
    }
    stat = stm->Finish();
    check(stat);
    time_t t2 = time(NULL);
    cout << (REP / (t2 - t1)) << " requests per second" << endl;
}

void test(const char *lbl, const char *srv)
{
    cout << lbl << ":" << endl;
    test_functional(srv);
    test_instantiation(srv);
    test_performance(srv);
    test_stream(srv);
    test_stream_performance(srv);
}

const char *JAVA = "localhost:12345";
const char *DOTNET = "localhost:12346";
const char *DOTNET2 = "localhost:12347";
const char *PYTHON = "localhost:12348";
const char *CPP = "localhost:12349";

int main()
{
    test("Java", JAVA);
    test(".NET", DOTNET);
    test(".NET (MS)", DOTNET2);
    test("Python", PYTHON);
    test("C++", CPP);
    return 0;
}
