-
Notifications
You must be signed in to change notification settings - Fork 1
/
minigfs_primary.cpp
191 lines (155 loc) · 6.75 KB
/
minigfs_primary.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// miniNFS
// for Json::value
#include <json/json.h>
#include <json/reader.h>
#include <json/writer.h>
#include <json/value.h>
#include <string>
// for JsonRPCCPP
#include <iostream>
#include "minigfs_server.h"
#include <jsonrpccpp/server/connectors/httpserver.h>
#include <stdio.h>
// ecs251
#include "Core.h"
#include "Directory.h"
#include "Replica.h"
#include "Shadow_Directory.h"
#include <time.h>
using namespace jsonrpc;
using namespace std;
class Myminigfs_Server : public minigfs_Server
{
public:
Myminigfs_Server(AbstractServerConnector &connector, serverVersion_t type);
virtual Json::Value ObtainChunkURL(const std::string& action, const std::string& arguments, const std::string& chunkindex, const std::string& class_id, const std::string& fhandle, const std::string& filename, const std::string& host_url, const std::string& object_id, const std::string& owner_vsID);
virtual Json::Value PushChunk2Replica(const std::string& action, const std::string& arguments, const std::string& chunk, const std::string& chunkindex, const std::string& class_id, const std::string& fhandle, const std::string& filename, const std::string& host_url, const std::string& object_id, const std::string& owner_vsID);
virtual Json::Value CommitAbort(const std::string& action, const std::string& arguments, const std::string& chunkindex, const std::string& class_id, const std::string& commitorabort, const std::string& fhandle, const std::string& filename, const std::string& host_url, const std::string& object_id, const std::string& owner_vsID);
virtual Json::Value LookUp(const std::string& action, const std::string& arguments, const std::string& class_id, const std::string& fhandle, const std::string& filename, const std::string& host_url, const std::string& object_id, const std::string& owner_vsID);
virtual Json::Value Create(const std::string& action, const std::string& arguments, const std::string& class_id, const std::string& created_class_id, const std::string& fhandle, const std::string& filename, const std::string& host_url, const std::string& object_id, const std::string& owner_vsID, const std::string& sattr);
virtual Json::Value dumpJ(const std::string& action, const std::string& arguments, const std::string& class_id, const std::string& host_url, const std::string& object_id, const std::string& owner_vsID);
};
Myminigfs_Server::Myminigfs_Server(AbstractServerConnector &connector, serverVersion_t type)
: minigfs_Server(connector, type)
{
std::cout << "Myminigfs_Server Object created" << std::endl;
}
Directory *mounted;
Replica *replica;
// member function
Json::Value
Myminigfs_Server::ObtainChunkURL
(const std::string& action, const std::string& arguments, const std::string& chunkindex, const std::string& class_id, const std::string& fhandle, const std::string& filename, const std::string& host_url, const std::string& object_id, const std::string& owner_vsID)
{
Json::Value result;
//
return result;
}
Json::Value
Myminigfs_Server::PushChunk2Replica
(const std::string& action, const std::string& arguments, const std::string& chunk, const std::string& chunkindex, const std::string& class_id, const std::string& fhandle, const std::string& filename, const std::string& host_url, const std::string& object_id, const std::string& owner_vsID)
{
Json::Value result;
std::cout<<"Prime_Replica PushChunk2Replica!"<<std::endl;
if (fhandle != "00000002") // inode 2 is the root PS: 我不确定这个if条件是啥。。。
{
result["status"] = "GFSERR_STALE";
result["vote"] = "abort";
}
else
{
result = replica->PushChunk2Replica(filename, fhandle, chunkindex, chunk);
}
return result;
}
Json::Value
Myminigfs_Server::CommitAbort
(const std::string& action, const std::string& arguments, const std::string& chunkindex, const std::string& class_id, const std::string& commitorabort, const std::string& fhandle, const std::string& filename, const std::string& host_url, const std::string& object_id, const std::string& owner_vsID)
{
Json::Value result;
std::cout<<"Prime_Replica CommitAbort!"<<std::endl;
if (fhandle != "00000002")
{
result["status"] = "GFSERR_STALE";
}
else
{
result = replica->CommitAbort(filename, fhandle, chunkindex, commitorabort);
}
return result;
}
Json::Value
Myminigfs_Server::LookUp(const std::string& action, const std::string& arguments, const std::string& class_id, const std::string& fhandle, const std::string& filename, const std::string& host_url, const std::string& object_id, const std::string& owner_vsID)
{
Json::Value result;
std::cout << action << " " << arguments << " " << owner_vsID << std::endl;
std::cout << "SFelixWu receiving LookUp" << std::endl;
if (fhandle != "00000002") // inode 2 is the root
{
result["status"] = "NFSERR_STALE";
}
else
{
result = mounted->LookUp(fhandle, filename);
}
return result;
}
Json::Value
Myminigfs_Server::Create(const std::string& action, const std::string& arguments, const std::string& class_id, const std::string& created_class_id, const std::string& fhandle, const std::string& filename, const std::string& host_url, const std::string& object_id, const std::string& owner_vsID, const std::string& sattr)
{
Json::Value result;
std::cout << action << " " << arguments << " " << owner_vsID << std::endl;
std::cout << "SFelixWu receiving Create" << std::endl;
if (fhandle != "00000002")
{
result["status"] = "NFSERR_STALE";
}
else
{
result = mounted->Create(fhandle, filename, sattr);
}
return result;
}
Json::Value
Myminigfs_Server::dumpJ(const std::string& action, const std::string& arguments, const std::string& class_id, const std::string& host_url, const std::string& object_id, const std::string& owner_vsID)
{
Json::Value result;
std::cout << action << " " << arguments << " " << owner_vsID << std::endl;
std::cout << "Primary receiving dumpJ" << std::endl;
if (object_id != "00000001")
{
result["status"] = "NFSERR_STALE onject_id is not 2";
}
else
{
Json::Value *myv_ptr = replica->dumpJ();
if (myv_ptr != NULL)
{
result = *myv_ptr;
result["status"] = "NFS_OK";
}
else
{
result["status"] = "NFSERR_STALE";
}
}
return result;
}
int
main()
{
Directory NFS_root
{ "http://169.237.6.102", "1234567890", "Directory", "00000000", "root", "00000002" };
mounted = (&NFS_root);
Replica GFS_primaryReplica
{ "http://169.237.6.102", "1234567890", "Replica", "00000001", "This is Prime_Chunk1", "This is Prime_Chunk2"};
replica = (&GFS_primaryReplica);
HttpServer httpserver(8300);
Myminigfs_Server s(httpserver,
JSONRPC_SERVER_V1V2); // hybrid server (json-rpc 1.0 & 2.0)
s.StartListening();
std::cout << "Hit enter to stop the server" << endl;
getchar();
s.StopListening();
return 0;
}