Difference between revisions of "Peerd.c"

From Organic Design wiki
(log startup msg)
m
 
(181 intermediate revisions by 3 users not shown)
Line 1: Line 1:
// This article and all its includes are licenced under LGPL
+
{{legacy}}
// GPL: http://www.gnu.org/copyleft/lesser.html
+
<source lang="c">
// SRC: http://www.organicdesign.co.nz/husk.c
+
// http://www.organicdesign.co.nz/peerd - nodal p2p wiki daemon
 +
// 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)
 +
#define nodeTRUE  1  // [[[[nodeTRUE]]]]
 +
#define nodeNEXT  1  // [[[[nodeNEXT]]]]
 +
#define nodePREV  2  // [[[[nodePREV]]]]
 +
#define nodeCODE  3  // [[[[nodeCODE]]]] is a [[[[association|pseudo node-value]]]] to determine is the nodes non-nodal-data is a function-ref
 +
#define nodePARENT 4  // [[[[nodePARENT]]]] updated by [[[[nodal reduction]]]], the node for which thsi is the [[[[focus]]]]
 +
#define nodeTRIE  5  // [[[[nodeTRIE]]]] use a different root for trie/hash so it can't interfere with serialisation of nodal-root
 +
#define nodeFIRST  6  // [[[[nodeFIRST]]]]
 +
#define nodeLAST  7  // [[[[nodeLAST]]]]
 +
#define nodeMAKE  8  // [[[[nodeMAKE]]]]
 +
#define nodeINIT  9  // [[[[nodeINIT]]]]
 +
#define nodeMAIN  10 // [[[[nodeMAIN]]]]
 +
#define nodeEXIT  11 // [[[[nodeEXIT]]]]
 +
 
  
 
#include <unistd.h>
 
#include <unistd.h>
Line 7: Line 31:
 
#include <stdio.h>
 
#include <stdio.h>
 
#include <string.h>
 
#include <string.h>
 +
#include <errno.h>
 +
#include <math.h>
 +
#include <stdarg.h>
 +
#include <time.h>
 +
#include <regex.h>
 +
#include "util.h"          // [[[[util.c]]]]: General utils, file,log,string etc
  
int err = 0;
+
// List & Node Space
int main(int argc, char **argv) {
+
#include "listSpace.h"      // [[[[listSpace.c]]]]: listSpace and some C-specific extras: hash, trie, linked-list
 +
#include "nodeSpace.h"      // [[[[nodeSpace.c]]]]: NodeSpace function declarations and initialisation
 +
#include "serialise.h"      // [[[[serialise.c]]]]: Nodal-to-text and visa-versa
 +
 
 +
// Interface related
 +
#include "SDL.h"
 +
#include "SDL_image.h"
 +
#include "SDL_ttf.h"
 +
//#include "SDL_opengl.h"    // OpenGL example [[[[http://www.libsdl.org/cgi/docwiki.cgi/OpenGL_20Full_20Example|here]]]]
 +
#include "interface.h"      // [[[[interface.c]]]]: Layer model, video, audio, input, OpenGL
 +
 
 +
// Peer daemon setup
 +
#if __WIN32__
 +
#include "servicate.h"      // [[[[servicate.c]]]]
 +
#elif __APPLE__
 +
#include "launchd.h"        // [[[[launchd.c]]]]
 +
#elif __unix__
 +
#include "daemonise.h"      // [[[[daemonise.c]]]]
 +
#endif
  
#include "util.c"       // General utils, file,log,string etc
+
// [[[[Communications]]]] related
 +
#if __WIN32__
 +
#include <winsock.h>
 +
#else
 +
#include <sys/socket.h>    // see [[[[http://www.opengroup.org/onlinepubs/009695399/functions/select.html|select()]]]]
 +
#include <sys/select.h>
 +
#include <netinet/in.h>
 +
#include <sys/time.h>       // for select() related stuff
 +
#include <fcntl.h>          // O_RDWR, O_NONBLOCK, F_GETFL, F_SETFL
 +
#include <netdb.h>          // for [[[[http://www.opengroup.org/onlinepubs/009695399/basedefs/netdb.h.html|hostent struct]]]]
 +
#endif
 +
#include "io.h"            // [[[[io.c]]]]: Main server and stream setup and functions
  
#ifdef WINDOWS          // Set up as a daemon or service
+
int main(int argc, char **argv) {
#include "servicate.c"
 
#else
 
#include "daemonise.c"
 
#endif
 
  
#include "listSpace.c" // listSpace and some C-specific extras: hash, trie, linked-list
+
logAdd("");            // Blank line in log to separate peerd sessions
#include "args.c"      // Handle command-line args and globals like peer and port
+
peerdInit();            // Set up as a daemon or service
#include "nodeSpace.c"  // NodeSpace function declarations and initialisation
+
listInit();            // Initialise list-space and hash/trie functions
#include "serialise.c"  // Nodal-to-text and visa-versa
+
nodeInit();            // Set up initial nodal structure for reduction loop
#include "interface.c"  // Layer model (currently bitmap/imagemap-based)
+
args(argc,argv);        // Handle command-line args and globals like peer and port
#include "server.c"    // Set up listening socket and declare serverIterate
+
ifInit();              // Initialise interface aspects (video, audio, input, OpenGL)
#include "nodalHusk.c"  // Build initial nodal environment
+
ioInit();              // Set up listening socket on specified port
  
if (err == 0) logAdd(sprintf(malloc(100),"\nDaemon \"%s\" started successfully and serving on port %d.",peer,port));
+
// Main [[[[nodal reduction]]]] loop
logAdd("Entering nodal reduction...\n");
+
// - maintains [[[[this]]]], [[[[parent]]]] and [[[[grandpa]]]] globals
while(nodeReduce(this=ROOT));
+
logAdd("Handing program execution over to nodal reduction...");
free(space);
+
while(1) nodeReduce();
  
 
}
 
}
 +
