-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFluidServer.c
166 lines (137 loc) · 2.98 KB
/
FluidServer.c
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
/*
* FluidServer.c
* FluidApp
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
#include <signal.h>
#include "net.h"
#include "fluid.h"
#include "error.h"
#include "memory.h"
#include "field.h"
#include "mpx.h"
fluid *r_fluid;
fluidServer *r_server;
fluidMessenger *r_messenger;
int usage()
{
printf( "FluidServer cpu 4 size 512 512\n"
"==================================\n"
"cpu tells number of cores to use\n"
" for heavy work. Use less if\n"
" Max is running on same machine\n"
"size is resolution of simulation.\n"
" width must be multiple of 32.\n");
return 0;
}
int main(int argc, char *argv[])
{
struct sigaction s;
memset(&s, 0, sizeof(s));
sigset_t ss;
sigemptyset(&ss);
sigaddset(&ss, SIGPIPE);
s.sa_mask = ss;//SIGPIPE;
s.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &s, NULL);
x_init(); //Setup exception handling / memory management.
int procs = 0;
int w = 720;
int h = 480;
int x;
for (x=1; x<argc; x++)
{
if (strcmp(argv[x], "cpu") == 0)
{
if (x+1 >= argc)
return usage();
x++;
procs = atoi(argv[x]);
}
else if (strcmp(argv[x], "size") == 0)
{
if (x + 2 >= argc)
return usage();
x++;
w = atoi(argv[x]);
x++;
h = atoi(argv[x]);
}
else
{
return usage();
}
}
if (procs <= 0 || w<= 0 || h<= 0 || w%32 != 0)
{
return usage();
}
x_try
mpInit(procs); //Start up enough threads for system
printf("\n\nFluid Server Launching\n");
printf(" - cpu %i size %i %i", procs, w, h);
r_fluid = fluidCreate(w, h);
printf(" - Created Fluid\n");
r_server = fluidServerCreate(r_fluid);
printf(" - Created networking agent\n");
r_messenger = fluidMessengerCreate(r_fluid, r_server);
printf(" - Created messenger\n");
printf("Fluid Server Launched!\n");
double t1 = x_time();
double totalTime = 0;
int frames = 0;
int done = 0;
while (!done)
{
double t2 = x_time();
fluidServerOnFrame(r_server);
frames ++;
totalTime += (t2-t1);
t1 = t2;
if (totalTime > 10.0f)
{
printf("FPS: %f (%f ms/frame)\n", (float)frames / totalTime, totalTime/(float)frames);
frames = 0;
totalTime = 0;
}
fieldMsg *m;
while (m = fluidServerNextMessage(r_server))
{
if (isFieldCharPtr(m, 0))
{
if (strcmp(fieldCharPtr(m, 0), "quit") == 0)
{
done = 1;
break;
}
}
int i;
printf("Execute: ");
for (i=0; i<fieldMsgCount(m); i++)
{
if (isFieldCharPtr(m, i))
printf("%s ", fieldCharPtr(m, i));
else if (isFieldInt(m, i))
printf("%i ", fieldInt(m, i));
else if (isFieldFloat(m, i))
printf("%f ", fieldFloat(m, i));
}
printf("\n");
fluidMessengerHandleMessage(r_messenger, m);
}
}
x_free(r_server);
x_free(r_messenger);
x_free(r_fluid);
return 0;
x_catch(e)
printf("In Handler: %s\n", errorMsg(e));
x_finally
mpTerminate();
return 0;
}