Peerd.c

From Organic Design wiki
Revision as of 01:41, 26 July 2007 by Nad (talk | contribs)

// http://www.organicdesign.co.nz/peerd - nodal p2p wiki daemonTemplate:C

See also

// Source: http://www.organicdesign.co.nz/peerd/files/C - the C language version for *ix,OSX,NT platforms // This article and all its includes are licenced under http://www.gnu.org/copyleft/lesser.html // Compiled in Win32 by peerd.c/win32-makefile to http://www.organicdesign.co.nz/wiki/images/a/a2/Peerd.msi // Compiled in Linux and OSX by rp to peerd.deb and peerd.dmg

// Globals int this = 0,parent,grandpa,port = 80; char *peer = "default"; char *file = (char*)0;

// Reserved nodes used by nodal reduction process (tmp)

  1. define nodeTRUE 1 // [[nodeTRUE]]
  2. define nodeNEXT 1 // [[nodeNEXT]]
  3. define nodePREV 2 // [[nodePREV]]
  4. define nodeCODE 3 // [[nodeCODE]] is a [[pseudo node-value]] to determine is the nodes non-nodal-data is a function-ref
  5. define nodePARENT 4 // [[nodePARENT]] updated by [[nodal reduction]], the node for which thsi is the [[focus]]
  6. define nodeTRIE 5 // [[nodeTRIE]] use a different root for trie/hash so it can't interfere with serialisation of nodal-root
  7. define nodeFIRST 6 // [[nodeFIRST]]
  8. define nodeLAST 7 // [[nodeLAST]]
  9. define nodeMAKE 8 // [[nodeMAKE]]
  10. define nodeINIT 9 // [[nodeINIT]]
  11. define nodeMAIN 10 // [[nodeMAIN]]
  12. define nodeEXIT 11 // [[nodeEXIT]]


  1. include <unistd.h>
  2. include <stdlib.h>
  3. include <stdio.h>
  4. include <string.h>
  5. include <errno.h>
  6. include <math.h>
  7. include <stdarg.h>
  8. include <time.h>
  9. include <regex.h>
  10. include "util.h" // [[util.c]]: General utils, file,log,string etc

// List & Node Space

  1. include "listSpace.h" // [[listSpace.c]]: listSpace and some C-specific extras: hash, trie, linked-list
  2. include "nodeSpace.h" // [[nodeSpace.c]]: NodeSpace function declarations and initialisation
  3. include "serialise.h" // [[serialise.c]]: Nodal-to-text and visa-versa

// Interface related

  1. include "SDL.h"
  2. include "SDL_image.h"
  3. include "SDL_ttf.h"

//#include "SDL_opengl.h" // OpenGL example [[[[1]]]]

  1. include "interface.h" // [[interface.c]]: Layer model, video, audio, input, OpenGL

// Peer daemon setup

  1. if __WIN32__
  2. include "servicate.h" // [[servicate.c]]
  3. elif __APPLE__
  4. include "launchd.h" // [[launchd.c]]
  5. elif __unix__
  6. include "daemonise.h" // [[daemonise.c]]
  7. endif

// [[Communications]] related

  1. if __WIN32__
  2. include <winsock.h>
  3. else
  4. include <sys/socket.h> // see [[[[2]]]]
  5. include <sys/select.h>
  6. include <netinet/in.h>
  7. include <sys/time.h> // for select() related stuff
  8. include <fcntl.h> // O_RDWR, O_NONBLOCK, F_GETFL, F_SETFL
  9. include <netdb.h> // for [[[struct]]]
  10. endif
  11. include "io.h" // [[io.c]]: Main server and stream setup and functions

int main(int argc, char **argv) {

logAdd(""); // Blank line in log to separate peerd sessions peerdInit(); // Set up as a daemon or service listInit(); // Initialise list-space and hash/trie functions nodeInit(); // Set up initial nodal structure for reduction loop args(argc,argv); // Handle command-line args and globals like peer and port ifInit(); // Initialise interface aspects (video, audio, input, OpenGL) ioInit(); // Set up listening socket on specified port

// Main [[nodal reduction]] loop // - maintains [[this]], [[parent]] and [[grandpa]] globals logAdd("Handing program execution over to nodal reduction..."); while(1) nodeReduce();

}