Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <errno.h>
- #include <limits.h>
- #include <stdutil/stdutil.h>
- #include <stdutil/stdhash.h>
- #include <stdutil/stddll.h>
- #include <stdutil/stderror.h>
- #include "sp.h"
- static char User[80];
- static char Spread_name[80];
- static mailbox Mbox;
- static int outputInterval;
- static char Private_group[MAX_GROUP_NAME];
- static void parseArgs( int argc, char *argv[] );
- static void Print_help();
- static void showStats(stdhash* stats);
- static int group_name_ptr_cmp(const void *strptr1, const void *strptr2);
- static stdhcode group_name_hashcode(const void *str);
- static stdhcode group_name_ptr_hashcode(const void *strptr);
- static void incrementStats(stdhash* stats, char *key);
- #define MY_MAX_NUM_GROUPS 1000
- #define MY_MAX_MESS_SIZE 102400
- static stddll wantedGroups;
- int main( int argc, char *argv[] )
- {
- sp_time connect_timeout = { 0, 50*1000 };
- int ret;
- parseArgs( argc, argv );
- ret = SP_connect_timeout(Spread_name, User, 0, 1, &Mbox, Private_group, connect_timeout ) ;
- if(ret < 0)
- {
- SP_error(ret);
- return EXIT_FAILURE;
- }
- printf("Connected\n");
- stdit hit;
- for (stddll_begin(&wantedGroups, &hit); !stddll_is_end(&wantedGroups, &hit); stddll_it_next(&hit)) {
- ret = SP_join( Mbox, *(char**)stddll_it_val(&hit));
- if(ret < 0)
- {
- SP_error(ret);
- return EXIT_FAILURE;
- }
- printf("Joined group %s\n", *(char**)stddll_it_val(&hit));
- }
- int mess_len;
- service service_type;
- char sender[MAX_GROUP_NAME];
- int num_groups;
- int16 mess_type;
- int endian_mismatch;
- char mess[MY_MAX_MESS_SIZE];
- long totalCounter = 0;
- stdhash groupCounter;
- struct timespec lastOutput;
- if (clock_gettime(CLOCK_HIGHRES, &lastOutput) != 0) {
- printf("clock_gettime() error: %s\n", strerror(errno));
- }
- stdhash_construct(&groupCounter, sizeof(char*), sizeof(long*),
- group_name_ptr_cmp, group_name_ptr_hashcode, 0);
- while(1) {
- service_type = 0;
- char groupsArray[MY_MAX_NUM_GROUPS][MAX_GROUP_NAME];
- int result;
- fd_set readset;
- struct timeval timeout = {0, 100*1000}; /* 0.1 sec */
- FD_ZERO(&readset);
- FD_SET(Mbox, &readset);
- /* the non-zero timeout is important for making sure this loop doesn't tie up the CPU */
- /* it should be granular enough or rather smaller than the stats output interval */
- result = select(Mbox + 1, &readset, NULL, NULL, &timeout);
- if (result == -1) {
- printf("select() error: %s\n", strerror(errno));
- return EXIT_FAILURE;
- } else {
- /* ready to read */
- if (FD_ISSET(Mbox, &readset)) {
- /* The Mbox has data available to be read */
- if ((mess_len = SP_receive(Mbox, &service_type, sender, MY_MAX_NUM_GROUPS,
- &num_groups, groupsArray, &mess_type, &endian_mismatch,
- MY_MAX_MESS_SIZE, mess)) < 0) {
- fprintf(stderr, "SP_receive failure: ");
- SP_error(mess_len);
- return EXIT_FAILURE;
- }
- if (Is_regular_mess(service_type)) {
- totalCounter++;
- int i;
- for (i = 0; i < num_groups; i++) {
- incrementStats(&groupCounter, groupsArray[i]);
- }
- }
- }
- struct timespec time;
- if (clock_gettime(CLOCK_HIGHRES, &time) != 0) {
- printf("clock_gettime() error: %s\n", strerror(errno));
- }
- if((lastOutput.tv_sec + outputInterval) < time.tv_sec) {
- showStats(&groupCounter);
- lastOutput.tv_sec = time.tv_sec;
- lastOutput.tv_nsec = time.tv_nsec;
- }
- }
- }
- stdhash_destruct(&groupCounter);
- stddll_destruct(&wantedGroups);
- return EXIT_SUCCESS;
- }
- static void parseArgs(int argc, char *argv[]) {
- /* Setting defaults */
- sprintf(User, "counter");
- sprintf(Spread_name, "4803@localhost");
- outputInterval = 5;
- stddll_construct(&wantedGroups, sizeof(char*));
- while( --argc > 0 )
- {
- argv++;
- if (!strncmp( *argv, "-i", 2 )) {
- if (argc < 2) Print_help();
- outputInterval = atoi(argv[1]);
- argc--; argv++;
- } else if (!strncmp(*argv, "-u", 2)) {
- if (argc < 2) Print_help();
- strncpy(User, argv[1], 80);
- argc--; argv++;
- } else if (!strncmp(*argv, "-g", 2)) {
- if (argc < 2) Print_help();
- char *group;
- if ((group = malloc(MAX_GROUP_NAME)) == 0) {
- fprintf(stderr,"(%s, %d): malloc(%d) failed\n",
- __FILE__, __LINE__, MAX_GROUP_NAME);
- exit(EXIT_FAILURE);
- }
- memcpy(group, argv[1], MAX_GROUP_NAME);
- int ret;
- if ((ret = stddll_push_back(&wantedGroups, &group)) != 0) {
- fprintf(stderr, "stddll_push() error: %s\n", stderr_strerr(ret));
- exit(EXIT_FAILURE);
- }
- argc--; argv++;
- } else if (!strncmp(*argv, "-s", 2)) {
- if (argc < 2) Print_help();
- strncpy(Spread_name, argv[1], 80);
- argc--; argv++;
- } else {
- Print_help();
- }
- }
- if (stddll_size(&wantedGroups) <= 0) {
- printf("Please specify at least one group to join.\n");
- Print_help();
- exit(EXIT_FAILURE);
- }
- if (outputInterval <= 0) {
- printf("The output interval must be greater than 0.\n");
- Print_help();
- exit(EXIT_FAILURE);
- }
- }
- static void Print_help()
- {
- printf( "Usage: spcounter\n%s\n%s\n%s\n%s\n",
- "\t[-g <group name>] : message group name",
- "\t[-u <user name>] : unique Spread user name",
- "\t[-s <spread name>] : either port or port@machine",
- "\t[-i <seconds>] : statistics output interval");
- exit(EXIT_FAILURE);
- }
- static void showStats(stdhash* stats) {
- if (stdhash_size(stats) < 1)
- return;
- struct timespec time;
- if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
- printf("clock_gettime() error: %s\n", strerror(errno));
- }
- printf("%li.%li - ", time.tv_sec, time.tv_nsec);
- stdit hit;
- for (stdhash_begin(stats, &hit); !stdhash_is_end(stats, &hit); stdhash_it_next(&hit)) {
- if (!stdhash_is_begin(stats, &hit)) {
- printf(", ");
- }
- printf("%s %li", *(char**)stdhash_it_key(&hit), *(long*)stdhash_it_val(&hit));
- }
- printf("\n");
- }
- static int group_name_ptr_cmp(const void *strptr1, const void *strptr2) {
- return strncmp(*(const char**) strptr1, *(const char**) strptr2, MAX_GROUP_NAME);
- }
- static stdhcode group_name_hashcode(const void *str) {
- return stdhcode_sfh(str, strlen((const char*) str));
- }
- static stdhcode group_name_ptr_hashcode(const void *strptr) {
- return group_name_hashcode(*(const void**) strptr);
- }
- static void incrementStats(stdhash* stats, char *key) {
- stdit iter;
- if (stdhash_is_end(stats, stdhash_find(stats, &iter, &key))) {
- long count = 1;
- char *group;
- if ((group = malloc(MAX_GROUP_NAME)) == 0) {
- fprintf(stderr,"(%s, %d): malloc(%d) failed\n",
- __FILE__, __LINE__, MAX_GROUP_NAME);
- exit(EXIT_FAILURE);
- }
- memcpy(group, key, MAX_GROUP_NAME);
- int ret;
- if ((ret = stdhash_insert(stats, 0, &group, &count)) != 0) {
- fprintf(stderr, "stdhash_insert() error: %s\n", stderr_strerr(ret));
- exit(EXIT_FAILURE);
- }
- } else {
- *(long*)stdhash_it_val(&iter) += 1;
- }
- }
Add Comment
Please, Sign In to add comment