Difference between revisions of "Peerd.c"

From Organic Design wiki
(SDL quit at end)
m
 
(177 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
 +
 +
// 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
  
int err = 0;
+
// Interface related
int main(int argc, char **argv) {
+
#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
  
#include "util.c"       // General utils, file,log,string etc
+
// 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
  
#ifdef WINDOWS         // Set up as a daemon or service
+
// [[[[Communications]]]] related
#include "servicate.c"
+
#if __WIN32__
#else
+
#include <winsock.h>
#include "daemonise.c"
+
#else
#endif
+
#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
  
#include "listSpace.c"  // listSpace and some C-specific extras: hash, trie, linked-list
+
int main(int argc, char **argv) {
#include "args.c"      // Handle command-line args and globals like peer and port
 
#include "nodeSpace.c"  // NodeSpace function declarations and initialisation
 
#include "serialise.c"  // Nodal-to-text and visa-versa
 
#include "interface.c"  // Layer model (currently bitmap/imagemap-based)
 
#include "server.c"    // Set up listening socket and declare serverIterate
 
#include "nodalHusk.c"  // Build initial nodal environment
 
  
if (err == 0) {
+
logAdd("");            // Blank line in log to separate peerd sessions
char *msg = malloc(100);
+
peerdInit();            // Set up as a daemon or service
sprintf(msg,"Daemon \"%s\" started successfully and serving on port %d.",peer,port);
+
listInit();            // Initialise list-space and hash/trie functions
logAdd(msg);
+
nodeInit();             // Set up initial nodal structure for reduction loop
free(msg);
+
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
  
logAdd("Entering nodal reduction...\n");
+
// Main [[[[nodal reduction]]]] loop
while(nodeReduce(this=ROOT));
+
// - maintains [[[[this]]]], [[[[parent]]]] and [[[[grandpa]]]] globals
free(space);
+
logAdd("Handing program execution over to nodal reduction...");
SDL_Quit();
+
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();

	}