SFBNet.h

Go to the documentation of this file.
00001 /*                                             -*- mode:C++; fill-column:100 -*-
00002   SFBNet.h - Simple 'functional gradient' network routing
00003   Copyright (C) 2010 The Regents of the University of New Mexico.  All rights reserved.
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014 
00015   You should have received a copy of the GNU General Public License
00016   along with this library; if not, write to the Free Software
00017   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
00018   USA
00019 */
00020 
00027 #ifndef SFBNET_H_
00028 #define SFBNET_H_
00029 
00030 #include <stdarg.h>
00031 
00032 #include "SFBConstants.h"       /* For FACE_COUNT, etc */
00033 #include "SFBAlarm.h"           /* For SFBAlarmIndexType */ 
00034 #include "SFBPrint.h"           /* For FacePrinter, etc */ 
00035 
00044 class SFBNet {
00045 public:
00046 
00055   bool shouldTakeFirstTie() { return flags&TAKE_FIRST_TIE; }
00056 
00064   void setShouldTakeFirstTie(bool on) { 
00065     if (on)
00066       flags |= TAKE_FIRST_TIE;
00067     else
00068       flags &= ~TAKE_FIRST_TIE;
00069   }
00070 
00075   void begin() ;
00076 
00080   bool isActive() { return (flags&NET_ACTIVE)!=0; } 
00081 
00085   void end() ;
00086 
00096   void offer(u8 service) ;
00097 
00105   bool withhold(u8 service) ;
00106 
00116   bool isOfferedByNode (u8 service) ;
00117 
00128   bool isOfferedByNet(u8 service) ;
00129 
00136   void openServiceStream(u8 service) ;
00137 
00142   void printf ( const char * fmt, ...) ;
00143 
00148   void vprintf(const char * fmt, va_list & ap) ;
00149 
00153   void printf(u8 service, const char * fmt, ...) ;
00154 
00162   void setBroadcastPeriod(u32 millis) ;
00163 
00171   void setHorizon(u32 distance) ;
00172 
00179   u32 getHorizon() { return defaultTTL; }
00180 
00184   u32 getServiceStreamFace() {
00185     return net_printer_face;
00186   }
00187 
00191   bool neighborActive(u8 face) ;
00192 
00198   enum Sizes {
00199     MAX_TOTAL_SVCS=64,  
00200     MAX_LOCAL_SVCS=32,  
00201     MAX_RETURN_ROUTE_LENGTH=100 
00202   };
00203 
00204   void printPath(u8 toFace, bool skipFirst = true);
00205 
00209   void dump(u8 face, const u8 * prefix = 0) ;
00210 
00211 private:
00212 
00220   enum Flags {
00221     TAKE_FIRST_TIE=      0x01, 
00222     RESPONSE_SENT_HEADER=0x02, 
00223     NET_ACTIVE=          0x04, 
00224     IN_TRIGGER=          0x08, 
00225     FLAG_RSRV4=          0x10,
00226     FLAG_RSRV5=          0x20,
00227     FLAG_RSRV6=          0x40,
00228     FLAG_RSRV7=          0x80
00229   };
00230 
00234   u8 ticks() { return MsToTicks(millis()); }
00235 
00236   u32 ticksToMs(u8 ticks) { return ticks<<8; }
00237 
00238   u32 MsToTicks32(u32 ms) { return ms>>8; }
00239 
00240   u8 MsToTicks(u32 ms) { return (u8) MsToTicks32(ms); }
00241 
00242 
00246   bool neighborActive_(u8 face) {
00247     u8 now = ticks();
00248     bool ret = IS_LATER8(neighborActiveTicks[face]+MsToTicks(2048),now);
00249     if (!ret) neighborActiveTicks[face] = now-10; // Pull along inactive faces to avoid wrapping
00250     return ret;
00251   }
00252 
00253   static const FacePrinter net_printer;
00254   static const FacePrinter response_printer;
00255 
00256   void functionPrint(u8 face, u8 byte) ;
00257   void functionPrintln(u8 face) ;
00258 
00259   void printResponseHeader() ;
00260   void printResponseHeaderIfNeeded() { if ((flags&RESPONSE_SENT_HEADER)==0) printResponseHeader(); }
00261   void responsePrint(u8 face, u8 byte) ;
00262   void responsePrintln (u8 face) ;
00263 
00264   static void NetBcastAlarm(u32) ;
00265   static void NetPacketHandler(u8 *) ;
00266 
00267   static void NetFunctionPrint(u8 face, u8 byte ) ;
00268   static void NetFunctionPrintln( u8 face) ;
00269   static void NetResponsePrint(u8 face, u8 byte) ;
00270   static void NetResponsePrintln(u8 face) ;
00271 
00275   void broadcastAlarm(u32) ;
00276 
00280   void packetHandler(u8 *) ;
00281 
00282   struct Entry {
00283     u8 func;                    /* Function. */
00284     u8 last_update[FACE_COUNT]; /* Bcast ID of last update. */
00285     u8 hops[FACE_COUNT];        /* Number of hops to provider. */
00286   };
00287 
00288   /* Pointer to the start of the temp packet in packet_buffer. */
00289   u8 * net_packet;
00290 
00291   Entry routes[MAX_TOTAL_SVCS];
00292 
00293   SFBAlarmIndexType alarmNumber; /* My alarm number */ 
00294 
00295   u8 last_route;
00296 
00297   u8 offered[MAX_LOCAL_SVCS];
00298   u8 last_offered;
00299 
00300   u8 bcastPeriodTicks;
00301   u8 ticksInGeneration;
00302 
00303   u8 defaultTTL;
00304 
00305   u8 neighborActiveTicks[FACE_COUNT];
00306   u8 bcastGeneration;
00307 
00308   u8 flags;
00309 
00310   /* Temporary packet buffer. */
00311   u8 packet_buffer[PACKET_PRINTER_PACKET_BUFFER_SIZE];
00312 
00313   /* Currently "open" function. */
00314   u8 open_function;
00315 
00316   u8 virtual_face;
00317 
00318   u8 packet_printer_face;
00319 
00320   /* Face for the virtual printer for the SFBNet. Dispatches to
00321    * u8_packet_printer_face. */
00322   u8 net_printer_face;
00323 
00324   /* The route back to the invoker. */
00325   u8 response_route[MAX_RETURN_ROUTE_LENGTH];
00326 
00327   /* The length of the route back to the invoker. */
00328   s8 response_route_len;
00329 
00330   /* The first face in the route back to the invoker. */
00331   u8 response_src;
00332 
00337   void ship() ;
00338 
00339   void handleBroadcast(u8 *) ;
00340   void handleInvoke(u8 *) ;
00341 
00342   Entry * lookup(u8 func, bool insert = false) ;
00343 
00344   u8 pickDirection(u8 func, u8 origin) ;
00345 
00346   u8 pickRandomFace(u8 func) ;
00347 
00348   void trigger(u8 * packet, u8 * return_route, u8 hops) ;
00349 
00350 };
00351 
00355 extern SFBNet Net;
00356 
00357 #endif /* SFBNET_H_ */

Generated on Fri Apr 22 06:54:11 2011 for SFB by doxygen 1.5.9