My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
write_msu.c
Go to the documentation of this file.
1 /*
2 START OF LICENSE STUB
3  DeDOS: Declarative Dispersion-Oriented Software
4  Copyright (C) 2017 University of Pennsylvania, Georgetown University
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 END OF LICENSE STUB
19 */
20 #include "webserver/write_msu.h"
21 #include "socket_msu.h"
23 #include "msu_state.h"
24 #include "logging.h"
25 #include "routing_strategies.h"
26 #include "profiler.h"
27 #include "local_msu.h"
28 
29 static int write_http_response(struct local_msu *self,
30  struct msu_msg *msg) {
31  struct response_state *resp_in = msg->data;
32 
33  size_t size = 0;
34  struct response_state *resp = msu_get_state(self, &msg->hdr.key, &size);
35  if (resp == NULL) {
36  resp = msu_init_state(self, &msg->hdr.key, sizeof(*resp));
37  memcpy(resp, resp_in, sizeof(*resp_in));
38  }
39 
40  int rtn = write_response(resp);
41  if (rtn & WS_ERROR) {
42  msu_error(self, NULL, 0);
44  if (close_connection(&resp->conn) == WS_ERROR) {
45  msu_error(self, NULL, 0);
46  }
47  msu_free_state(self, &msg->hdr.key);
48  free(resp_in);
49  return -1;
50  } else if (rtn & (WS_INCOMPLETE_READ | WS_INCOMPLETE_WRITE)) {
51  rtn = msu_monitor_fd(resp->conn.fd, RTN_TO_EVT(rtn), self, &msg->hdr);
52  free(resp_in);
53  return rtn;
54  } else {
57  if (close_connection(&resp->conn) == WS_ERROR) {
58  msu_error(self, NULL, 0);
59  }
60  log(LOG_WEBSERVER_WRITE, "Successful connection to fd %d closed",
61  resp->conn.fd);
62  log(LOG_WEBSERVER_WRITE, "Wrote request: %s", resp->body);
63  msu_free_state(self, &msg->hdr.key);
64  free(resp_in);
65  return 0;
66  }
67 }
68 
70  .name = "Webserver_write_MSU",
72  .receive = write_http_response,
74 };
75 
76 
int msu_error(struct local_msu *msu, struct msu_msg_hdr *hdr, int broadcast)
Definition: local_msu.c:353
int msu_remove_fd_monitor(int fd)
Definition: socket_msu.c:87
void * msu_get_state(struct local_msu *msu, struct msu_msg_key *key, size_t *size)
Gets the state allocated with the given key.
Definition: msu_state.c:82
struct connection conn
State storage that is tied to a specific MSU mesasge.
void * data
Payload.
Definition: msu_message.h:104
#define WS_INCOMPLETE_READ
Definition: webserver.h:25
Logging of status messages to the terminal.
int write_response(struct response_state *state)
struct msu_type WEBSERVER_WRITE_MSU_TYPE
Definition: write_msu.c:69
#define WS_INCOMPLETE_WRITE
Definition: webserver.h:26
int close_connection(struct connection *conn)
void * msu_init_state(struct local_msu *msu, struct msu_msg_key *key, size_t size)
Initializes a new MSU state of the given size with the provided key.
Definition: msu_state.c:55
For profiling the path of MSU messages through DeDOS.
Declares the structures and functions applicable to MSUs on the local machine.
#define PROFILE_EVENT(hdr, stat_id)
If the header is marked for profiling, profiles the given event.
Definition: profiler.h:77
Profiling.
Definition: stat_ids.h:68
The structure that represents an MSU located on the local machine.
Definition: local_msu.h:38
Defines a type of MSU.
Definition: msu_type.h:46
#define RTN_TO_EVT(rtn__)
Definition: webserver.h:40
struct msu_msg_hdr hdr
Definition: msu_message.h:102
#define WS_ERROR
Definition: webserver.h:27
int msu_monitor_fd(int fd, uint32_t events, struct local_msu *destination, struct msu_msg_hdr *hdr)
Definition: socket_msu.c:58
#define log(level, fmt,...)
Log at a custom level.
Definition: logging.h:147
A message that is to be delivered to an instance of an MSU.
Definition: msu_message.h:101
#define WEBSERVER_WRITE_MSU_TYPE_ID
Definition: msu_ids.h:28
int msu_free_state(struct local_msu *msu, struct msu_msg_key *key)
Frees the state assocated with the given MSU and key.
Definition: msu_state.c:97
char * name
Name for the msu type.
Definition: msu_type.h:48
int route_to_origin_runtime(struct msu_type *type, struct local_msu *sender, struct msu_msg *msg, struct msu_endpoint *output)
Routes an MSU message to the runtime on which the message originated.
Declares strategies that MSUs can use for routing to endpoints.
struct msu_msg_key key
Routing/state key.
Definition: msu_message.h:87
static int write_http_response(struct local_msu *self, struct msu_msg *msg)
Definition: write_msu.c:29