|
| 1 | +#include <stdlib.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include "ccs-client.h" |
| 6 | + |
| 7 | +#define SHRINK 1 |
| 8 | +#define EXPAND 0 |
| 9 | +#define OP EXPAND |
| 10 | + |
| 11 | +#define BUF 255 |
| 12 | + |
| 13 | +int main (int argc, char **argv) |
| 14 | +{ |
| 15 | + int OLDNPROCS, NEWNPROCS; |
| 16 | + |
| 17 | + if (argc < 5) { |
| 18 | + printf("Usage: %s <hostname> <port> <oldprocs> <newprocs> \n", argv[0]); |
| 19 | + return 1; |
| 20 | + } |
| 21 | + |
| 22 | + // Create a CcsServer and connect to the given hostname and port |
| 23 | + CcsServer server; |
| 24 | + char host[BUF], *bitmap; |
| 25 | + int i, port, cmdLen, mode; |
| 26 | + |
| 27 | + sprintf(host, "%s", argv[1]); |
| 28 | + sscanf(argv[2], "%d", &port); |
| 29 | + sscanf(argv[3], "%d", &OLDNPROCS); |
| 30 | + sscanf(argv[4], "%d", &NEWNPROCS); |
| 31 | + |
| 32 | + //printf("Rescaling from %i to %i\n", OLDNPROCS, NEWNPROCS); |
| 33 | + |
| 34 | + if( NEWNPROCS > OLDNPROCS) |
| 35 | + mode = EXPAND; |
| 36 | + else if(OLDNPROCS > NEWNPROCS) |
| 37 | + mode = SHRINK; |
| 38 | + else{ |
| 39 | + printf("0"); |
| 40 | + return 0; |
| 41 | + } |
| 42 | + //printf("Connecting to server %s %d\n", host, port); |
| 43 | + if (CcsConnect(&server, host, port, NULL) == -1) { |
| 44 | + printf("0"); |
| 45 | + return 0; |
| 46 | + } |
| 47 | + //printf("Connected to server\n"); |
| 48 | + |
| 49 | + cmdLen = OLDNPROCS * sizeof(char) + sizeof(int) + sizeof(char); |
| 50 | + bitmap = (char *) malloc(cmdLen); |
| 51 | + |
| 52 | + if (mode == EXPAND) { |
| 53 | + //printf("Sending expand command.\n"); |
| 54 | + for (i = 0; i < OLDNPROCS; i++) { |
| 55 | + bitmap[i] = 1; |
| 56 | + } |
| 57 | + } |
| 58 | + else { |
| 59 | + //printf("Sending shrink command.\n"); |
| 60 | + for (i = 0; i < OLDNPROCS; i++) { |
| 61 | + if (i < NEWNPROCS) |
| 62 | + bitmap[i] = 1; |
| 63 | + else |
| 64 | + bitmap[i] = 0; |
| 65 | + } |
| 66 | + } |
| 67 | + memcpy(&bitmap[OLDNPROCS], &NEWNPROCS, sizeof(int)); |
| 68 | + bitmap[OLDNPROCS+sizeof(int)] = '\0'; |
| 69 | + if (CcsSendRequest(&server, "set_bitmap", 0, cmdLen, bitmap) == -1) { |
| 70 | + printf("0"); |
| 71 | + return 0; |
| 72 | + } |
| 73 | + |
| 74 | + //printf("Waiting for reply...\n" ); |
| 75 | + if (CcsRecvResponse(&server, cmdLen, bitmap , 180) == -1) { |
| 76 | + printf("0"); |
| 77 | + return 0; |
| 78 | + } |
| 79 | + //printf("Reply received.\n"); |
| 80 | + printf("1"); |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
0 commit comments