SimpleMove.cpp 4.44 KB
Newer Older
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#include "NOMADVRLib/ConfigFileAtoms.h"
#include "NOMADVRLib/atoms.hpp"
#include "PeriodicTable.h"
#include "exportXYZ.h"
#include "state.h"
#include "myrecv.h"

#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#define closesocket close
#endif

const int NUMATOMS=118;

int main (int argc, char ** argv)
{

if (argc!=4) {
	fprintf (stderr, "Parameters: <server> <port> <secret>\n");
	return 1;
}

state_t state;

numAtoms=new int[1];
numAtoms[0]=NUMATOMS;

atoms=new float* [1];
atoms[0]=new float[NUMATOMS*4];

CreatePeriodicTable(atoms[0], 5);

exportXYZ("A.xyz", "PeriodicTable", 1);

FILE *ncfg=fopen ("A.ncfg", "w");
fprintf (ncfg, "xyzfile A.xyz\n"
	"server %s %s %s\n"
	"menubutton Infobox\n"
	"sidebuttontimestep 0\n", argv[1], argv[2], argv[3]);
fclose (ncfg);

//connect to server, TCP

int sock;

struct hostent *he;
struct sockaddr_in serv_addr;

	if ( (he = gethostbyname(argv[1]) ) == nullptr ) {
		fprintf (stderr, "Connect to server, could not get host name %s\n", argv[1]);
      return 2; /* error */
	}
	memset((char *) &serv_addr, 0, sizeof(serv_addr));
	memcpy(&serv_addr.sin_addr, he->h_addr_list[0], he->h_length);
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(atoi(argv[2]));
	sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if ( connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) {
				fprintf (stderr, "Connect to server, could not get connection %s\n", argv[1]);
      return 3; /* error */
	}

int n;
	int32_t tmp;
	tmp=htonl (atoi(argv[3])); //secret
	n = send(sock, (char*)&tmp , sizeof(tmp), 0);
	if (n<sizeof(tmp)) {
		closesocket(sock);
		sock=INVALID_SOCKET;
		fprintf (stderr, "couldn't send secret\n");
		return 4;
	}


char buffer[100];

uint32_t ID;
int found;
while (true) {
	n=myrecv(sock, buffer, 1, 0);
	if (n<1) {
			fprintf (stderr, "couldn't receive data command");
			return 5;
	}
	printf ("received order %c\n", buffer[0]);
	switch (buffer[0]) {
		case 'X': // ID
		n=myrecv(sock, &ID, 4, 0);
		if (n<4) {
			fprintf (stderr, "couldn't receive data X");
			return 5;
		}
		break;

		case 't': 
		n=myrecv(sock, &state.timestep, 4, 0);
		if (n<4) 
{
			fprintf (stderr, "couldn't receive data t");
			return 5;
		}
		break;

		case 'i':
		n=myrecv(sock, &state.iso, 4, 0);
		if (n<4) 
{
			fprintf (stderr, "couldn't receive data i\n");
			return 5;
		}
		break;

		case 's': 
		n=myrecv(sock, &state.showatoms, 1, 0);
		if (n<1) 
{
			fprintf (stderr, "couldn't receive data s\n");
			return 5;
		}
		break;

		case 'n': 
		n=myrecv(sock, &state.ncfg, 4, 0);
		if (n<4) 
{
			fprintf (stderr, "couldn't receive data n\n");
			return 5;
		}
		break;

		case 'p': // user position
		n=myrecv (sock, buffer+1, sizeof(float)*3 + 4, 0);
		if (n<sizeof(float)*3 + 4) {
			fprintf (stderr, "couldn't receive data p\n");
			return 5;
		}
		break;

		case 'd': //disconnect
		n=myrecv (sock, buffer+1, 4, 0);
		if (n<4) 
{
			fprintf (stderr, "couldn't receive data d\n");
			return 5;
		}
		break;

		case 'D': //Drag
		n=myrecv (sock, buffer+1, 6*sizeof(float) + 4*2 +1, 0);
		if (n<6*sizeof(float) + 4*2 +1) 
{
			fprintf (stderr, "couldn't receive data D\n");
			return 5;
		}
		//find the nearest atom
		//test, use atom 0
		float orig[3];
		float dest[3];
		uint32_t ID, time;
		char button;
		memcpy (&ID, buffer+1, 4);
		memcpy (orig, buffer+5, 3*sizeof(float));
		memcpy (dest, buffer+5+3*sizeof(float), 3*sizeof(float));
		memcpy (&time, buffer+5+6*sizeof(float), 4);
		button=buffer[1+6*sizeof(float)+8];
		if (button!=2) {
			printf ("intermediate, discarding\n");
			continue;
		}
		found=-1;
		for (int i=0;i<NUMATOMS;i++) {
			float sum=0;
			for (int j=0;j<3;j++) {
				const float tmp=atoms[0][i*4+j]-orig[j];
				sum+=tmp*tmp;
			}
			const float r=atomRadius(static_cast<int>(atoms[0][i*4+3]));
			if (sum < r*r) {
				found=i;
				break;
			}
			
		}
		if (found!=-1) {
			printf ("Starting Drag atom %d\n", found);
			//drag it
			for (int i=0;i<3;i++)
				atoms[0][found*4+i]+=dest[i]-orig[i];

			exportXYZ("A.xyz", "PeriodicTable", 1);
			//reload scene

			buffer[0]='n';
			memcpy(buffer+1, (char*)&state.ncfg, 4);
			n=send (sock, buffer, 5, 0);
			if (n<5) {
				fprintf (stderr, "could not send reload command\n");
				return 6;
			}
			printf ("Reload sent\n");
		}
		break;

		default: 
			fprintf (stderr, "Unknown command received: '%c', %d\n", buffer[0], buffer[0]);
			return -5;
	}
}

return 0;
}