Пример #1
0
TA_RetCode TA_DictFree( TA_Dict *dict )
{
   TA_PrivDictInfo *theDict;

   dnode_t   *node;
   dnode_t   *next;
   TA_String *stringToDelete;
   void      *valueToDelete;
   dict_t    *kazlibDict;
   TA_Libc   *libHandle;
   int        flags;

   theDict = (TA_PrivDictInfo *)dict;

   if( theDict == NULL )
      return TA_BAD_PARAM;

   kazlibDict = &theDict->d;
   libHandle  = theDict->libHandle;

   /* Delete all the key-value pair sequentially. */
   node = dict_first( libHandle, kazlibDict );

   while (node != NULL)
   {
      /* Get the next node. */
      next = dict_next( libHandle, kazlibDict, node );

      /* Free the 'node, the 'key' string and the 'value'. */
      flags = theDict->flags;
      valueToDelete  = dnode_get(node);
      if( flags & (TA_DICT_KEY_TWO_STRING|TA_DICT_KEY_ONE_STRING) )
      {
         stringToDelete = TA_StringFromChar(dnode_getkey(node));
         dict_delete_free( libHandle, kazlibDict, node );
         TA_StringFree( TA_GetGlobalStringCache( libHandle ), stringToDelete );
      }
      else
         dict_delete_free( libHandle, kazlibDict, node );

      if( flags & TA_DICT_KEY_TWO_STRING )
      {
         /* The value is a dictionary. Delete it. */
         TA_DictFree( (TA_Dict *)valueToDelete );
      }
      else if( theDict->freeValueFunc )
         theDict->freeValueFunc( libHandle, valueToDelete );

      node = next;
   }

   /* Free the TA_PrivDictInfo */
   TA_Free( libHandle, theDict );

   return TA_SUCCESS;
}
Пример #2
0
TA_RetCode TA_DictDeletePair_I( TA_Dict *dict, int key )
{
   TA_PrivDictInfo *theDict;
   void      *valueToDelete;
   dnode_t   *node;
   TA_Libc   *libHandle;
   dict_t    *kazlibDict;

   theDict = (TA_PrivDictInfo *)dict;

   if( theDict == NULL )
      return TA_BAD_PARAM;
   kazlibDict = &theDict->d;
   libHandle  = theDict->libHandle;

   /* Find the key-value pair. */
   node = dict_lookup( libHandle, kazlibDict, (void *)key );

   if( node )
   {
      /* Free the 'node' and the 'value'. */
      valueToDelete  = dnode_get(node);
      dict_delete_free( libHandle, kazlibDict, node );
      if( theDict->freeValueFunc )
         theDict->freeValueFunc( libHandle, valueToDelete );
   }
   else
      return TA_KEY_NOT_FOUND;

   return TA_SUCCESS;
}
Пример #3
0
TA_RetCode TA_DictDeletePair_S( TA_Dict *dict, const char *key )
{
   TA_PrivDictInfo *theDict;
   TA_String *stringToDelete;
   void      *valueToDelete;
   dnode_t   *node;
   TA_Libc   *libHandle;
   dict_t    *kazlibDict;

   theDict = (TA_PrivDictInfo *)dict;

   if( (theDict == NULL) || (key == NULL) )
      return TA_BAD_PARAM;
   kazlibDict = &theDict->d;
   libHandle  = theDict->libHandle;

   /* Find the key-value pair. */
   node = dict_lookup( libHandle, kazlibDict, key );

   if( node )
   {
      /* Free the 'node', the 'key' string and the 'value'. */
      stringToDelete = TA_StringFromChar( dnode_getkey(node) );
      valueToDelete  = dnode_get(node);
      dict_delete_free( libHandle, kazlibDict, node );
      TA_StringFree( TA_GetGlobalStringCache( libHandle ), stringToDelete );
      if( theDict->freeValueFunc )
         theDict->freeValueFunc( libHandle, valueToDelete );
   }
   else
      return TA_KEY_NOT_FOUND;

   return TA_SUCCESS;
}
Пример #4
0
/// Remove the specified PTX from the DB via its index.
/// @param[in] key      the key associated with a given PTX
/// return zero iff the node was found and successfully removed
static int
_shop_ptx_invalidate(shopping_state_s *ssp, CCS key, CCS msg, int ignored)
{
    dnode_t *dnp;
    CCS id;

    dnp = dict_lookup(ssp->ptx_dict, key);

    if (ignored && dnp) {
	id = (CCS)dnode_get(dnp);
	vb_printf(VB_WHY, "WOULD INVALIDATE %s (%s) due to '%s'", id, key, msg);
	return 0;
    }

    if (dnp) {
	id = (CCS)dnode_get(dnp);
	key = (CCS)dnode_getkey(dnp);
	vb_printf(VB_WHY, "PTX %s invalidated due to '%s'", id, msg);
	dict_delete_free(ssp->ptx_dict, dnp);
	putil_free(id);
	putil_free(key);
	return 0;
    } else {
	putil_warn("invalidated PTX %s twice", key);
	return 1;
    }
}
Пример #5
0
void ledger_close_topic(ledger_ctx *ctx, const char *name) {
    ledger_topic *topic = NULL;
    dnode_t *dtopic;

    dtopic = dict_lookup(&ctx->topics, name);
    if(dtopic != NULL) {
        topic = dnode_get(dtopic);
        ledger_topic_close(topic);
        dict_delete_free(&ctx->topics, dtopic);
    }
}
Пример #6
0
/// Free all data structures allocated by _shop_ptx_init().
static void
_shop_ptx_destroy(dict_t *dict)
{
    dnode_t *dnp, *next;

    if (dict) {
	for (dnp = dict_first(dict); dnp;) {
	    CCS key, id;

	    next = dict_next(dict, dnp);

	    key = (CCS)dnode_getkey(dnp);
	    id = (CCS)dnode_get(dnp);
	    dict_delete_free(dict, dnp);
	    putil_free(key);
	    putil_free(id);
	    dnp = next;
	}
	dict_destroy(dict);
    }
}
Пример #7
0
TA_RetCode TA_DictDeletePair_S2( TA_Dict *dict, const char *key1, const char *key2 )
{
   TA_PrivDictInfo *theDict;
   TA_String *stringToDelete;
   dnode_t   *node;
   TA_Dict   *subDict;
   TA_RetCode retCode;
   TA_Libc   *libHandle;
   dict_t    *kazlibDict;

   theDict = (TA_PrivDictInfo *)dict;

   if( (theDict == NULL)    ||
       (key1 == NULL)       || 
       (key2 == NULL))
      return TA_BAD_PARAM;
   kazlibDict = &theDict->d;
   libHandle  = theDict->libHandle;

   /* Find the dictionary for this 'key1'. */
   node = dict_lookup( libHandle, kazlibDict, key1 );

   if( !node )
      return TA_KEY_NOT_FOUND;

   subDict = (TA_Dict *)dnode_get(node);

   retCode = TA_DictDeletePair_S( subDict, key2 );

   /* Delete the dictionary if it is empty. */
   if( (retCode == TA_SUCCESS) && (TA_DictSize(subDict) == 0) )
   {
      TA_DictFree( subDict );
      /* Free the 'node' and the 'key1' string. */
      stringToDelete = TA_StringFromChar( dnode_getkey(node) );
      dict_delete_free( theDict->libHandle, kazlibDict, node );
      TA_StringFree( TA_GetGlobalStringCache( libHandle ), stringToDelete );
   }
   return TA_SUCCESS;
}
Пример #8
0
int main(int argc, char **args){
    if(argc != 3){
        fprintf(stderr,"%s RECV SEND\n",args[0]);
        exit(1);
    }
    signal(SIGINT,&call_for_stop);
    
    bstring pull_addr = bfromcstr(args[1]);
    bstring pub_addr  = bfromcstr(args[2]);

    mongrel2_ctx *ctx = mongrel2_init(1); // Yes for threads?

    mongrel2_socket *pull_socket = mongrel2_pull_socket(ctx);
    mongrel2_connect(pull_socket, bdata(pull_addr));

    pub_socket = mongrel2_pub_socket(ctx);
    mongrel2_connect(pub_socket, bdata(pub_addr));
mongrel2_set_identity(pub_socket, bdata(&SENDER) );

    mongrel2_request *request;

    // Polling is done to show how to do a clean shutdown
    int poll_response;
    zmq_pollitem_t socket_tracker;
    socket_tracker.socket = pull_socket->zmq_socket;
    socket_tracker.events = ZMQ_POLLIN;

    // Let's try out some ADT goodness
    dict_t* dict = dict_create(DICTCOUNT_T_MAX, compare_session);
    dict_set_allocator(dict, alloc_dict, free_dict, NULL);

    dnode_t* tempnode = NULL;
    m2_ws_session_data *counter = NULL;
    int retval = 0;
    while(shutdown != 1){
        poll_response = zmq_poll(&socket_tracker,1,500*1000);
        if(poll_response > 0){
            request = mongrel2_recv(pull_socket);
            fprintf(stdout,"got something...\n");

            if(request != NULL && mongrel2_request_for_disconnect(request) != 1){
                m2_ws_session_id* incoming = calloc(1,sizeof(m2_ws_session_id));
                incoming->req = request;
                printf("Looking at incoming->conn_id = %d\n",incoming->req->conn_id);
                tempnode = dict_lookup(dict,incoming);

                if(tempnode == NULL){
                    mongrel2_ws_reply_upgrade(request,pub_socket);
                    counter = calloc(1,sizeof(m2_ws_session_data));
                    counter->times_seen = 0;
                    retval = dict_alloc_insert(dict,incoming,counter);
                    assert(retval == 1);
                } else {
                    free(incoming);
                    counter = dnode_get(tempnode);
                    counter->times_seen += 1;
                }

                if(blength(request->body) > 0){
                    if(tempnode && mongrel2_ws_frame_get_fin(blength(request->body),
                                                             (uint8_t*)bdata(request->body))){
                        printf("Hey, it's a close\n");
                        dict_delete_free(dict,tempnode);
                        mongrel2_disconnect(pub_socket,request);
                    }
                } else {
                    bstring randmsg = genrandmsg();
                    mongrel2_ws_reply(pub_socket,request,randmsg);
                }
                

                printf("FYI: we've got %ld entries\n",dict_count(dict));
            } else {
                fprintf(stdout,"Connection %d disconnected\n", request->conn_id);
            }
        } else if (poll_response < 0){
            fprintf(stdout, "Error on poll!");
            shutdown = 1;
        }
    }
    // bstring msg = bformat("{\"msg\" : \"hi there %d\"}", request->conn_id);
    // fprintf(stdout,"Sending new msg: '%*s'",blength(msg),bdata(msg));
    // mongrel2_ws_reply(pub_socket,request,msg);
    // bdestroy(msg);
    // mongrel2_request_finalize(request);
    // mongrel2_reply(pub_socket,request,bfromcstr(""));
    
    bdestroy(pull_addr);
    bdestroy(pub_addr);

    mongrel2_close(pull_socket);
    mongrel2_close(pub_socket);
    mongrel2_deinit(ctx);
    fprintf(stdout,"\nClean shutdown done! Thanks for playing!\n");
    return 0;
}
Пример #9
0
int main(void)
{
    input_t in;
    dict_t darray[10];
    dict_t *d = &darray[0];
    dnode_t *dn;
    size_t i;
    char *tok1, *tok2, *val;
    const char *key;

    char *help =
        "a <key> <val>          add value to dictionary\n"
        "d <key>                delete value from dictionary\n"
        "l <key>                lookup value in dictionary\n"
        "( <key>                lookup lower bound\n"
        ") <key>                lookup upper bound\n"
        "< <key>                lookup strict lower bound\n"
        "> <key>                lookup strict upper bound\n"
        "# <num>                switch to alternate dictionary (0-9)\n"
        "j <num> <num>          merge two dictionaries\n"
        "f                      free the whole dictionary\n"
        "k                      allow duplicate keys\n"
        "c                      show number of entries\n"
        "t                      dump whole dictionary in sort order\n"
        "m                      make dictionary out of sorted items\n"
        "p                      turn prompt on\n"
        "s                      switch to non-functioning allocator\n"
        "q                      quit";

    for (i = 0; i < sizeof darray / sizeof *darray; i++)
        dict_init(&darray[i], DICTCOUNT_T_MAX, comparef);

    for (;;) {
        if (prompt)
            putchar('>');
        fflush(stdout);

        if (!fgets(in, sizeof(input_t), stdin))
            break;

        switch(in[0]) {
            case '?':
                puts(help);
                break;
            case 'a':
                if (tokenize(in+1, &tok1, &tok2, (char **) 0) != 2) {
                    puts("what?");
                    break;
                }
                key = dupstring(tok1);
                val = dupstring(tok2);

                if (!key || !val) {
                    puts("out of memory");
                    free((void *) key);
                    free(val);
                }

                if (!dict_alloc_insert(d, key, val)) {
                    puts("dict_alloc_insert failed");
                    free((void *) key);
                    free(val);
                    break;
                }
                break;
            case 'd':
                if (tokenize(in+1, &tok1, (char **) 0) != 1) {
                    puts("what?");
                    break;
                }
                dn = dict_lookup(d, tok1);
                if (!dn) {
                    puts("dict_lookup failed");
                    break;
                }
                val = (char *) dnode_get(dn);
                key = (char *) dnode_getkey(dn);
                dict_delete_free(d, dn);

                free(val);
                free((void *) key);
                break;
            case 'f':
                dict_free_nodes(d);
                break;
            case 'l':
            case '(':
            case ')':
            case '<':
            case '>':
                if (tokenize(in+1, &tok1, (char **) 0) != 1) {
                    puts("what?");
                    break;
                }
                dn = 0;
                switch (in[0]) {
                case 'l':
                    dn = dict_lookup(d, tok1);
                    break;
                case '(':
                    dn = dict_lower_bound(d, tok1);
                    break;
                case ')':
                    dn = dict_upper_bound(d, tok1);
                    break;
                case '<':
                    dn = dict_strict_lower_bound(d, tok1);
                    break;
                case '>':
                    dn = dict_strict_upper_bound(d, tok1);
                    break;
                }
                if (!dn) {
                    puts("lookup failed");
                    break;
                }
                val = (char *) dnode_get(dn);
                puts(val);
                break;
            case 'm':
                construct(d);
                break;
            case 'k':
                dict_allow_dupes(d);
                break;
            case 'c':
                printf("%lu\n", (unsigned long) dict_count(d));
                break;
            case 't':
                for (dn = dict_first(d); dn; dn = dict_next(d, dn)) {
                    printf("%s\t%s\n", (char *) dnode_getkey(dn),
                            (char *) dnode_get(dn));
                }
                break;
            case 'q':
                exit(0);
                break;
            case '\0':
                break;
            case 'p':
                prompt = 1;
                break;
            case 's':
                dict_set_allocator(d, new_node, del_node, NULL);
                break;
            case '#':
                if (tokenize(in+1, &tok1, (char **) 0) != 1) {
                    puts("what?");
                    break;
                } else {
                    int dictnum = atoi(tok1);
                    if (dictnum < 0 || dictnum > 9) {
                        puts("invalid number");
                        break;
                    }
                    d = &darray[dictnum];
                }
                break;
            case 'j':
                if (tokenize(in+1, &tok1, &tok2, (char **) 0) != 2) {
                    puts("what?");
                    break;
                } else {
                    int dict1 = atoi(tok1), dict2 = atoi(tok2);
                    if (dict1 < 0 || dict1 > 9 || dict2 < 0 || dict2 > 9) {
                        puts("invalid number");
                        break;
                    }
                    dict_merge(&darray[dict1], &darray[dict2]);
                }
                break;
            default:
                putchar('?');
                putchar('\n');
                break;
        }
    }

    return 0;
}