Difference between revisions of "Peerd.c"

From Organic Design wiki
(move nodal constants into nodeSpace.c)
m
 
(298 intermediate revisions by 3 users not shown)
Line 1: Line 1:
// prototype of listSpace in c
+
{{legacy}}
// Licenced under LGPL: www.gnu.org/copyleft/lesser.html
+
<source lang="c">
#include <stdlib.h>
+
// http://www.organicdesign.co.nz/peerd - nodal p2p wiki daemon
#include <stdio.h>
+
// Source: http://www.organicdesign.co.nz/peerd/files/C - the C language version for *ix,OSX,NT platforms
#include <string.h>
+
// 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 & constants
+
// Globals
#define MAXITEMS 10000
+
int this = 0,parent,grandpa,port = 80;
int i,j; // global iterators
+
char *peer = "default";
int items = 0;
+
char *file = (char*)0;
int *space;
 
  
// Strucst used to emulate a variant-type
+
// Reserved nodes used by nodal reduction process (tmp)
typedef struct types {
+
#define nodeTRUE  1  // [[[[nodeTRUE]]]]
char* text;
+
#define nodeNEXT  1  // [[[[nodeNEXT]]]]
void (*code)();
+
#define nodePREV  2  // [[[[nodePREV]]]]
int number;
+
#define nodeCODE  3  // [[[[nodeCODE]]]] is a [[[[association|pseudo node-value]]]] to determine is the nodes non-nodal-data is a function-ref
} variant;
+
#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]]]]
  
