source: proiecte/SolarSim/C/Parallel/src/io.c @ 152

Last change on this file since 152 was 152, checked in by (none), 14 years ago
File size: 4.4 KB
Line 
1/* io.c */
2#include <sys/types.h>
3#include <sys/socket.h>
4#include <arpa/inet.h>
5#include <netdb.h>
6#include <math.h>
7
8#include "SolarSim.h"
9
10void
11read_initial( AState state, const char *filename ) {
12        int i;
13        double rx, ry, rz, vx, vy, vz, mass;
14
15        FILE *f = fopen(filename, "rt");
16        if( !f ) {
17                state->particles = 0;
18                return;
19        }
20
21        fscanf( f, "%d\n", &state->particles );
22        state->masses = malloc( state->particles * sizeof(real) );
23        state->positions = malloc( state->particles * sizeof(vector) );
24        state->velocities = malloc( state->particles * sizeof(vector) );
25        state->accelerations = malloc( state->particles * sizeof(vector) );
26       
27        for( i = 0; i < state->particles; i++ ) {
28                fscanf( f, "%lf %lf %lf %lf %lf %lf %lf\n", &rx, &ry, &rz, &vx, &vy, &vz, &mass );
29                state->masses[i] = mass;
30                v_init(&state->positions[i],(real)rx,(real)ry,(real)rz);
31                v_init(&state->velocities[i],(real)vx,(real)vy,(real)vz);
32        }
33
34        fclose(f);
35}
36
37void
38rand_data( AState state, int nParticles ) {
39        int i;
40        double rx, ry, rz, vx, vy, vz, mass;
41
42        state->particles = nParticles;
43        state->masses = malloc( state->particles * sizeof(real) );
44        state->positions = malloc( state->particles * sizeof(vector) );
45        state->velocities = malloc( state->particles * sizeof(vector) );
46        state->accelerations = malloc( state->particles * sizeof(vector) );
47
48        srand( time(NULL) );
49        for( i = 0; i < nParticles; i++ ) {
50                rx = ((double)rand()) / RAND_MAX * 150E09;
51                ry = ((double)rand()) / RAND_MAX * 150E09;
52                rz = ((double)rand()) / RAND_MAX * 150E09;
53                vx = ((double)rand()) / RAND_MAX * 150E09;
54                vy = ((double)rand()) / RAND_MAX * 150E09;
55                vz = ((double)rand()) / RAND_MAX * 150E09;
56                mass = ((double)rand()) / RAND_MAX * 1E20;
57               
58                state->masses[i] = mass;
59                v_init(&state->positions[i],(real)rx,(real)ry,(real)rz);
60                v_init(&state->velocities[i],(real)vx,(real)vy,(real)vz);
61        }
62}
63
64int
65connect_to_server( char *servername, int portno, int PROTOCOL ){
66        return connect_to_server_tcp(servername, portno);
67}
68
69int
70connect_to_server_tcp( char* servername, int portno ){
71        struct sockaddr_in serv_addr;
72        struct hostent *server;
73        int server_sockfd;
74
75        server_sockfd = socket( AF_INET, SOCK_STREAM, 0 );
76        if ( server_sockfd < 0 ) {
77                printf ( "ERROR opening socket" );
78                return -1;
79        }
80   
81        server = gethostbyname( servername );
82        if (server == NULL) {
83                fprintf( stderr, "ERROR, no such host\n" );
84                return -1;
85        }
86   
87        memset( (char *) &serv_addr, 0, sizeof(serv_addr) );
88        serv_addr.sin_family = AF_INET;
89        memcpy( (char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr, server->h_length );
90        serv_addr.sin_port = htons( portno );
91   
92        if ( connect( server_sockfd, (struct sockaddr*) &serv_addr, sizeof( serv_addr ) ) < 0 ) {
93                printf( "ERROR connecting" );
94                return -1;
95        }
96
97        return server_sockfd;
98}
99
100
101int
102connect_to_server_udp( char* servername, int portno ){
103        struct sockaddr_in serv_addr, cli_addr;
104        struct hostent *server;
105        int server_sockfd, n;
106
107        server_sockfd = socket( AF_INET, SOCK_DGRAM, 0 );
108        if ( server_sockfd < 0 ) {
109                printf ( "ERROR opening socket" );
110                return -1;
111        }
112   
113        server = gethostbyname( servername );
114        if (server == NULL) {
115                fprintf( stderr, "ERROR, no such host\n" );
116                return -1;
117        }
118   
119        memset( (char *) &serv_addr, 0, sizeof(serv_addr) );
120        serv_addr.sin_family = AF_INET;
121        memcpy( (char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr, server->h_length );
122        serv_addr.sin_port = htons( portno );
123
124        memset( (char *) &cli_addr, 0, sizeof(cli_addr) );
125        cli_addr.sin_family = AF_INET;
126        cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
127        cli_addr.sin_port = htons(0);
128
129        n = bind(server_sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr));
130        if( n < 0 ) {
131                fprintf(stderr, "%d: cannot bind port\n", portno);
132                return -1;
133        }
134
135        return server_sockfd;
136}
137
138void
139send_state( int sock, AState state ) {
140        send( sock, &state->particles, sizeof(int), 0 );
141        send( sock, state->masses, state->particles*sizeof(real), 0 );
142        send( sock, state->positions, state->particles*sizeof(vector), 0 );
143        send( sock, state->velocities, state->particles*sizeof(vector), 0 );
144}
145
146void
147write_state( int fd, AState state ) {
148        write( fd, &state->particles, sizeof(int) );
149        write( fd, state->masses, state->particles*sizeof(real) );
150        write( fd, state->positions, state->particles*sizeof(vector) );
151        write( fd, state->velocities, state->particles*sizeof(vector) );
152}
Note: See TracBrowser for help on using the repository browser.