Permalink
Browse files

Marginally working file loading

  • Loading branch information...
1 parent 741f34b commit 6d252485e6a6d2db7bc73c0c7819c611d6be150b Yurii Rashkovskii committed Apr 25, 2012
Showing with 80 additions and 21 deletions.
  1. +52 −4 c_src/main.c
  2. +0 −2 c_src/user.c
  3. +10 −13 hypnotoad.py
  4. +18 −2 src/main.clp
View
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <unistd.h>
#include "zmq.h"
#include "clips.h"
#include "hypnotoad.h"
@@ -24,14 +25,61 @@ int main(int argc, char **argv) {
zmq_setsockopt(sub, ZMQ_SUBSCRIBE, "", 0);
while (1) {
- char * msg;
+ char * msg, * topic;
+ int topic_size;
+ char * remote_filename;
zmq_msg_t request;
+
+ zmq_msg_init (&request);
+ zmq_recv (sub, &request, 0);
+ topic = zmq_msg_data (&request);
+ topic_size = zmq_msg_size(&request);
+
+ int fact = !strncmp(topic, "fact", 4);
+ if (!fact) {
+ remote_filename = (char *) malloc(topic_size + 1);
+ strncpy(remote_filename, topic, topic_size);
+ }
+
+ zmq_msg_close (&request);
+
zmq_msg_init (&request);
zmq_recv (sub, &request, 0);
msg = zmq_msg_data (&request);
- LoadFactsFromString(msg, zmq_msg_size(&request));
- Focus(module);
- Run(-1);
+
+ if (fact) {
+ LoadFactsFromString(msg, zmq_msg_size(&request));
+ Focus(module);
+ Run(-1);
+ } else {
+ char * filename = tmpnam(0);
+
+ /* dump file */
+ FILE * f = fopen(filename,"w+");
+ fwrite(msg, zmq_msg_size(&request), 1, f);
+ fclose(f);
+
+ char * fact = (char *) malloc(1024);
+ sprintf(fact, "(remote-file \"%s\" \"%s\")\n",remote_filename,filename);
+
+ /* load file mapping */
+ Focus(module);
+ LoadFactsFromString(fact, -1);
+
+ Focus(module);
+ Run(-1);
+
+#if 0
+ DATA_OBJECT res;
+ Eval("(facts)",&res);
+ fflush(0);
+#endif
+ /* cleanup */
+ free(fact);
+ free(remote_filename);
+ unlink(filename);
+ free(filename);
+ }
zmq_msg_close (&request);
}
View
@@ -12,8 +12,6 @@ void * remote_retr_file()
char * filename = (char *) DOToString(data);
- printf("Fetching %s\n", filename);
-
zmq_msg_t message;
zmq_msg_init_size (&message, strlen(filename));
memcpy (zmq_msg_data (&message), filename, strlen(filename));
View
@@ -25,15 +25,16 @@ def run(self):
while True:
data = self.command.receive()
url = urlparse(data)
- if url.scheme == 'hypnotoad' and url.netloc = self.command.id:
- self.serve(data, url.path)
+ if url.scheme == 'hypnotoad' and url.netloc == self.command.id:
+ self.serve(data, url.path.lstrip('/'))
def serve(self, url, path):
f = open(path, 'r')
+ print 1
content = f.read()
- b64 = base64.b64encode(content)
- self.command.publish('(remote-file-content "%s" "%s")' % (url, b64))
-
+ print 2
+ self.command.publish(url, content)
+ print 3
class Command:
def __init__(self, args):
@@ -47,8 +48,8 @@ def __init__(self, args):
self.args = args
self.id = str(uuid.uuid4())
- def publish(self, data):
- self.pub.send_multipart([data])
+ def publish(self, topic, data):
+ self.pub.send_multipart([topic, data])
def receive(self):
return self.sub.recv()
@@ -58,7 +59,7 @@ class Apply(Command):
def run(self):
print "Applying", self.args
include_stmt = string.join([ '(include "hypnotoad://%s/%s")' % (self.id, toadie) for toadie in self.args ])
- self.publish(include_stmt)
+ self.publish("facts", include_stmt)
Commands = {'apply': Apply}
@@ -72,11 +73,7 @@ def main():
file_server = FileServer(command)
file_server.start()
- command.run()
-
- file_server.join()
-
-
+ command.run()
if __name__ == "__main__":
View
@@ -2,10 +2,26 @@
(defrule load-constructs ?fact <- (require ?filename)
=>
- (load (remote-retrieve-file ?filename))
+ (remote-retrieve-file ?filename)
+ (assert (require-ready ?filename))
(retract ?fact))
(defrule load-facts ?fact <- (include ?filename)
=>
- (load-facts (remote-retrieve-file ?filename))
+ (remote-retrieve-file ?filename)
+ (assert (include-ready ?filename))
(retract ?fact))
+
+(defrule load-contructs-ready
+ ?fact <- (require-ready ?filename)
+ (remote-file ?filename ?local-filename)
+ =>
+ (load ?local-filename)
+ (retract ?fact))
+
+(defrule load-facts-ready
+ ?fact <- (include-ready ?filename)
+ (remote-file ?filename ?local-filename)
+ =>
+ (load-facts ?local-filename)
+ (retract ?fact))

0 comments on commit 6d25248

Please sign in to comment.