Difference between revisions of "NodeSpace.h"

From Organic Design wiki
m
m
Line 11: Line 11:
 
// - nodal constants
 
// - nodal constants
 
// - this and parent
 
// - this and parent
 
// stateless
 
node nodeSESSION;
 
node nodeSTREAMS;
 
 
// function ref
 
node nodeEVENTS;
 
node nodeIO;
 
node nodeSERVER;
 
node nodeDESKTOP;
 
 
// pointer to struct
 
node nodeSTREAM;
 
node nodeSPRITE;
 
node nodeEVENT;
 
  
 
// Prototypes
 
// Prototypes

Revision as of 03:00, 28 November 2006

// [[[[1]]]] - nodal p2p wiki daemon // This article and all its includes are licenced under LGPL // GPL: [[[[2]]]] // SRC: [[[[3]]]] // included in [[[[4]]]][[[[5]]]]

typedef int node; // node: a type for referring to nodes typedef void (code)(); // code: a type for executing functions from pointer

// constants defined in [[peerd.c]] // - nodal constants // - this and parent

// Prototypes node nodeGetValue(node subject,node key); node nodeSetValue(node subject,node key,node value); node nodeInsertKey(node subject,node key); node nodeRemoveKey(node subject,node key); node nodeLoopInsert(node subject,node insert); node nodeLoopRemove(node subject); node nodeTraverse(node subject,node *path); void **nodeState(node subject,node key); void nodeInit(); void nodeExit(); void ifExit(); void nodeReduce();


// ----------------------------------------------------------------------------------------- // // nodeSpace.c

// set nodal value of node/path node nodeGetValue(node subject,node key) { // todo: if subject/nodeGETVAL... return listGetValue(listTraverse(subject,key)); }

node nodeSetValue(node subject,node key,node value) { // todo: onChange return listSetValue(listTraverse(subject,key),value); }

// Returns a newly created node node nodeInsert() { return listInsert(); }

// Returns newly created node // - this is just traverse() but with an event raised node nodeInsertKey(node subject,node key) { // todo: hook in nodeSTART return listTraverse(subject,key); }

// Returns the node that was removed node nodeRemoveKey(node subject,node key) { // todo: hook in nodeSTOP node i = listTraverse(subject,key); // todo: delete return i; }

// Returns the inserted node as the new loop pointer // - this allows it to act as a stack if nodeLOOP (current-loop-node) is updated with returned node // - either can be 0 on entry (for new loop and/or new node) // todo: update nodePARENT of inserted node node nodeLoopInsert(node subject,node insert) { node focus = nodeGetValue(subject,0); if (insert == 0) insert = nodeInsert(); if (focus) { // Insert into existing [[loop]] node next = nodeGetValue(focus,nodeNEXT); nodeSetValue(insert,nodeNEXT,next); nodeSetValue(next,nodePREV,insert); nodeSetValue(focus,nodeNEXT,insert); nodeSetValue(insert,nodePREV,focus); } else { // No existing [[loop]], make new node into a single item loop nodeSetValue(focus,0,insert); nodeSetValue(focus,nodeNEXT,insert); nodeSetValue(focus,nodePREV,insert); } return insert; }

// Returns the new loop pointer so nodeLOOP can be removed and updated // todo: nodePARENT should be set to 0 node nodeLoopRemove(node subject) { node prev = nodeGetValue(subject,nodePREV); node next = nodeGetValue(subject,nodeNEXT); nodeSetValue(next,nodePREV,prev); return nodeSetValue(prev,nodeNEXT,next); }

// not used yet - use array-of-node path? node nodeTraverse(node subject,node *path) { }

// Treat an association value as a pointer index void **nodeState(node subject,node key) { int index = nodeGetValue(subject,key); if (index == 0) nodeSetValue(subject,key,index = insertPointer(NULL)); return pointer(index); }

// Clean up all the mess and exit // - add exit functions here for any includes requiring cleanup before exit void nodeExit() { logAdd("Gracefully exiting."); ifExit(); ioExit(); listExit(); exit(EXIT_SUCCESS); }


// Set up initial nodal structure // - todo: this structure should be read from [[nodeSpace.txt]] // - use [[io.c]] for reading content from wiki if url/file char *serialise(node); void deserialise(char*); void nodeInit() {

if (file) {

// todo: load from file and deserialise

} else { // no file specified in command-line args, use default inline text deserialise("\ *root\ *io;session;\ *io\ *server;streams;\ *stream\ *session\ *events;desktop;\ *sprite\ "); } nodeIO = trieGetValue("io"); logAdd("nodeIO = %d",nodeIO); nodeSESSION = trieGetValue("session"); logAdd("nodeSESSION = %d",nodeSESSION); nodeSERVER = trieGetValue("server"); logAdd("nodeSERVER = %d",nodeSERVER); nodeSTREAMS = trieGetValue("streams"); logAdd("nodeSTREAMS = %d",nodeSTREAMS); nodeSTREAM = trieGetValue("stream"); logAdd("nodeSTREAM = %d",nodeSTREAM); nodeEVENTS = trieGetValue("events"); logAdd("nodeEVENTS = %d",nodeEVENTS); nodeDESKTOP = trieGetValue("desktop"); logAdd("nodeDESKTOP = %d",nodeDESKTOP); nodeSPRITE = trieGetValue("sprite"); logAdd("nodeSPRITE = %d",nodeSPRITE);

}

// Moves "this" to node's [[focus]] if exists then rotates [[loop]] and executes/reduces the focus item void nodeReduce() { if (this = listGetValue(parent = this)) { // Move "this" to the [[focus]] in the node's loop listSetValue(parent,nodeGetValue(this,nodeNEXT)); // Rotate the [[loop]] nodeSetValue(this,nodePARENT,parent); // Update the [[parent]] association code *ptr = *nodeState(this,nodeCODE); // [[nodeCODE]] value is a pointer-index ptr ? ptr() : nodeReduce(); // Execute ptr if non-NULL, else [[reduce]] item } }