summaryrefslogtreecommitdiff
path: root/server/main.c
blob: cb4e2b4b033455d89ad1fb5194602d3653eb9540 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include "global.h"
#include "data_stream.h"
#include "line_reader.h"
#include "zbuffer.h"
#include "util.h"

#define PORT 57575

#define POLLDELAY (10*1000000)

/*struct proc_taskinfo {
    uint64_t        pti_virtual_size;      virtual memory size (bytes)
    uint64_t        pti_resident_size;     resident memory size (bytes)
    uint64_t        pti_total_user;        total time
    uint64_t        pti_total_system;
    uint64_t        pti_threads_user;      existing threads only
    uint64_t        pti_threads_system;
    int32_t         pti_policy;            default policy for new threads
    int32_t         pti_faults;            number of page faults
    int32_t         pti_pageins;           number of actual pageins
    int32_t         pti_cow_faults;        number of copy-on-write faults
    int32_t         pti_messages_sent;     number of messages sent
    int32_t         pti_messages_received; number of messages received
    int32_t         pti_syscalls_mach;     number of mach system calls
    int32_t         pti_syscalls_unix;     number of unix system calls
    int32_t         pti_csw;               number of context switches
    int32_t         pti_threadnum;         number of threads in the task
    int32_t         pti_numrunning;        number of running threads
    int32_t         pti_priority;          task priority
};*/

static void connection_handler(int sock){
	struct data_stream *stream=data_stream_init(sock);
	struct line_reader *reader=line_reader_init(sock);

	i64 polldelay=POLLDELAY;
	i64 timeleft=polldelay;

	bool do_send_frames=false;

	bool do_quit=false;

	while(true){
		struct timeval timeout={timeleft/1000000,timeleft%1000000};
		i64 before=make_timestamp();

		fd_set inset;
		FD_ZERO(&inset);
		FD_SET(sock,&inset);
		int ret=select(sock+1,&inset,NULL,NULL,do_send_frames?&timeout:NULL);
		if(ret<0){
			if(errno==EINTR)continue;
			perror("select");
			break;
		}
		if(ret==0||!FD_ISSET(sock,&inset)){  // timeout
			before=make_timestamp();
			if(do_send_frames){
				if(data_stream_frame(stream)<0){
					printf("Error sending data\n");
					goto cleanup;
				}
				printf("."); fflush(stdout);
			}
			timeleft=polldelay;

			i64 after=make_timestamp();
			if(after<before){
				printf("Time ran backwards?\n");
				after=before;
			}
			timeleft-=after-before;
			if(timeleft<0)timeleft=0;
			continue;
		}

		i64 after=make_timestamp();
		if(after<before){
			printf("Time ran backwards?\n");
			after=before;
		}
		timeleft-=after-before;
		if(timeleft<0)timeleft=0;

		char buf[1024];
		i64 nr=read(sock,buf,sizeof buf);
		if(nr<0){
			if(errno==EINTR)continue;
			perror("read");
			break;
		}
		if(nr==0)break;  // EOF
		line_reader_supply_data(reader,buf,nr);

		char *line;
		while((line=line_reader_get_line(reader,false))!=NULL){
			char *word=line;

			printf("Read <%s>\n",line);

			char *next=strchr(word,' ');
			if(next!=NULL){
				*next='\0';
				next++;
			}
			if(strcmp(word,"delay")==0){
				char *endp;
				i64 v=strtoll(next,&endp,10);
				if(*next=='\0'||*endp!='\0'||v<=1000){
					free(line);
					printf("Invalid number in delay\n");
					continue;
				}
				polldelay=v;
				printf("polldelay -> %" PRIi64 "\n",polldelay);
				if(timeleft>polldelay)timeleft=polldelay;
			} else if(strcmp(word,"start")==0){
				do_send_frames=true;
				printf("Sending enabled\n");
			} else if(strcmp(word,"stop")==0){
				do_send_frames=false;
				printf("Sending disabled\n");
			} else if(strcmp(word,"flush")==0){
				printf("Flushing\n");
				if(data_stream_flush(stream)<0){
					printf("Error flushing\n");
					goto cleanup;
				}
			} else if(strcmp(word,"quit")==0){
				printf("Quitting by request\n");
				do_quit=true;
				goto cleanup;
			} else {
				printf("Read unrecognised line <%s>!\n",line);
			}

			free(line);
		}
	}

cleanup:
	data_stream_finish_destroy(stream);
	if(do_quit)exit(0);
}

int main(void){
	int sock=socket(PF_INET,SOCK_STREAM,0);
	if(sock<0){
		perror("socket");
		return 1;
	}

	int one=1;
	if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&one,sizeof(int))<0){
		perror("setsockopt");
		return 1;
	}

	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_addr.s_addr=htonl(INADDR_ANY);
	sin.sin_port=htons(PORT);

	if(bind(sock,(struct sockaddr*)&sin,sizeof(sin))<0){
		close(sock);
		perror("bind");
		return 1;
	}

	if(listen(sock,1)<0){
		perror("listen");
		return 1;
	}

	printf("Listening on port %d\n",PORT);

	while(true){
		struct sockaddr_storage client_addr;
		socklen_t client_addr_sz=sizeof(client_addr);
		int clientsock=accept(sock,(struct sockaddr*)&client_addr,&client_addr_sz);
		if(clientsock<0){
			perror("accept");
			continue;
		}

		char str[INET_ADDRSTRLEN];
		inet_ntop(client_addr.ss_family,&((struct sockaddr_in*)&client_addr)->sin_addr,
		          str,sizeof(str));

		printf("Accept from %s\n",str);
		connection_handler(clientsock);
		close(clientsock);
	}
}