# 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
    00207   void dump(u8 face, const u8 * prefix = 0) ;
    00208
    00209 private:
    00210
    00218   enum Flags {
    00219     TAKE_FIRST_TIE=      0x01,
    00220     RESPONSE_SENT_HEADER=0x02,
    00221     NET_ACTIVE=          0x04,
    00222     FLAG_RSRV3=          0x08,
    00223     FLAG_RSRV4=          0x10,
    00224     FLAG_RSRV5=          0x20,
    00225     FLAG_RSRV6=          0x40,
    00226     FLAG_RSRV7=          0x80
    00227   };
    00228
    00232   u8 ticks() { return MsToTicks(millis()); }
    00233
    00234   u32 ticksToMs(u8 ticks) { return ticks<<8; }
    00235
    00236   u32 MsToTicks32(u32 ms) { return ms>>8; }
    00237
    00238   u8 MsToTicks(u32 ms) { return (u8) MsToTicks32(ms); }
    00239
    00240
    00244   bool neighborActive_(u8 face) {
    00245     u8 now = ticks();
    00246     bool ret = IS_LATER8(neighborActiveTicks[face]+MsToTicks(2048),now);
    00247     if (!ret) neighborActiveTicks[face] = now-10; // Pull along inactive faces to avoid wrapping
    00248     return ret;
    00249   }
    00250
    00251   static const FacePrinter net_printer;
    00252   static const FacePrinter response_printer;
    00253
    00254   void functionPrint(u8 face, u8 byte) ;
    00255   void functionPrintln(u8 face) ;
    00256
    00257   void printResponseHeader() ;
    00258   void printResponseHeaderIfNeeded() { if ((flags&RESPONSE_SENT_HEADER)==0) printResponseHeader(); }
    00259   void responsePrint(u8 face, u8 byte) ;
    00260   void responsePrintln (u8 face) ;
    00261
    00262   static void NetBcastAlarm(u32) ;
    00263   static void NetPacketHandler(u8 *) ;
    00264
    00265   static void NetFunctionPrint(u8 face, u8 byte ) ;
    00266   static void NetFunctionPrintln( u8 face) ;
    00267   static void NetResponsePrint(u8 face, u8 byte) ;
    00268   static void NetResponsePrintln(u8 face) ;
    00269
    00273   void broadcastAlarm(u32) ;
    00274
    00278   void packetHandler(u8 *) ;
    00279
    00280   struct Entry {
    00281     u8 func;                    /* Function. */
    00282     u8 last_update[FACE_COUNT]; /* Bcast ID of last update. */
    00283     u8 hops[FACE_COUNT];        /* Number of hops to provider. */
    00284   };
    00285
    00286   /* Pointer to the start of the temp packet in packet_buffer. */
    00287   u8 * net_packet;
    00288
    00289   Entry routes[MAX_TOTAL_SVCS];
    00290
    00291   SFBAlarmIndexType alarmNumber; /* My alarm number */
    00292
    00293   u8 last_route;
    00294
    00295   u8 offered[MAX_LOCAL_SVCS];
    00296   u8 last_offered;
    00297
    00298   u8 bcastPeriodTicks;
    00299   u8 ticksInGeneration;
    00300
    00301   u8 defaultTTL;
    00302
    00303   u8 neighborActiveTicks[FACE_COUNT];
    00304   u8 bcastGeneration;
    00305
    00306   u8 flags;
    00307
    00308   /* Temporary packet buffer. */
    00309   u8 packet_buffer[PACKET_PRINTER_PACKET_BUFFER_SIZE];
    00310
    00311   /* Currently "open" function. */
    00312   u8 open_function;
    00313
    00314   u8 virtual_face;
    00315
    00316   u8 packet_printer_face;
    00317
    00318   /* Face for the virtual printer for the SFBNet. Dispatches to
    00319    * u8_packet_printer_face. */
    00320   u8 net_printer_face;
    00321
    00322   /* The route back to the invoker. */
    00323   u8 response_route[MAX_RETURN_ROUTE_LENGTH];
    00324
    00325   /* The length of the route back to the invoker. */
    00326   s8 response_route_len;
    00327
    00328   /* The first face in the route back to the invoker. */
    00329   u8 response_src;
    00330
    00335   void ship() ;
    00336
    00337   void handleBroadcast(u8 *) ;
    00338   void handleInvoke(u8 *) ;
    00339
    00340   Entry * lookup(u8 func, bool insert = false) ;
    00341
    00342   u8 pickDirection(u8 func, u8 origin) ;
    00343
    00344   u8 pickRandomFace(u8 func) ;
    00345
    00346   void trigger(u8 * packet, u8 * return_route, u8 hops) ;
    00347
    00348 };
    00349
    00353 extern SFBNet Net;
    00354
    00355 #endif /* SFBNET_H_ */