Distributed Cache
requests.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <arpa/inet.h>
4 #include <fcntl.h>
5 #include <netinet/in.h>
6 #include <sys/socket.h>
7 #include <sys/time.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10 
11 #include <string>
12 
13 struct GetRequest {
14  char container[10];
15  int key;
16 };
17 
18 struct CreateRequest {
19  char container[10];
20 };
21 
22 struct EraseRequest {
23  char container[10];
24  int key;
25 };
26 
27 struct GetResponse {
28  int value;
29 };
30 
31 struct PutRequest {
32  char container[10];
33  int key;
34  int value;
35 };
36 
37 enum class RequestType {
38  GET,
39  PUT,
40  ERASE,
41  RESET,
43  GOSSIP,
44  GET_TARGET,
46 };
47 
48 struct Address {
49  std::string ip;
50  int port;
51  Address(const std::string ip = "invalid", const int port = -1)
52  : ip(ip), port(port){};
53  bool operator==(const Address& other) {
54  return ip == other.ip && port == other.port;
55  }
56  bool operator<(const Address& other) const {
57  if (ip != other.ip) return ip < other.ip;
58  return port < other.port;
59  }
60  std::string toString() const { return ip + ":" + std::to_string(port); }
61  operator std::string() const { return toString(); }
62  static int getConnection(Address addr) {
63  int IP = inet_addr(addr.ip.c_str()); // INADDR_ANY;
64  struct sockaddr_in server;
65  int sd = socket(AF_INET, SOCK_STREAM, 0);
66  server.sin_family = AF_INET;
67  server.sin_addr.s_addr = IP;
68  server.sin_port = htons(addr.port);
69 
70  if (connect(sd, (struct sockaddr*)&server, sizeof(server)) < 0) {
71  perror("connect failed: ");
72  // throw std::runtime_error("Connection Failed to IP address - " +
73  // std::string(addr));
74  return -1;
75  }
76 
77  return sd;
78  }
79 };
80 
81 /* A simple routine calling UNIX write() in a loop */
82 static ssize_t loop_write(int fd, const void* data, size_t size) {
83  ssize_t ret = 0;
84  int req = size;
85  while (size > 0) {
86  ssize_t r;
87  if ((r = write(fd, data, size)) < 0) {
88  perror("write");
89  break;
90  }
91  if (r == 0) break;
92  ret += r;
93  data = (const char*)data + r;
94  size -= (size_t)r;
95  }
96 
97  if (size)
98  throw std::runtime_error(
99  "Write Could Not Complete, bytes written: " + std::to_string(ret) +
100  " bytes requested " + std::to_string(req));
101  return ret;
102 }
103 
104 /* A simple routine calling UNIX read() in a loop */
105 static ssize_t loop_read(int fd, void* data, size_t size) {
106  int req = size;
107  ssize_t ret = 0;
108  while (size > 0) {
109  ssize_t r;
110  if ((r = read(fd, data, size)) < 0) {
111  perror("read");
112  break;
113  }
114  if (r == 0) break;
115  ret += r;
116  data = (char*)data + r;
117  size -= (size_t)r;
118  }
119 
120  if (size)
121  throw std::runtime_error(
122  "Read Could Not Complete, bytes read: " + std::to_string(ret) +
123  " bytes requested " + std::to_string(req));
124  return ret;
125 }
126 
127 /*
128 
129 Hierarchy Structure. Each PayloadHeader is for a single artifact
130 
131  GossipPayload
132  PayloadHeader
133  Buffer
134  PayloadHeader
135  Buffer
136 */
137 
140 };
141 
143  char type[20];
144  int size; // (not including header size)
145 };
server
CacheServer server
Definition: cache_server.cpp:236
loop_read
static ssize_t loop_read(int fd, void *data, size_t size)
Definition: requests.h:105
GossipPayload
Definition: requests.h:138
GetRequest::container
char container[10]
Definition: requests.h:14
EraseRequest::key
int key
Definition: requests.h:24
Address::port
int port
Definition: requests.h:50
RequestType::PUT
@ PUT
Address::getConnection
static int getConnection(Address addr)
Definition: requests.h:62
RequestType::GET_TARGET
@ GET_TARGET
RequestType::RESET
@ RESET
EraseRequest
Definition: requests.h:22
Address::operator==
bool operator==(const Address &other)
Definition: requests.h:53
GetResponse
Definition: requests.h:27
Address::ip
std::string ip
Definition: requests.h:49
RequestType::PUT_TARGET
@ PUT_TARGET
RequestType
RequestType
Definition: requests.h:37
CreateRequest
Definition: requests.h:18
Address::toString
std::string toString() const
Definition: requests.h:60
PutRequest::key
int key
Definition: requests.h:33
RequestType::ERASE
@ ERASE
Address
Definition: requests.h:48
PutRequest::value
int value
Definition: requests.h:34
loop_write
static ssize_t loop_write(int fd, const void *data, size_t size)
Definition: requests.h:82
RequestType::GOSSIP
@ GOSSIP
PutRequest
Definition: requests.h:31
PayloadHeader
Definition: requests.h:142
Address::operator<
bool operator<(const Address &other) const
Definition: requests.h:56
RequestType::CREATECONTAINER
@ CREATECONTAINER
GetRequest::key
int key
Definition: requests.h:15
Address::Address
Address(const std::string ip="invalid", const int port=-1)
Definition: requests.h:51
RequestType::GET
@ GET
PayloadHeader::type
char type[20]
Definition: requests.h:143
PayloadHeader::size
int size
Definition: requests.h:144
CreateRequest::container
char container[10]
Definition: requests.h:19
PutRequest::container
char container[10]
Definition: requests.h:32
GetResponse::value
int value
Definition: requests.h:28
GossipPayload::gossipArtifactCount
int gossipArtifactCount
Definition: requests.h:139
EraseRequest::container
char container[10]
Definition: requests.h:23
GetRequest
Definition: requests.h:13