1 //
2 // Copyright (C) 2015 Google, Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at:
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "base.h"
18 #include <rapidjson/document.h>
19 #include <rapidjson/writer.h>
20 #include <rapidjson/stringbuffer.h>
21 #include "utils/command_receiver.h"
22
23 #include <arpa/inet.h>
24 #include <errno.h>
25 #include <iostream>
26 #include <netinet/in.h>
27 #include <pthread.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <unistd.h>
33
34 const int kBacklogInt = 10;
35 #define PORT 8080
36 #define SOCK_BUF_LEN 100
37 #define MEMSET_VALUE 0
38
39 int client_sock;
40 int socket_desc;
41
SockTest()42 void SockTest() {
43 char str[SOCK_BUF_LEN];
44 int listen_fd, comm_fd, c;
45 struct sockaddr_in servaddr, client;
46 rapidjson::Document d;
47 rapidjson::StringBuffer buffer;
48 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
49 CommandReceiver cr;
50
51 listen_fd = socket(AF_INET, SOCK_STREAM, 0);
52 memset (&servaddr, MEMSET_VALUE, sizeof(servaddr));
53 servaddr.sin_family = AF_INET;
54 servaddr.sin_addr.s_addr = INADDR_ANY;
55 servaddr.sin_port = htons(PORT);
56
57 int bind_result = bind(
58 listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
59 if (bind_result != 0) {
60 LOG(ERROR) << sl4n::kTagStr <<
61 ": Failed to assign the address to the socket."
62 << " Error: " << strerror(errno) << ", " << errno;
63 exit(1);
64 }
65
66 int listen_result = listen(listen_fd, kBacklogInt);
67 if (listen_result != 0) {
68 LOG(ERROR) << sl4n::kTagStr << ": Failed to setup the passive socket."
69 << " Error: " << strerror(errno) << ", " << errno;
70 exit(1);
71 }
72
73 comm_fd = accept(listen_fd, (struct sockaddr*)&client, (socklen_t*)&c);
74 if (comm_fd == -1) {
75 LOG(ERROR) << sl4n::kTagStr << ": Failed to accept the socket."
76 << " Error: " << strerror(errno) << ", " << errno;
77 exit(1);
78 }
79
80 while (true) {
81 memset(str, MEMSET_VALUE, sizeof(str));
82 int read_result = read(comm_fd, str, SOCK_BUF_LEN);
83 if (read_result < 0) {
84 LOG(FATAL) << sl4n::kTagStr << ": Failed to write to the socket."
85 << " Error: " << strerror(errno) << ", " << errno;
86 exit(1);
87 }
88
89 d.Parse(str);
90 cr.Call(d);
91 d.Accept(writer);
92 std::string str2 = buffer.GetString();
93 str2 += '\n';
94 strncpy(str, str2.c_str(), sizeof(str)-1);
95 int result = write(comm_fd, str, strlen(str)+1);
96 if (result < 0) {
97 LOG(FATAL) << sl4n::kTagStr << ": Failed to write to the socket."
98 << " Error: " << strerror(errno) << ", " << errno;
99 exit(1);
100 }
101 d.RemoveAllMembers(); // Remove all members from the json object
102 buffer.Clear();
103 }
104 }
105
main(int argc,char ** argv)106 int main(int argc, char **argv) {
107 logging::LoggingSettings log_settings;
108 if (!logging::InitLogging(log_settings)) {
109 LOG(ERROR) << "Failed to set up logging";
110 return EXIT_FAILURE;
111 }
112 SockTest();
113 return 0;
114 }
115