</source>
 +
[[Category:C]]

Latest revision as of 15:17, 6 July 2015

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, now this page is for historic record only.
// http://www.organicdesign.co.nz/peerd - nodal p2p wiki daemon
// 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)
#define nodeTRUE   1  // [[[[nodeTRUE]]]]
#define nodeNEXT   1  // [[[[nodeNEXT]]]]
#define nodePREV   2  // [[[[nodePREV]]]]
#define nodeCODE   3  // [[[[nodeCODE]]]] is a [[[[association|pseudo node-value]]]] to determine is the nodes non-nodal-data is a function-ref
#define nodePARENT 4  // [[[[nodePARENT]]]] updated by [[[[nodal reduction]]]], the node for which thsi is the [[[[focus]]]]
#define nodeTRIE   5  // [[[[nodeTRIE]]]] use a different root for trie/hash so it can't interfere with serialisation of nodal-root
#define nodeFIRST  6  // [[[[nodeFIRST]]]]
#define nodeLAST   7  // [[[[nodeLAST]]]]
#define nodeMAKE   8  // [[[[nodeMAKE]]]]
#define nodeINIT   9  // [[[[nodeINIT]]]]
#define nodeMAIN   10 // [[[[nodeMAIN]]]]
#define nodeEXIT   11 // [[[[nodeEXIT]]]]


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <time.h>
#include <regex.h>
#include "util.h"           // [[[[util.c]]]]: General utils, file,log,string etc

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

// Interface related
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
//#include "SDL_opengl.h"     // OpenGL example [[[[http://www.libsdl.org/cgi/docwiki.cgi/OpenGL_20Full_20Example|here]]]]
#include "interface.h"      // [[[[interface.c]]]]: Layer model, video, audio, input, OpenGL

// Peer daemon setup
#if __WIN32__
#include "servicate.h"      // [[[[servicate.c]]]]
#elif __APPLE__
#include "launchd.h"        // [[[[launchd.c]]]]
#elif __unix__
#include "daemonise.h"      // [[[[daemonise.c]]]]
#endif

// [[[[Communications]]]] related
#if __WIN32__
#include <winsock.h>
#else
#include <sys/socket.h>     // see [[[[http://www.opengroup.org/onlinepubs/009695399/functions/select.html|select()]]]]
#include <sys/select.h>
#include <netinet/in.h>
#include <sys/time.h>       // for select() related stuff
#include <fcntl.h>          // O_RDWR, O_NONBLOCK, F_GETFL, F_SETFL
#include <netdb.h>          // for [[[[http://www.opengroup.org/onlinepubs/009695399/basedefs/netdb.h.html|hostent struct]]]]
#endif
#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();

	}