fiphobo.cc 4.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "fiphobo.h"

#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <cstdio>
#include <stdlib.h> 
#include <unistd.h> 

namespace fiphoboserver
{

FiPhobo::FiPhobo()
{
    memset(&descriptor, 0, sizeof(descriptor));
    descriptor.xd_attrs = {0};
}

void FiPhobo::setBucketName(std::string bucketName)
{
#ifdef DEBUG
    std::cout << "Phobos_Layer::setBucketName" << std::endl;
#endif
    int rc = pho_attr_set(&descriptor.xd_attrs, "bucket", bucketName.c_str());
    if(rc)
    {
        printf("pho_attr_set returned an error: %d\n", rc);
        return;
    }
}

void FiPhobo::setMetaData(std::vector<std::pair<std::string, std::string>> metaData)
{
#ifdef DEBUG
    std::cout << "Phobos_Layer::setMetaData" << std::endl;
#endif
    std::for_each(metaData.begin(), metaData.end(), 
        [&](std::pair<std::string, std::string> pair){
            int rc = pho_attr_set(&descriptor.xd_attrs, pair.first.c_str(), 
                pair.second.c_str());
            if(rc)
            {
                printf("pho_attr_set returned an error: %d\n", rc);
                return;
            }
        });
}

void FiPhobo::startPutOperation(unsigned int size, std::string objectID)
{
#ifdef DEBUG
    std::cout << "Phobos_Layer::startPutOperation" << std::endl;
#endif
    startFIFO();

    char *unconstedObjectID = new char[objectID.length() + 1];
    strcpy(unconstedObjectID, objectID.c_str());
    
    descriptor.xd_op = PHO_XFER_OP_PUT;
    descriptor.xd_objid = unconstedObjectID;
    descriptor.xd_fd = fifo_descriptor;
    descriptor.xd_size = size;

#ifdef DEBUG
    std::cout << "Starting async phobos_put_cpp" << std::endl;
#endif
    phobos_result = std::async([&](){
        return phobos_put_cpp(&descriptor, 1, NULL, NULL);
    });
    
    //TODOOOO
    // delete [] unconstedObjectID;
}

void FiPhobo::addDataToFIFO(std::unique_ptr<folly::IOBuf> buffer)
{
#ifdef DEBUG
    std::cout << "Phobos_Layer::addDataToFIFO" << std::endl;
#endif
    if(ioRunning)
    {
        int ioRC = io_result.get();
        if(ioRC < 0)
        {
            //TODO error handling
        }
    }
    ioRunning = true;
    
    buffer->coalesce();
    io_result = std::async([&](){
        return write(fifo_descriptor, buffer->data(), buffer->length());
    });
}

void FiPhobo::startGetOperation(std::string& objectID)
{
#ifdef DEBUG
    std::cout << "Phobos_Layer::startGetOperation" << std::endl;
#endif
    startFIFO();

    char* unconstedObjectID = (char*) objectID.c_str();
    
    descriptor.xd_op = PHO_XFER_OP_GET;
    descriptor.xd_objid = unconstedObjectID;
    descriptor.xd_fd = fifo_descriptor;

    phobos_result = std::async([&](){
        return phobos_get_cpp(&descriptor, 1, NULL, NULL);
    });
}

std::unique_ptr<folly::IOBuf> FiPhobo::getDataFromFIFO(size_t size)
{
#ifdef DEBUG
    std::cout << "Phobos_Layer::getDataFromFIFO" << std::endl;
#endif
    std::unique_ptr<folly::IOBuf> buffer = folly::IOBuf::create(size);
    
    
    if(ioRunning)
    {
        int ioRC = io_result.get();
        if(ioRC < 0)
        {
            //TODO error handling
        }
    }
    ioRunning = true;
    
    io_result = std::async([&](){
        buffer->unshare();
        return read(fifo_descriptor, buffer->writableData(), size);
    });
    return std::move(buffer);
}
    
void FiPhobo::finishIO()
{
#ifdef DEBUG
    std::cout << "Phobos_Layer::finishIO" << std::endl;
#endif
    int ioRC = io_result.get();
    if(ioRC < 0)
    {
        //TODO error handling
    }
    
    int phobosRC = phobos_result.get();
    if(phobosRC)
    {
        //TODO error handling!
    }
    ioRunning = false;
}

FiPhobo::~FiPhobo()
{
#ifdef DEBUG
    std::cout << "Phobos_Layer::~Phobos_Layer" << std::endl;
#endif
    // pho_attrs_free(&descriptor.xd_attrs); // TODO: Is that done in desc_destroy??
    pho_xfer_desc_destroy_cpp(&descriptor);  
    if(fifo_descriptor > 0)
    {
        close(fifo_descriptor);            
    }
Ciarán Ó Rourke's avatar
Ciarán Ó Rourke committed
171
    if( access( fifoName.c_str(), F_OK ) != -1 )
172
    {
Ciarán Ó Rourke's avatar
Ciarán Ó Rourke committed
173
        remove(fifoName.c_str());
174
175
176
177
178
179
180
181
182
    }
}
    
void FiPhobo::startFIFO()
{
#ifdef DEBUG
    std::cout << "Phobos_Layer::startFIFO" << std::endl;
#endif
    int rc = mkfifo(fifoName.c_str(), 0777);
183
    if(rc < 0)
184
185
186
187
188
    {
        printf("mkfifo returned an error: %s\n", std::strerror(errno));
        return;
    }
    
Ciarán Ó Rourke's avatar
Ciarán Ó Rourke committed
189
    fifo_descriptor = open(fifoName.c_str(), O_RDWR);
190
191
    if (fifo_descriptor < 0) 
    {
192
        printf("Could not open fifo: %s\n", fifoName);
193
194
195
196
197
198
199
        return;
    }
    
#ifdef DEBUG
    std::cout << "End of Phobos_Layer::startFIFO" << std::endl;
#endif
}
200
} // namespace fiphoboserver