5751970 [rkeene@sledge /home/rkeene/devel/old/distlib]$ cat -n distlib.c
  1 /*
  2     Main library.   See Docs/model.txt (or perhaps, you should just try 
  3     to dicipher the comments in the code instead.)
  4     
  5     -- Roy Keene [151119990247] rkeene@suspend.net
  6 */
  7 
  8 #include <string.h>
  9 #include <stdlib.h>
 10 #include <sys/types.h>
 11 #include <sys/stat.h>
 12 #include <fcntl.h>
 13 #include <unistd.h>
 14 #ifdef DEBUG
 15 #include <stdio.h>
 16 #endif
 17 #include "distlib.h"
 18 #include "net.h"
 19 
 20 int taskid=1;
 21 int distlib_connection_fd[32]={-1, -1, -1, -1, -1, -1, -1, -1,
 22                                -1, -1, -1, -1, -1, -1, -1, -1,
 23                                -1, -1, -1, -1, -1, -1, -1, -1,
 24                                -1, -1, -1, -1, -1, -1, -1, -1};
 25 char *distlib_hosts[DISTLIB_NUM_HOSTS]={"10.0.0.1", "10.0.0.3"};
 26 struct distlib_hostinfo distlib_hostinfo_data[DISTLIB_NUM_HOSTS];
 27 
 28 int distlib_calc_int (int v1, int v2, unsigned char op, int n, int cost) {
 29     struct distlib_calc_msg1 mesg;
 30     mesg.v1=v1;
 31     mesg.v2=v2;
 32     mesg.op=op;
 33 #ifdef DEBUG
 34     printf("distlib.c: distlib_calc_int(%i, %i, %i, %i, %i)\n",v1,v2,op,n,cost);
 35     printf("distlib.c: distlib_calc_int: sizeof(mesg)=%i\n",sizeof(mesg));
 36     printf("distlib.c: distlib_calc_int: sizeof(distlib_calc_msg1)=%i\n",sizeof(struct distlib_calc_msg1));
 37 #endif
 38     return(distlib_sendmessage(0, &mesg, sizeof(mesg)-3, DISTLIB_TYPE_CALC_INT, 0, cost));
 39 }
 40 
 41 int distlib_calc_float (float v1, float v2, unsigned char op, int n, int cost) {
 42     struct distlib_calc_msg2 mesg;
 43     mesg.v1=v1;
 44     mesg.v2=v2;
 45     mesg.op=op;
 46 #ifdef DEBUG
 47     printf("distlib.c: distlib_calc_float(%f, %f, %i, %i, %i)\n",v1,v2,op,n,cost);
 48     printf("distlib.c: distlib_calc_float: sizeof(mesg)=%i\n",sizeof(mesg));
 49 #endif
 50     return(distlib_sendmessage(0, &mesg, sizeof(mesg)-3, DISTLIB_TYPE_CALC_FLOAT, 0, cost));
 51 }
 52 
 53 int distlib_exec (char path[127], char args[128][127], int cost) {
 54     struct distlib_exec_msg mesg;
 55     strcpy(mesg.path,path);
 56     return(-1);
 57 }
 58 
 59 
 60 int distlib_memset (char *name, char *value, int size) {
 61     struct distlib_mem *mesg;
 62     int taskid;
 63     mesg=malloc(size+64+sizeof(size));
 64     mesg->size=size;
 65 #ifdef DEBUG
 66     printf("distlib.c: distlib_memset(\"%s\",\"%s\",%i)\n",name,value,size);
 67     strncpy(mesg->name,name,63);
 68     printf("distlib.c: distlib_memset: strncpy(mesg.name, name, 63)\n");
 69     strcpy(&(mesg->value),value);
 70     printf("distlib.c: distlib_memset: strcpy(mesg.value,value)\n");
 71 #else
 72     strncpy(mesg->name,name,63);
 73     strcpy(mesg->value,value);
 74 #endif
 75     taskid=distlib_sendmessage(0, mesg,size+64+sizeof(size),DISTLIB_TYPE_MEM, 0, DISTLIB_COST_MEMSET);
 76     free(mesg);
 77     return(taskid);
 78 }
 79 
 80 /*
 81 distlib_msg_reply *distlib_getmem(char *name) {
 82     
 83 }
 84 */
 85 
 86 int distlib_sendmessage (int desthost, void *message, int size, unsigned char typeid, int taskid, unsigned char cost) {
 87     struct distlib_msg_hdr hdr;
 88     unsigned char dest[(sizeof(hdr)+size)];
 89     int new_taskid=0;
 90 #ifdef DEBUG
 91     int i;
 92 #endif
 93     hdr.size[0]=sizeof(dest)&0xff;
 94     hdr.size[1]=sizeof(dest)&0xff00;
 95     hdr.size[2]=sizeof(dest)&0xff0000;
 96     hdr.size[3]=sizeof(dest)&0xff000000;
 97     hdr.typeid=typeid;
 98     hdr.cost=cost;
 99     hdr.taskid=taskid;
100 #ifdef DEBUG
101     printf("distlib.c: distlib_sendmessage(%i, %lu, %i, %i, %i)\n", desthost, (unsigned long) &message, size, typeid,
	cost);
102 #endif
103     if (distlib_connection_fd[desthost]==-1) distlib_init(desthost);
104     memcpy((void *) dest, &hdr, sizeof(hdr));
105     memcpy(((void *) dest)+sizeof(hdr), message, size);
106     write(distlib_connection_fd[desthost],&dest,sizeof(dest));
107     read(distlib_connection_fd[desthost], &new_taskid, sizeof(new_taskid));
108 #ifdef DEBUG
109     printf("distlib.c: distlib_sendmessage: dest[]={");
110     for (i=0;i<sizeof(dest);i++)
111         printf("0x%-2x ", dest[i]);
112     printf("}\n");
113 #endif
114     return(new_taskid);
115 }
116 
117 void distlib_init (int dest) {
118 #ifdef DEBUG
119     printf("distlib.c: distlib_init(%i)\n", dest);
120     printf("distlib.c: distlib_init: distlib_hosts[%i]=%s\n", dest, distlib_hosts[dest]);
121 #endif
122     if (distlib_connection_fd[dest]!=-1) return;
123     distlib_hostinfo_data[dest].load=0;
124     distlib_hostinfo_data[dest].max_load=1000;
125     distlib_connection_fd[dest]=createconnection_tcp(distlib_hosts[dest] ,PORT);
126     return;
127 }
128 
129 int distlib_decode (unsigned char *data, int size) {
130     unsigned long packet_size;
131     int loc=0;
132     struct distlib_msg_hdr hdr;
133     void *packet;
134     while (1) {
135         memcpy(&hdr, &data[loc], sizeof(hdr));
136         packet_size=hdr.size[0]+(hdr.size[1]<<8)+(hdr.size[2]<<16)+(hdr.size[3]<<24);
137         packet=malloc(packet_size-sizeof(hdr));
138         memcpy(packet, &data[loc+sizeof(hdr)], packet_size-sizeof(hdr));
139         printf("%lu, %i, %i\n",packet_size, hdr.typeid, hdr.cost);
140         switch (hdr.typeid) {
141             case DISTLIB_TYPE_CALC_INT:
142                 printf("Got DISTLIB_TYPE_CALC_INT!\n");
143                 distlib_sendmessage(distlib_create_job(hdr.cost, hdr.taskid),packet,packet_size-sizeof(hdr), hdr.typeid,
	hdr.taskid, hdr.cost);
144                 break;
145             case DISTLIB_TYPE_CALC_FLOAT:
146                 printf("Got DISTLIB_TYPE_CALC_FLOAT!\n");
147                 break;
148             default:
149                 printf("Got type %i!\n",hdr.typeid);
150         }
151         loc+=packet_size;
152         if (loc>=size) break;       
153     }
154     free(packet);
155     return(-1);
156 }
157 
158 
159 int distlib_create_taskid (void) {
160 #ifdef DEBUG
161     printf("distlib.c: distlib_create_taskid()\n");
162 #endif
163     taskid++;
164     return(taskid);
165 }
166 
167 
168 /*
169     Look for a host with the lowest Load and add COST to it.
170     Return it's host ID.
171 */
172 int distlib_create_job (int cost, int taskid) {
173     int i,lowest=0;
174 #ifdef DEBUG
175     printf("distlib.c: distlib_create_job(%i, %i)\n", cost, taskid);
176 #endif
177     for (i=0;i<(DISTLIB_NUM_HOSTS-1);i++) {
178         if (distlib_hostinfo_data[i].load<lowest) {
179             lowest=distlib_hostinfo_data[i].load;
180             break;
181         }
182     }
183     distlib_hostinfo_data[i].load+=cost;
184 #ifdef DEBUG
185     printf("distlib.c: distlib_create_job: i=%i\n", i);
186 #endif
187     return(i);
188 }
5751971 [rkeene@sledge /home/rkeene/devel/old/distlib]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2000-02-17 05:18:41