int main() {
 
  
#include "listSpace.c"
+
#include <unistd.h>
#include "nodeSpace.c"
+
#include <stdlib.h>
#include "server.c"
+
#include <stdio.h>
 
+
#include <string.h>
// Insert items 0-127 as ascii character nodes
+
#include <errno.h>
// - this allows us to temporarily use list-space as a dictionary
+
#include <math.h>
//  since C/C++ doesn't inherently have one
+
#include <stdarg.h>
for (i=0; i<128; i++) listInsert();
+
#include <time.h>
 
+
#include <regex.h>
 
+
#include "util.h"           // [[[[util.c]]]]: General utils, file,log,string etc
// NODAL CONTENT - default "hardwired" default-nodal-app
 
 
 
// define the serialsed-text version of nodal structure
 
// - allow shorthand for Next's?
 
 
 
// NodalSpace = deserialise(string);
 
 
 
// if the node is a function (same type-check as reduce() uses),
 
//  then use its value (if any) as local function name,
 
//  change it to a ref with this[functionName] (if pre-declared - see below)
 
 
 
 
 
// NODAL FUNCTIONS - later included in content instead of "hardwired" here
 
 
 
// pre-declared functions built by nodal-app
 
// Currently it can't compile, so additional functionality required by nodal-app are pre-declared here
 
// later the script and its functions will be a nodal-state produced by a nodal-build and cache
 
  
// Process passed "alias1|alias2|alias3..." string (GUID's are just alias's)
+
// List & Node Space
// NOTE: GUID-alias is traversed as ASCII not binary - these are external guids
+
#include "listSpace.h"     // [[[[listSpace.c]]]]: listSpace and some C-specific extras: hash, trie, linked-list
//      - this is using list-space as a dictionary
+
#include "nodeSpace.h"      // [[[[nodeSpace.c]]]]: NodeSpace function declarations and initialisation
//       - the nodal structure of a number can still be binary
+
#include "serialise.h"      // [[[[serialise.c]]]]: Nodal-to-text and visa-versa
// - used by [de]serialise()
 
// - returns integer index of subject node
 
int processNodeLink(char *link) {
 
int subject = atoi(link); // not like this anymore, passed link is external
 
// - split by pipe and for each,
 
//   - if its a number, convert to a binary-coded-string of \x00's and \x01's
 
//  - traverse from root through name using chr-as-node-index,
 
//  - the first iteration creates the subject,
 
//  - subsequent iterations must link their last jump to the existing subject
 
return subject;
 
}
 
  
// Treat each character in text as a list-item-index to traverse from root
+
// Interface related
int traverseText(char *text) {
+
#include "SDL.h"
int i,subject = 0;
+
#include "SDL_image.h"
for (i=0; i<strlen(text); i++)
+
#include "SDL_ttf.h"
subject = listTraverse(subject, (int)text[i]);
+
//#include "SDL_opengl.h"    // OpenGL example [[[[http://www.libsdl.org/cgi/docwiki.cgi/OpenGL_20Full_20Example|here]]]]
return subject;
+
#include "interface.h"      // [[[[interface.c]]]]: Layer model, video, audio, input, OpenGL
}
 
  
// Convert a node to text for storage or transport
+
// Peer daemon setup
char* serialise(int subject) {
+
#if __WIN32__
char *text;
+
#include "servicate.h"      // [[[[servicate.c]]]]
return text;
+
#elif __APPLE__
}
+
#include "launchd.h"        // [[[[launchd.c]]]]
 +
#elif __unix__
 +
#include "daemonise.h"      // [[[[daemonise.c]]]]
 +
#endif
  
// Convert textual represntation of one or more nodes and associations to nodal change
+
// [[[[Communications]]]] related
int deserialise(char *text) {
+
#if __WIN32__
printf("\nPARSING THE FOLLOWING TEXT:\n--------------------------------------------\n%s\n--------------------------------------------\n\n", text);
+
#include <winsock.h>
printf("INFORMATION EXTRACTED:\n--------------------------------------------\n");
+
#else
int node, subject = 0, key, i, j = 0, linkStart, c;
+
#include <sys/socket.h>    // see [[[[http://www.opengroup.org/onlinepubs/009695399/functions/select.html|select()]]]]
char links[100];
+
#include <sys/select.h>
char *link1 = (char*)&links;
+
#include <netinet/in.h>
char *link2 = link1+50;
+
#include <sys/time.h>      // for select() related stuff
for (i = 0; i < strlen(text); i++) {
+
#include <fcntl.h>          // O_RDWR, O_NONBLOCK, F_GETFL, F_SETFL
char *s = text+i;
+
#include <netdb.h>          // for [[[[http://www.opengroup.org/onlinepubs/009695399/basedefs/netdb.h.html|hostent struct]]]]
if ((c = (int)s[0]) < 32) j = 1;
+
#endif
else switch (j) {
+
#include "io.h"             // [[[[io.c]]]]: Main server and stream setup and functions
case 0: // waiting for next line
 
break;
 
case 1: // starting new line
 
// - if not starting with *[[ then wait for next line
 
if (strncmp(s, "*[[", 3) == 0) {
 
j = 2;
 
linkStart = i;
 
} else j = 0;
 
break;
 
case 2: // processing first link
 
if (strncmp(s, "]]", 2) == 0) {
 
if (strncmp(s, "]]:[[", 5) == 0) {
 
// link1 as an association-key
 
// key = strtoint(...)
 
j = 3; // process link2 next
 
}
 
else {
 
// link1 specifies a change of nodal subject
 
link1 = strncpy(link1, text+linkStart+3, i-linkStart-3);
 
link1[i-linkStart-3] = '\0';
 
subject = processNodeLink(link1);
 
printf("%d:\n", subject);
 
j = 0; // wait for new line
 
}
 
}
 
break;
 
case 3: // processing second link
 
if (strncmp(s, "]]", 2) == 0) {
 
// process link2
 
link1 = strncpy(link1, text+linkStart+3, i-linkStart-3);
 
link2 = strchr(link1, '[')+2;
 
link1[i-linkStart-3] = '\0';
 
link1[i-linkStart-strlen(link2)-8] = '\0';
 
int key = processNodeLink(link1);
 
int val = processNodeLink(link2);
 
// store in nodal-space
 
// - we use list functions because nodal functions cause change
 
listSetValue(listTraverse(subject,key),val);
 
printf("   %d => %d\n", key, val);
 
j = 0;
 
}
 
break;
 
}
 
//printf("chr:%c,%d\n",s[0],j);
 
}
 
printf("--------------------------------------------\n");
 
return node;
 
}
 
  
// box properties
+
int main(int argc, char **argv) {
// events
 
// tcc
 
  
int test = deserialise( "testing deserialise:\n\r== can do headings for wiki ==\n*[[7491|Foo]]\n*[[2401|bar]]:[[1928|hello]] # my special association\r\r\n*[[2401|baz]]:[[1328|world]]\n*[[2421|key]]:[[1628]]\n*[[5061|key2]]:[[8928|]]\n*[[2821|key3]]:[[3928|value]]");
+
logAdd("");             // Blank line in log to separate peerd sessions
printf("\n%d list-space items have been created.\n", items);
+
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
  
// Count how many nodes have payloads
+
// Main [[[[nodal reduction]]]] loop
int p=0;
+
// - maintains [[[[this]]]], [[[[parent]]]] and [[[[grandpa]]]] globals
for (i=0; i<MAXITEMS; i++) if (listGetValue(i)) p++;
+
logAdd("Handing program execution over to nodal reduction...");
printf("%d items in use:\n", p);
+
while(1) nodeReduce();
for (i=0; i<MAXITEMS; i++) if (j=listGetValue(i)) printf("    %d = %d\n",i,j);
 
printf("\n\n");
 
  
}
+
}
 +
</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();

	}