
#include "service.h"
#include <unistd.h>

CustomServer::CustomServer() {
}

CustomServer::~CustomServer() {
}

::grpc::Status CustomServer::OpenStream(::grpc::ServerContext* context, const ::RequestCmd* request, ::grpc::ServerWriter< ::clouds>* writer) {
////////////////////////////////
    streamStatu = true;
    printf("%s\n", __FUNCTION__);
    while (streamStatu) {
        clouds clamp_clouds;
        pcl::PointCloud<pcl::PointXYZ>::Ptr pcl_cloud(new pcl::PointCloud<pcl::PointXYZ>);
        TransitData::get_instance_pointer()->getCloud(0, pcl_cloud);
        // printf("get point num: %ld\n", pcl_cloud->size());
        for(auto &pt: pcl_cloud->points) {
            PointXY point;
            point.set_x(pt.x);
            point.set_y(pt.y);
            clamp_clouds.add_clamp_1()->CopyFrom(point);
        }
        TransitData::get_instance_pointer()->getCloud(1, pcl_cloud);
        for(auto &pt: pcl_cloud->points) {
            PointXY point;
            point.set_x(pt.x);
            point.set_y(pt.y);
            clamp_clouds.add_clamp_2()->CopyFrom(point);
        }
        TransitData::get_instance_pointer()->getCloud(2, pcl_cloud);
        for(auto &pt: pcl_cloud->points) {
            PointXY point;
            point.set_x(pt.x);
            point.set_y(pt.y);
            clamp_clouds.add_clamp_3()->CopyFrom(point);
        }
        TransitData::get_instance_pointer()->getCloud(3, pcl_cloud);
        for(auto &pt: pcl_cloud->points) {
            PointXY point;
            point.set_x(pt.x);
            point.set_y(pt.y);
            clamp_clouds.add_clamp_4()->CopyFrom(point);
        }
        writer->Write(clamp_clouds);
        if (context->IsCancelled()) {
            printf("Data Stream canceled\n");
            streamStatu = false;
            return ::grpc::Status::OK;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
////////////////////////////////
    return ::grpc::Status::OK;

}

::grpc::Status CustomServer::CloseStream(::grpc::ServerContext* context, const ::RequestCmd* request, ::grpc::ServerWriter< ::clouds>* writer) {
////////////////////////////////
    streamStatu = false;
    printf("%s\n", __FUNCTION__);
////////////////////////////////
    return ::grpc::Status::OK;

}

StreamRpcServer::StreamRpcServer() {
    thread_ = nullptr;
}

StreamRpcServer::~StreamRpcServer() {

    if (thread_ != nullptr) {
        server_->Shutdown();
        if (thread_->joinable()) {
            thread_->join();
            delete thread_;
        }
    }
}

void StreamRpcServer::Listenning(std::string ip, int port) {
    char ipport[64] = {0};
    printf("listenning grpc %s ", ip.c_str());
    sprintf(ipport, "%s:%d", ip.c_str(), port);
    builder_.AddListeningPort(ipport, grpc::InsecureServerCredentials());
    builder_.RegisterService(&service_);

    server_ = builder_.BuildAndStart();
    thread_ = new std::thread(&StreamRpcServer::WaitThread, this);
}

void StreamRpcServer::WaitThread(void *p) {
    StreamRpcServer *server = (StreamRpcServer *) p;
    printf(" listenning ........\n");
    server->server_->Wait();
    printf(" server closesd.....\n");
}

