Advertisement
Guest User

Untitled

a guest
Aug 17th, 2016
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.24 KB | None | 0 0
  1. #include "ppapi/cpp/instance.h"
  2. #include "ppapi/cpp/module.h"
  3. #include "ppapi/cpp/var.h"
  4. #include "ppapi/cpp/var_array_buffer.h"
  5. #include <cstring>
  6. #include "nacl_io/nacl_io.h"
  7. #include "ppapi/utility/completion_callback_factory.h"
  8. #include "ppapi/utility/threading/simple_thread.h"
  9. #include "sys/mount.h"
  10. #include "logger.h"
  11. #include "ppapi/cpp/var_dictionary.h"
  12. #include <string.h>
  13. #include <queue>
  14. #include "handlers.h"
  15.  
  16. PP_Instance g_instance;
  17. static int i=0;
  18. typedef struct {
  19.   const char* name;
  20.   HandleFunc function;
  21. } FuncNameMapping;
  22.  
  23.  
  24. int HandleFopen(pp::Var params, pp::Var* out, const char** error)
  25. {
  26.     Logger::Log("I am open file");
  27. }
  28.  
  29. static FuncNameMapping g_function_map[] = {
  30.     {"fopen", HandleFopen},
  31.     {NULL, NULL},
  32. };
  33.  
  34.  
  35.  
  36. /**
  37.  * This is a base of your NaCl application. There is one instance of
  38.  * <code>pp::Instance</code> class object per <embed> element on a web page.
  39.  */
  40. class NaClInstance : public pp::Instance {
  41.  public:
  42.  
  43.   explicit NaClInstance(PP_Instance instance)
  44.       : pp::Instance(instance),
  45.         glb_app_thread(this),
  46.         m_callback_factory(this),
  47.         glb_file_thread(this),
  48.         lock_file_()
  49.   {
  50.  
  51. }
  52.  
  53.   virtual ~NaClInstance() {
  54.       glb_app_thread.Join();
  55.   }
  56.  
  57.   /**
  58.    * Handles messages from JS sent by <code>nacl_module.postMessage(...)</code>.
  59.    * @see <code>HandleMessage</code> in <toolchain>/ppapi/cpp/instance.h file for more details.
  60.    */
  61.   virtual void HandleMessage(const pp::Var& message) {
  62.         Logger::Log(std::string("Message Received"));
  63.  
  64.         if (!message.is_dictionary()) {
  65.             Logger::Error(std::string("Invalid Message"));
  66.             return;
  67.         }
  68.  
  69.         pp::VarDictionary var_dictionary_message(message);
  70.         std::string command = var_dictionary_message.Get("command").AsString();
  71.         if (command == "open") {
  72.             Logger::Log(std::string("Open File"));
  73.  
  74.             pp::VarDictionary fileOp;
  75.             pp::VarArray args;
  76.             args.Set(0, "filename.txt");
  77.             args.Set(1, "wb");
  78.             fileOp.Set("args", args);
  79.             fileOp.Set("cmd", "fopen");
  80.  
  81.             queue_file_.push(fileOp);
  82.  
  83.         } else if (command == "write") {
  84.             Logger::Log(std::string("Write"));
  85.             pp::VarDictionary fileOp;
  86.             pp::VarArray args;
  87.             args.Set(0, "buffer");
  88.             args.Set(1, "somedata");
  89.             fileOp.Set("args", args);
  90.             fileOp.Set("cmd", "fwrite");
  91.  
  92.             queue_file_.push(fileOp);
  93.  
  94.         } else if (command == "close") {
  95.             Logger::Log(std::string("Close"));
  96.             pp::VarDictionary fileOp;
  97.             pp::VarArray args;
  98.             args.Set(0, "buffer");
  99.             args.Set(1, "somedata");
  100.             fileOp.Set("args", args);
  101.             fileOp.Set("cmd", "fclose");
  102.         }
  103.   }
  104.  
  105.  
  106.  
  107.   /**
  108.    * Initializes this instance with provided arguments listed in the <embed>
  109.    * tag.
  110.    * @see <code>Init()</code> in <toolchain>/ppapi/cpp/instance.h file for more details.
  111.    */
  112.   virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
  113.         Logger::InitializeInstance(this);
  114.  
  115.         Logger::Log(std::string("Init has been called for instance"));
  116.         // Set global instance for posting messages to JavaScript
  117.         g_instance = this->pp_instance();
  118.  
  119.         // Initialize NaCL I/O library
  120.         nacl_io_init_ppapi(this->pp_instance(), pp::Module::Get()->get_browser_interface());
  121.  
  122.         // Create GLB Application thread
  123.         glb_app_thread.Start();
  124.  
  125.  
  126.         glb_file_thread.Start();
  127.  
  128.         // Unmount default file system and mount persistent HTML5 filesystem
  129.         umount("/");
  130.         int mount_result = mount("","/persistent","html5fs",0,"type=PERSISTENT,expected_size=1048576");
  131.         Logger::Log(std::string("The mount result is: (0 for success, -1 for failure)"));
  132.         Logger::Log(std::to_string(mount_result));
  133.  
  134.         //start a permanent thread to handle FileIO
  135.         glb_file_thread.message_loop().PostWork(m_callback_factory.NewCallback(&NaClInstance::HandleFileThread));
  136.  
  137.     return true;
  138.   }
  139.  
  140.  private:
  141.     pp::SimpleThread glb_app_thread;
  142.     pp::CompletionCallbackFactory<NaClInstance> m_callback_factory;
  143.     pp::SimpleThread glb_file_thread;
  144.     //std::queue< std::shared_ptr<int> > queue_file_;
  145.     std::queue< pp::VarDictionary > queue_file_;
  146.     pp::Lock lock_file_;
  147.     FILE * pFile;
  148.     void HandleFileThread(int32_t result) {
  149.         while (1) {
  150.             if(!queue_file_.empty())
  151.             {
  152.                 lock_file_.Acquire();
  153.                 ///std::shared_ptr<int> data = queue_file_.front();
  154.                 pp::VarDictionary params = queue_file_.front();
  155.                 HandleIOMessage(params);
  156.                 queue_file_.pop();
  157.                 lock_file_.Release();
  158.             }
  159.         }
  160.       }
  161.  
  162.     /**
  163.      * Get a value from a Dictionary, given a string key.
  164.      * @param[in] dict The dictionary to look in.
  165.      * @param[in] key The key to look up.
  166.      * @return PP_Var The value at |key| in the |dict|. If the key doesn't exist,
  167.      *     return a PP_Var with the undefined value.
  168.      */
  169.     pp::Var GetDictVar(pp::VarDictionary dict, const char* key) {
  170.       pp::Var key_var (key);
  171.       Logger::Log(std::string("the key is :") + key_var.AsString());
  172.       pp::Var value = dict.Get(key_var);
  173.       return value;
  174.     }
  175.  
  176.     int ParseMessage(pp::Var message, const char** out_function,
  177.             pp::Var* out_params) {
  178.  
  179.         if (!message.is_dictionary()) {
  180.             return 1;
  181.         }
  182.         pp::VarDictionary var_dictionary_message(message);
  183.         pp::Var cmd_value = GetDictVar(var_dictionary_message, "cmd");
  184.  
  185.         *out_function =  cmd_value.AsString().c_str();
  186.  
  187.  
  188.         if (!cmd_value.is_string()) {
  189.             return 1;
  190.         }
  191.         *out_params = GetDictVar(var_dictionary_message, "args");
  192.         if (!out_params->is_array()) {
  193.             return 1;
  194.         }
  195.         return 0;
  196.     }
  197.  
  198.     /**
  199.      * Given a function name, look up its handler function.
  200.      * @param[in] function_name The function name to look up.
  201.      * @return The handler function mapped to |function_name|.
  202.      */
  203.     HandleFunc GetFunctionByName(const char* function_name) {
  204.       FuncNameMapping* map_iter = g_function_map;
  205.       for (; map_iter->name; ++map_iter) {
  206.         if (strcmp(map_iter->name, function_name) == 0) {
  207.           return map_iter->function;
  208.         }
  209.       }
  210.  
  211.       return NULL;
  212.     }
  213.  
  214.  
  215.     void HandleIOMessage(pp::Var message) {
  216.         const char* function_name; //get the function name from the message
  217.         pp::Var params;  //get the params from the message
  218.         if (ParseMessage(message, &function_name, &params)) {
  219.             Logger::Log("Unable to parse message");
  220.           return;
  221.         }
  222.  
  223.         HandleFunc function = GetFunctionByName(function_name);
  224.         if (!function) {
  225.           /* Function name wasn't found. Error.*/
  226.             Logger::Log("Error: Unknown function \"%s\"", function_name);
  227.           return;
  228.         }
  229.  
  230.         /* Function name was found, call it.
  231.         pp::Var result_var;
  232.         const char* error;
  233.         int result = (*function)(params, &result_var, &error);
  234.         if (result != 0) {
  235.           /* Error.
  236.           if (error != NULL) {
  237.             PostMessage("Error: \"%s\" failed: %s.", function_name, error);
  238.             free((void*)error);
  239.           } else {
  240.             PostMessage("Error: \"%s\" failed.", function_name);
  241.           }
  242.           return;
  243.         }*/
  244.  
  245.         /* Function returned an output dictionary. Send it to JavaScript.
  246.         g_ppb_messaging->PostMessage(g_instance, result_var);
  247.         g_ppb_var->Release(result_var);*/
  248.       }
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.     /*
  256.     void HandleIOMessage(int data) {
  257.  
  258.         if (i == 1) {
  259.             pFile = fopen("/persistent/file.txt", "wb");
  260.             if (pFile == NULL) {
  261.                 Logger::Log(std::string("Can not open file"));
  262.             } else {
  263.                 std::string s = std::to_string(i);
  264.                 Logger::Log(s);
  265.             Logger::Log(std::string("File Opened"));
  266.         }
  267.     } else if (i == 2) {
  268.          char buffer[] = { 'x' , 'y' , 'z' };
  269.           if(pFile != NULL)
  270.           {
  271.               fwrite (buffer , sizeof(char), sizeof(buffer), pFile);
  272.               Logger::Log(std::string("Line Written"));
  273.           }
  274.     }else if(i==3)
  275.     {
  276.           if(pFile != NULL)
  277.           {
  278.               fclose(pFile);
  279.               Logger::Log(std::string("File Closed"));
  280.           }
  281.  
  282.     }
  283.  
  284. }
  285. */
  286.  
  287. };
  288.  
  289. /**
  290.  * A NaCl app must have one class that implements <code>pp::Module</code>.
  291.  */
  292. class NaClModule : public pp::Module {
  293.  public:
  294.   NaClModule()
  295.       : pp::Module() {
  296.   }
  297.  
  298.   virtual ~NaClModule() {
  299.   }
  300.  
  301.   /**
  302.    * This method is called every time a browser encounters an <embed> element
  303.    * on a web page.
  304.    */
  305.   virtual pp::Instance* CreateInstance(PP_Instance instance) {
  306.     return new NaClInstance(instance);
  307.   }
  308. };
  309.  
  310. namespace pp {
  311.  
  312. /**
  313.  * This function is an entry point to a NaCl application.
  314.  */
  315. Module* CreateModule() {
  316.   return new NaClModule();
  317. }
  318.  
  319. }  // namespace pp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement