Difference between revisions of "Peerd.c"

From Organic Design wiki
m
m
 
(197 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 main(int argc, char **argv) {
+
// List & Node Space
 
+
#include "listSpace.h"      // [[[[listSpace.c]]]]: listSpace and some C-specific extras: hash, trie, linked-list
#include "util.c"       // General utils, file,log,string etc
+
#include "nodeSpace.h"      // [[[[nodeSpace.c]]]]: NodeSpace function declarations and initialisation
 +
#include "serialise.h"     // [[[[serialise.c]]]]: Nodal-to-text and visa-versa
  
#ifdef WINDOWS          // Set up as a daemon or service
+
// Interface related
#include "servicate.c"
+
#include "SDL.h"
#else
+
#include "SDL_image.h"
#include "daemonise.c"
+
#include "SDL_ttf.h"
#endif
+
//#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 "listSpace.c" // listSpace and some C-specific extras: hash, trie, linked-list
+
// 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
  
// Store the command-line args in the listSpace hash, eg char *name = *hash("name")
+
// [[[[Communications]]]] related
printf("argc=%d\n",argc);
+
#if __WIN32__
int l;
+
#include <winsock.h>
while (argc-->2) {
+
#else
char **arg = split('=',argv[argc]);
+
#include <sys/socket.h>    // see [[[[http://www.opengroup.org/onlinepubs/009695399/functions/select.html|select()]]]]
*hash(*arg) = strncpy(malloc(l=strlen(arg[1])+1),arg[1],l);
+
#include <sys/select.h>
printf("\t*hash(\"%s\")=\"%s\"\n",arg[0],arg[1]);
+
#include <netinet/in.h>
free(arg);
+
#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
  
// Make a global peer name and port number
+
int main(int argc, char **argv) {
char *peer = strncpy(malloc(l=strlen(argv[1])+1),argv[1],l);
 
int port = atoi(*hash("port"));
 
 
 
// Set name as seen in ps list
 
int i;
 
char *tmp = *argv;
 
for (i=0; i<30; i++) tmp[i] = 65+i;
 
sprintf(*argv,"peerd: %s (http%d)",peer,port);
 
*argv[strlen(*argv)] = '\0';
 
printf("argv[0] = \"%s\"\n",*argv);
 
 
 
printf("postargs\n");
 
#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 "nodalHusk.c"  // Build initial nodal environment
 
#include "server.c"    // Set up listening socket and declare serverIterate
 
  
printf("port=%s\n",*hash("port"));
+
logAdd("");            // Blank line in log to separate peerd sessions
printf("fredie=%s\n",*hash("fred"));
+
peerdInit();           // Set up as a daemon or service
logAdd("\nEntering nodal reduction...\n");
+
listInit();            // Initialise list-space and hash/trie functions
while(nodeReduce(this=ROOT));
+
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
  
free(space);
+
// Main [[[[nodal reduction]]]] loop
 +
// - maintains [[[[this]]]], [[[[parent]]]] and [[[[grandpa]]]] globals
 +
logAdd("Handing program execution over to nodal reduction...");
 +
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();

	}