Hi Shailander,

Maybe you can try to use APIs in tclHash.c 
(ns-allinone-2.33/tcl8.4.18/generic)
to implement your hash operation.

Here is a example code, (I hope it will be useful but WITHOUT ANY WARRANTY
since I have not tested it  yet!)

/************** h file *******************/

#ifndef NS_EVENT_PACKET_HASH_H
#define NS_EVENT_PACKET_HASH_H

#include "config.h"
#include "tclcl.h"
#include "cmu-trace.h"

class Event_Packet_Hash_Entry {
 public:
        // key variable
  nsaddr_t              source_;
  u_int16_t     evt_id_;

  // addtional variable
  u_int16_t             evt_index_;
  int                                   evt_ttl_;
  bool                          evt_grs_;

  Event_Packet_Hash_Entry() {
    source_ = (nsaddr_t)0;
    evt_id_ = 0;
    evt_index_ = 0;
    evt_ttl_ = 0;
    evt_grs_ = false;
  }

  ~Event_Packet_Hash_Entry() { }

};


class Event_Packet_Hash_Table {
 public:
  Tcl_HashTable htable;

  Event_Packet_Hash_Table() {
    Tcl_InitHashTable(&htable, 2);
  }

  void ResetHash();
  void PutinHash(nsaddr_t source, u_int16_t evt_id, u_int16_t evt_index, int 
evt_ttl);
  void ModifyHash(nsaddr_t source, u_int16_t evt_id, u_int16_t evt_index, 
int evt_ttl, bool evt_grs);
  Event_Packet_Hash_Entry *GetHash(nsaddr_t source, u_int16_t evt_id);
};

#endif  //NS_EVENT_PACKET_HASH_H


/************** cc file ******************/
#include "tclcl.h"
#include "event_packet_hash.h"

Event_Packet_Hash_Entry *Event_Packet_Hash_Table::GetHash(nsaddr_t source, 
u_int16_t evt_id)
{
  u_int32_t key[2];

  key[0] = source;
  key[1] = (u_int32_t) evt_id;
  // for dulplication check

  Tcl_HashEntry *entryPtr = Tcl_FindHashEntry(&htable, (char *)key);

  if (entryPtr == NULL ) {
        //printf("Hash Null\n");
         return NULL;  }
        //printf("Hash Get\n");

  return (Event_Packet_Hash_Entry *)Tcl_GetHashValue(entryPtr);
}

void Event_Packet_Hash_Table::ModifyHash(nsaddr_t source, u_int16_t evt_id, 
u_int16_t evt_index, int evt_ttl, bool evt_grs)
{
        unsigned int key[2];
        Event_Packet_Hash_Entry *hashPtr;
        int newPtr;

        key[0] = source;
        key[1] = (u_int32_t) evt_id;
        Tcl_HashEntry *entryPtr = Tcl_FindHashEntry(&htable, (char *)key);

        if (entryPtr == NULL ) {
                        return;         }

        // Get Hash Entry
        hashPtr = (Event_Packet_Hash_Entry *)Tcl_GetHashValue(entryPtr);
        //old_num_dup = hashPtr->hop_count_;

        // Delete Hash Entry
        delete hashPtr;
        // Delete Hash Entry
        Tcl_DeleteHashEntry(entryPtr);


                // Re-create Hash Entry
                entryPtr = Tcl_CreateHashEntry(&htable, (char *)key, &newPtr);
                if (!newPtr) {      return;
    }

    hashPtr = new Event_Packet_Hash_Entry;
    hashPtr->source_ = source;
    hashPtr->evt_id_ = evt_id;
    hashPtr->evt_index_ = evt_index;
    hashPtr->evt_ttl_ = evt_ttl;
    hashPtr->evt_grs_ = evt_grs;

                // binding Hash entry & Table Entry
    Tcl_SetHashValue(entryPtr, hashPtr);

}

void Event_Packet_Hash_Table::PutinHash(nsaddr_t source, u_int16_t evt_id, 
u_int16_t evt_index, int evt_ttl)
{
    Tcl_HashEntry *entryPtr;
    Event_Packet_Hash_Entry    *hashPtr;
    unsigned int key[2];
    int newPtr;


    key[0] = source;
    key[1] = (u_int32_t) evt_id;

    entryPtr = Tcl_CreateHashEntry(&htable, (char *)key, &newPtr);

    if (!newPtr)
      return;

    hashPtr = new Event_Packet_Hash_Entry;
    hashPtr->source_ = source;
    hashPtr->evt_id_ = evt_id;
    hashPtr->evt_index_ = evt_index;
    hashPtr->evt_ttl_ = evt_ttl;
    hashPtr->evt_grs_ = false;

    Tcl_SetHashValue(entryPtr, hashPtr);
    //printf("Put in Hash: Source Addr:%ld\tNum:%d\n",source_addr,pkt_num);
}


//--- Hash Cleaning --------------//
void Event_Packet_Hash_Table::ResetHash()
{
  Event_Packet_Hash_Entry *hashPtr;
  Tcl_HashEntry *entryPtr;
  Tcl_HashSearch searchPtr;

  entryPtr = Tcl_FirstHashEntry(&htable, &searchPtr);
  while (entryPtr != NULL) {
    hashPtr = (Event_Packet_Hash_Entry *)Tcl_GetHashValue(entryPtr);
    delete hashPtr;
    Tcl_DeleteHashEntry(entryPtr);
    entryPtr = Tcl_NextHashEntry(&searchPtr);
  }
}

Regards,
Phenix

--------------------------------------------------
From: "Shailander Srinivasa V" <[email protected]>
Sent: Saturday, December 20, 2008 7:21 PM
To: <[email protected]>
Subject: [ns] DHT implementation in NS-2

>
> Hi All,
>
> I am trying to implement DHT(Dynamic Hash
> Table) in NS-2 as a part of Location-Aware-Routing Protocol for
> MANETS. I am unable to find any DHT implementations in NS-2.
> Can anyone please guide me on how to approach towards the implementation.
> Thanking You in Advance
>
> Thanks with Regards,
> Shailander
>
>
>      Add more friends to your messenger and enjoy! Go to 
> http://messenger.yahoo.com/invite/ 


Reply via email to