p802_15_4hlist.cc

Go to the documentation of this file.
00001 /********************************************/
00002 /*     NS2 Simulator for IEEE 802.15.4      */
00003 /*           (per P802.15.4/D18)            */
00004 /*------------------------------------------*/
00005 /* by:        Jianliang Zheng               */
00006 /*        (zheng@ee.ccny.cuny.edu)          */
00007 /*              Myung J. Lee                */
00008 /*          (lee@ccny.cuny.edu)             */
00009 /*        ~~~~~~~~~~~~~~~~~~~~~~~~~         */
00010 /*           SAIT-CUNY Joint Lab            */
00011 /********************************************/
00012 
00013 // File:  p802_15_4hlist.cc
00014 // Mode:  C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t
00015 
00016 // $Header: /nfs/jade/vint/CVSROOT/ns-2/wpan/p802_15_4hlist.cc,v 1.2 2005/07/13 03:51:33 tomh Exp $
00017 
00018 /*
00019  * Copyright (c) 2003-2004 Samsung Advanced Institute of Technology and
00020  * The City University of New York. All rights reserved.
00021  *
00022  * Redistribution and use in source and binary forms, with or without
00023  * modification, are permitted provided that the following conditions
00024  * are met:
00025  * 1. Redistributions of source code must retain the above copyright
00026  *    notice, this list of conditions and the following disclaimer.
00027  * 2. Redistributions in binary form must reproduce the above copyright
00028  *    notice, this list of conditions and the following disclaimer in the
00029  *    documentation and/or other materials provided with the distribution.
00030  * 3. All advertising materials mentioning features or use of this software
00031  *    must display the following acknowledgement:
00032  *  This product includes software developed by the Joint Lab of Samsung 
00033  *      Advanced Institute of Technology and The City University of New York.
00034  * 4. Neither the name of Samsung Advanced Institute of Technology nor of 
00035  *    The City University of New York may be used to endorse or promote 
00036  *    products derived from this software without specific prior written 
00037  *    permission.
00038  *
00039  * THIS SOFTWARE IS PROVIDED BY THE JOINT LAB OF SAMSUNG ADVANCED INSTITUTE
00040  * OF TECHNOLOGY AND THE CITY UNIVERSITY OF NEW YORK ``AS IS'' AND ANY EXPRESS 
00041  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00042  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 
00043  * NO EVENT SHALL SAMSUNG ADVANCED INSTITUTE OR THE CITY UNIVERSITY OF NEW YORK 
00044  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00045  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00046  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00047  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00048  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00049  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00050  */
00051 
00052 
00053 #include <scheduler.h>
00054 #include "p802_15_4hlist.h"
00055 
00056 
00057 int addHListLink(HLISTLINK **hlistLink1, HLISTLINK **hlistLink2, UINT_16 hostid, UINT_8 sn)
00058 {
00059     HLISTLINK *tmp;
00060     if(*hlistLink2 == NULL)     //not exist yet
00061     {
00062         *hlistLink2 = new HLISTLINK(hostid, sn);
00063         if (*hlistLink2 == NULL) return 1;
00064         *hlistLink1 = *hlistLink2;
00065     }
00066     else
00067     {
00068         tmp=new HLISTLINK(hostid, sn);
00069         if (tmp == NULL) return 1;
00070         tmp->last = *hlistLink2;
00071         (*hlistLink2)->next = tmp;
00072         *hlistLink2 = tmp;
00073     }
00074     return 0;
00075 }
00076 
00077 int updateHListLink(int oper, HLISTLINK **hlistLink1, HLISTLINK **hlistLink2, UINT_16 hostid, UINT_8 sn)
00078 {
00079     HLISTLINK *tmp;
00080     int ok;
00081 
00082     ok = 1;
00083 
00084     tmp = *hlistLink1;
00085     while(tmp != NULL)
00086     {
00087         if(tmp->hostID == hostid)
00088         {
00089             if (oper == hl_oper_del)    //delete an element
00090             {
00091                 if(tmp->last != NULL)
00092                 {
00093                     tmp->last->next = tmp->next;
00094                     if(tmp->next != NULL)
00095                         tmp->next->last = tmp->last;
00096                     else
00097                         *hlistLink2 = tmp->last;
00098                 }
00099                 else if (tmp->next != NULL)
00100                 {
00101                     *hlistLink1 = tmp->next;
00102                     tmp->next->last = NULL;
00103                 }
00104                 else
00105                 {
00106                     *hlistLink1 = NULL;
00107                     *hlistLink2 = NULL;
00108                 }
00109                 delete tmp;
00110             }
00111             if (oper == hl_oper_rpl)    //replace
00112             {
00113                 if (tmp->SN != sn)
00114                     tmp->SN = sn;
00115                 else
00116                 {
00117                     ok = 2;
00118                     break;
00119                 }
00120             }
00121             ok = 0;
00122             break;
00123         }
00124         tmp = tmp->next;
00125     }
00126     return ok;
00127 }
00128 
00129 int chkAddUpdHListLink(HLISTLINK **hlistLink1, HLISTLINK **hlistLink2, UINT_16 hostid, UINT_8 sn)
00130 {
00131     int i;
00132 
00133     i = updateHListLink(hl_oper_rpl, hlistLink1, hlistLink2, hostid, sn);
00134     if (i == 0) return 1;
00135     else if (i == 2) return 2;
00136 
00137     i = addHListLink(hlistLink1, hlistLink2, hostid, sn);
00138     if (i == 0) return 0;
00139     else return 3;
00140 }
00141 
00142 void emptyHListLink(HLISTLINK **hlistLink1, HLISTLINK **hlistLink2)
00143 {
00144     HLISTLINK *tmp, *tmp2;
00145 
00146     if(*hlistLink1 != NULL)
00147     {
00148         tmp = *hlistLink1;
00149         while(tmp != NULL)
00150         {
00151             tmp2 = tmp;
00152             tmp = tmp->next;
00153             delete tmp2;
00154         }
00155         *hlistLink1 = NULL;
00156     }
00157     *hlistLink2 = *hlistLink1;
00158 }
00159 
00160 void dumpHListLink(HLISTLINK *hlistLink1, UINT_16 hostid)
00161 {
00162     HLISTLINK *tmp;
00163     int i;
00164 
00165     fprintf(stdout, "[%.2f] --- dump host list (by host %d) ---\n", Scheduler::instance().clock(), hostid);
00166     tmp = hlistLink1;
00167     i = 1;
00168     while(tmp != NULL)
00169     {
00170         fprintf(stdout, "\t%d:\tfrom host %d:\tSN = %d\n", i, tmp->hostID, tmp->SN);
00171         tmp = tmp->next;
00172         i++;
00173     }
00174 }
00175 
00176 // End of file: p802_15_4hlist.cc

Generated on Tue Mar 6 16:47:48 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6