Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ppapi/cpp/instance.h"
- #include "ppapi/cpp/module.h"
- #include "ppapi/cpp/var.h"
- #include "ppapi/cpp/var_array_buffer.h"
- #include <cstring>
- #include "nacl_io/nacl_io.h"
- #include "ppapi/utility/completion_callback_factory.h"
- #include "ppapi/utility/threading/simple_thread.h"
- #include "sys/mount.h"
- #include "logger.h"
- #include "ppapi/cpp/var_dictionary.h"
- #include <string.h>
- #include <queue>
- #include "handlers.h"
- PP_Instance g_instance;
- static int i=0;
- typedef struct {
- const char* name;
- HandleFunc function;
- } FuncNameMapping;
- int HandleFopen(pp::Var params, pp::Var* out, const char** error)
- {
- Logger::Log("I am open file");
- }
- static FuncNameMapping g_function_map[] = {
- {"fopen", HandleFopen},
- {NULL, NULL},
- };
- /**
- * This is a base of your NaCl application. There is one instance of
- * <code>pp::Instance</code> class object per <embed> element on a web page.
- */
- class NaClInstance : public pp::Instance {
- public:
- explicit NaClInstance(PP_Instance instance)
- : pp::Instance(instance),
- glb_app_thread(this),
- m_callback_factory(this),
- glb_file_thread(this),
- lock_file_()
- {
- }
- virtual ~NaClInstance() {
- glb_app_thread.Join();
- }
- /**
- * Handles messages from JS sent by <code>nacl_module.postMessage(...)</code>.
- * @see <code>HandleMessage</code> in <toolchain>/ppapi/cpp/instance.h file for more details.
- */
- virtual void HandleMessage(const pp::Var& message) {
- Logger::Log(std::string("Message Received"));
- if (!message.is_dictionary()) {
- Logger::Error(std::string("Invalid Message"));
- return;
- }
- pp::VarDictionary var_dictionary_message(message);
- std::string command = var_dictionary_message.Get("command").AsString();
- if (command == "open") {
- Logger::Log(std::string("Open File"));
- pp::VarDictionary fileOp;
- pp::VarArray args;
- args.Set(0, "filename.txt");
- args.Set(1, "wb");
- fileOp.Set("args", args);
- fileOp.Set("cmd", "fopen");
- queue_file_.push(fileOp);
- } else if (command == "write") {
- Logger::Log(std::string("Write"));
- pp::VarDictionary fileOp;
- pp::VarArray args;
- args.Set(0, "buffer");
- args.Set(1, "somedata");
- fileOp.Set("args", args);
- fileOp.Set("cmd", "fwrite");
- queue_file_.push(fileOp);
- } else if (command == "close") {
- Logger::Log(std::string("Close"));
- pp::VarDictionary fileOp;
- pp::VarArray args;
- args.Set(0, "buffer");
- args.Set(1, "somedata");
- fileOp.Set("args", args);
- fileOp.Set("cmd", "fclose");
- }
- }
- /**
- * Initializes this instance with provided arguments listed in the <embed>
- * tag.
- * @see <code>Init()</code> in <toolchain>/ppapi/cpp/instance.h file for more details.
- */
- virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
- Logger::InitializeInstance(this);
- Logger::Log(std::string("Init has been called for instance"));
- // Set global instance for posting messages to JavaScript
- g_instance = this->pp_instance();
- // Initialize NaCL I/O library
- nacl_io_init_ppapi(this->pp_instance(), pp::Module::Get()->get_browser_interface());
- // Create GLB Application thread
- glb_app_thread.Start();
- glb_file_thread.Start();
- // Unmount default file system and mount persistent HTML5 filesystem
- umount("/");
- int mount_result = mount("","/persistent","html5fs",0,"type=PERSISTENT,expected_size=1048576");
- Logger::Log(std::string("The mount result is: (0 for success, -1 for failure)"));
- Logger::Log(std::to_string(mount_result));
- //start a permanent thread to handle FileIO
- glb_file_thread.message_loop().PostWork(m_callback_factory.NewCallback(&NaClInstance::HandleFileThread));
- return true;
- }
- private:
- pp::SimpleThread glb_app_thread;
- pp::CompletionCallbackFactory<NaClInstance> m_callback_factory;
- pp::SimpleThread glb_file_thread;
- //std::queue< std::shared_ptr<int> > queue_file_;
- std::queue< pp::VarDictionary > queue_file_;
- pp::Lock lock_file_;
- FILE * pFile;
- void HandleFileThread(int32_t result) {
- while (1) {
- if(!queue_file_.empty())
- {
- lock_file_.Acquire();
- ///std::shared_ptr<int> data = queue_file_.front();
- pp::VarDictionary params = queue_file_.front();
- HandleIOMessage(params);
- queue_file_.pop();
- lock_file_.Release();
- }
- }
- }
- /**
- * Get a value from a Dictionary, given a string key.
- * @param[in] dict The dictionary to look in.
- * @param[in] key The key to look up.
- * @return PP_Var The value at |key| in the |dict|. If the key doesn't exist,
- * return a PP_Var with the undefined value.
- */
- pp::Var GetDictVar(pp::VarDictionary dict, const char* key) {
- pp::Var key_var (key);
- Logger::Log(std::string("the key is :") + key_var.AsString());
- pp::Var value = dict.Get(key_var);
- return value;
- }
- int ParseMessage(pp::Var message, const char** out_function,
- pp::Var* out_params) {
- if (!message.is_dictionary()) {
- return 1;
- }
- pp::VarDictionary var_dictionary_message(message);
- pp::Var cmd_value = GetDictVar(var_dictionary_message, "cmd");
- *out_function = cmd_value.AsString().c_str();
- if (!cmd_value.is_string()) {
- return 1;
- }
- *out_params = GetDictVar(var_dictionary_message, "args");
- if (!out_params->is_array()) {
- return 1;
- }
- return 0;
- }
- /**
- * Given a function name, look up its handler function.
- * @param[in] function_name The function name to look up.
- * @return The handler function mapped to |function_name|.
- */
- HandleFunc GetFunctionByName(const char* function_name) {
- FuncNameMapping* map_iter = g_function_map;
- for (; map_iter->name; ++map_iter) {
- if (strcmp(map_iter->name, function_name) == 0) {
- return map_iter->function;
- }
- }
- return NULL;
- }
- void HandleIOMessage(pp::Var message) {
- const char* function_name; //get the function name from the message
- pp::Var params; //get the params from the message
- if (ParseMessage(message, &function_name, ¶ms)) {
- Logger::Log("Unable to parse message");
- return;
- }
- HandleFunc function = GetFunctionByName(function_name);
- if (!function) {
- /* Function name wasn't found. Error.*/
- Logger::Log("Error: Unknown function \"%s\"", function_name);
- return;
- }
- /* Function name was found, call it.
- pp::Var result_var;
- const char* error;
- int result = (*function)(params, &result_var, &error);
- if (result != 0) {
- /* Error.
- if (error != NULL) {
- PostMessage("Error: \"%s\" failed: %s.", function_name, error);
- free((void*)error);
- } else {
- PostMessage("Error: \"%s\" failed.", function_name);
- }
- return;
- }*/
- /* Function returned an output dictionary. Send it to JavaScript.
- g_ppb_messaging->PostMessage(g_instance, result_var);
- g_ppb_var->Release(result_var);*/
- }
- /*
- void HandleIOMessage(int data) {
- if (i == 1) {
- pFile = fopen("/persistent/file.txt", "wb");
- if (pFile == NULL) {
- Logger::Log(std::string("Can not open file"));
- } else {
- std::string s = std::to_string(i);
- Logger::Log(s);
- Logger::Log(std::string("File Opened"));
- }
- } else if (i == 2) {
- char buffer[] = { 'x' , 'y' , 'z' };
- if(pFile != NULL)
- {
- fwrite (buffer , sizeof(char), sizeof(buffer), pFile);
- Logger::Log(std::string("Line Written"));
- }
- }else if(i==3)
- {
- if(pFile != NULL)
- {
- fclose(pFile);
- Logger::Log(std::string("File Closed"));
- }
- }
- }
- */
- };
- /**
- * A NaCl app must have one class that implements <code>pp::Module</code>.
- */
- class NaClModule : public pp::Module {
- public:
- NaClModule()
- : pp::Module() {
- }
- virtual ~NaClModule() {
- }
- /**
- * This method is called every time a browser encounters an <embed> element
- * on a web page.
- */
- virtual pp::Instance* CreateInstance(PP_Instance instance) {
- return new NaClInstance(instance);
- }
- };
- namespace pp {
- /**
- * This function is an entry point to a NaCl application.
- */
- Module* CreateModule() {
- return new NaClModule();
- }
- } // namespace pp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement