Docs GODI Archive
Projects Blog Link DB

Search GODI:


More options
File lib/felix/rtl/flx_stdin_drv.cxx GODI Package apps-felix
 
   flx_stdin_drv.cxx  
#line 94 "lpsrc/flx_test.ipk"
#include <stdio.h>
#include <vector>
#include <list>
#include <map>
#include <string.h>
#include <assert.h>
#include <string>
//#include <unistd.h>

#include "flx_rtl.hpp"
#include "flx_collector.hpp"
#include "flx_dynlink.hpp"

using namespace flx::rtl;
using namespace flx::gc::generic;
using namespace flx::gc::collector;
using namespace std;

#ifdef FLX_STATIC_LINK
extern void *line_handler_creator;
#endif

typedef string message_t;

struct library_linkage_t : flx_dynlink_t
{
  typedef con_t *(*line_handler_creator_t)
  (
    thread_frame_t*
  );

  // interface
  line_handler_creator_t create_line_handler;

  // find dlsyms from library
  void usr_link()
  {
    create_line_handler = (line_handler_creator_t)
      DLSYM(library,line_handler_creator);
    if(!create_line_handler) {
      printf("Unable to get line handler creator procedure from library\n");
      exit(1);
    }
  }
};


int main(int argc, char** argv)
{

  if (argc<2)
  {
    printf("stdin filter driver\n");
    printf("usage flx_stdin_drv ./<shared library name>\n");
    exit(1);
  }
  try
  {
    printf("Running stdin filter ..\n");
    library_linkage_t library;
    flx_libinit_t instance;

    library.link(argv[1]);
    malloc_free allocator;
    flx_collector_t collector(&allocator);
    instance.create(&library, &collector,0,NULL,stdin,stdout,stderr);

    collector.add_root(instance.start_proc);
    int gc_counter = 0;
    con_t *top =
      library.create_line_handler(
        instance.thread_frame
    );
    collector.add_root(top);
    fthread_t ft = fthread_t(top);
    _uctor_ *request = ft.run();

    while(true)
    {
      // dispatch message
      if(request && request->variant == svc_read)
      {
        if (request->data)
        {
          char buffer[2000];
          if(fgets(buffer,2000,stdin))
            **(string**)(request->data) = string(buffer);
          else break;
        }
        else
        {
          printf("SERVICE ERROR: waiting for message without setting address\n");
          exit(1);
        }
      }

      // collect garbage
      {
        gc_counter++;
        if(gc_counter==10) {
          int collected FLX_UNUSED = collector.collect();
          if(collector.get_allocation_count()>100)
            printf("WARNING!: Currently allocated=%ld\n",
            collector.get_allocation_count());
          gc_counter = 0;
        }
      }
    }
    instance.destroy();
    printf("DONE\n");
  }
  catch (...)
  {
    printf("EXCEPTION\n");
  }
}



This web site is published by Informatikbüro Gerd Stolpmann
Powered by Caml