/
RPC.cpp
259 lines (206 loc) · 5.35 KB
/
RPC.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/** @file RPC.cpp
*
* Simple RPC proxy and server object.
*
* A packet is transmitted between the client proxy and server object.
* This packet has a header and message body, where the body contains a
* disk block to be read or written to disk at the server side:
*
* _____ _____ ______________________
* | cmd | num | block |
* | 4 | 4 | NDISK_BLOCK_SIZE |
* |_____|_____|______________________|
*
* A command is a 4-byte string issued by the client to the server:
* "NUMB" query number of disk blocks (server stores #blocks in block
* data on return)
* "FRMT" request a disk format with 'num' blocks
* "WRTE" request a disk write of the block data at block 'num'
* "READ" request a disk read of the block data at block 'num'
*
* @author Copyright Robert van Engelen, April 2006
**/
#include "RPC.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <iostream>
RPC::RPC() : sock(-1)
{ }
RPC::~RPC()
{
disconnect();
}
int RPC::connect(const char *host, int port)
{
// Disconnect if necessary to reconnect
disconnect();
// New TCP/IP socket
sock = (int)socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
return DISK_ERROR;
// Resolve host
struct hostent *hostent = gethostbyname(host);
if (hostent == NULL)
return DISK_ERROR;
// Set port
struct sockaddr_in peer;
memset(&peer, 0, sizeof(peer));
peer.sin_family = AF_INET;
memcpy(&peer.sin_addr, hostent->h_addr, hostent->h_length);
peer.sin_port = htons((short)port);
// Connect
if (::connect(sock, (const struct sockaddr*)&peer, sizeof(peer)) < 0)
return DISK_ERROR;
message2("Connected to %s:%d\n", host, port);
return DISK_OK;
}
void RPC::disconnect()
{
if (sock >= 0)
{
shutdown(sock, 2); // Send EOF
close(sock);
sock = -1;
}
}
int RPC::call(const char command[5], int block_num, char *buf) const
{
if (sock < 0)
return DISK_ERROR;
char packet[NDISK_BLOCK_SIZE + 8];
// Add the command to the packet header
memcpy(packet, command, 4);
// Add the block_num parameter to the packet header
ntos(packet + 4, block_num);
// Add the block to the packet
if (buf)
memcpy(packet + 8, buf, NDISK_BLOCK_SIZE);
size_t len = NDISK_BLOCK_SIZE + 8;
char *msg = packet;
// Write the packet (with 8-byte header)
do
{
ssize_t nwritten = send(sock, msg, len, 0);
if (nwritten < 0)
return DISK_ERROR;
len -= nwritten;
msg += nwritten;
}
while (len > 0);
len = NDISK_BLOCK_SIZE + 8;
msg = packet;
// Read the packet (with 8-byte header)
do
{
ssize_t nread = recv(sock, msg, len, 0);
if (nread <= 0)
return DISK_ERROR;
len -= nread;
msg += nread;
}
while (len > 0);
if (buf){
//buf = (char *) malloc(NDISK_BLOCK_SIZE);
//buf = (char*) (packet+8);
if(strcmp(command, "WRTE")!=0){
//std::cout<<"buf="<<buf<<", command is "<<command<<std::endl;
memcpy(buf, packet + 8, NDISK_BLOCK_SIZE);
}
//free(buf);
}
return DISK_OK;
}
int RPC::serve(int port, Disk& disk)
{
int master = (int)socket(AF_INET, SOCK_STREAM, 0);
int set = 1;
setsockopt(master, SOL_SOCKET, SO_REUSEADDR, (char*)&set, sizeof(int));
struct sockaddr_in peer;
memset(&peer, 0, sizeof(peer));
peer.sin_family = AF_INET;
peer.sin_addr.s_addr = htonl(INADDR_ANY);
peer.sin_port = htons((short)port);
if (bind(master, (struct sockaddr*)&peer, sizeof(peer)))
return DISK_ERROR;
listen(master, 100);
message("Server started\n");
for (;;)
{
socklen_t n = sizeof(peer);
sock = accept(master, (struct sockaddr*)&peer, &n);
if (sock < 0)
return DISK_ERROR;
message("Connection established\n");
while (dispatch(disk) == DISK_OK)
;
shutdown(sock, 2); // Send EOF
close(sock);
message("Connection closed\n");
}
}
int RPC::dispatch(Disk& disk)
{
char buf[NDISK_BLOCK_SIZE];
char packet[NDISK_BLOCK_SIZE + 8];
size_t len = NDISK_BLOCK_SIZE + 8;
char *msg = packet;
if (disk.block_size() != NDISK_BLOCK_SIZE)
return DISK_ERROR;
// Read the packet (with 8-byte header)
do
{
ssize_t nread = recv(sock, msg, len, 0);
if (nread <= 0)
return DISK_ERROR;
len -= nread;
msg += nread;
}
while (len > 0);
// Get the command from the packet's first four bytes
char command[5];
memcpy(command, packet, 4);
command[4] = '\0';
// Get the block number parameter from the packet
int block_num = ston(packet + 4);
if (!strcmp(command, "NUMB"))
{
// Write num blocks into packet data
ntos(packet + 8, disk.num_blocks());
}
else if (!strcmp(command, "FRMT"))
{
disk.format(block_num);
}
else if (!strcmp(command, "READ"))
{
disk.read(block_num, buf);
memcpy(packet + 8, buf, NDISK_BLOCK_SIZE);
}
else if (!strcmp(command, "WRTE"))
{
memcpy(buf, packet + 8, NDISK_BLOCK_SIZE);
disk.write(block_num, buf);
}
else
{
message1("Unknown RPC command %s\n", command);
}
message2("RPC command %s(%d)\n", command, block_num);
len = NDISK_BLOCK_SIZE + 8;
msg = packet;
// Write the packet (with 8-byte header)
do
{
ssize_t nwritten = send(sock, msg, len, 0);
if (nwritten < 0)
return DISK_ERROR;
len -= nwritten;
msg += nwritten;
}
while (len > 0);
return DISK_OK;
}