2016-08-28 2 views
2

自分のC++ベースのPNaClモジュールを何回か構築する方法について、Googleのネイティブクライアントチュートリアルを読んでいます。何とか私は賢くなっていません。メッセージ機能を実装したいのですが。私はPNaClコードの基礎としての.ccファイルには、次のしている、このすべては、グーグルのHello Worldのチュートリアルから取られている:私はPNaClモジュールは、main()関数を使用していません理解したよう古いC++コードをPNaClモジュールに組み込む

#include "ppapi/cpp/instance.h" 
#include "ppapi/cpp/module.h" 
#include "ppapi/cpp/var_array.h" 
#include "ppapi/cpp/var.h" 

namespace { 
// The expected string sent by the browser. 
const char* const kHelloString = "hello"; 
// The string sent back to the browser upon receipt of a message 
// containing "hello". 
const char* const kReplyString = "hello from NaCl"; 
} // namespace 

class job1Instance : public pp::Instance { 
    public: 
     explicit job1Instance(PP_Instance instance): pp::Instance(instance) {} 
     virtual ~job1Instance() {} 

    virtual void HandleMessage(const pp::Var& message) { 
     if (!message.is_string()) { 
      return; 
     } 
     std::string message_txt = message.AsString(); 
     pp::Var reply; 
     if (message_txt == kHelloString) { 
      reply = pp::Var(kReplyString); 
      PostMessage(kReplyString); 
     } 
    } 
}; 

class job1 : public pp::Module { 
    public: 
     job1() : pp::Module() {} 
     virtual ~job1() {} 

     virtual pp::Instance* CreateInstance(PP_Instance instance) { 
     return new job1Instance(instance); 
    } 
}; 

namespace pp { 

    Module* CreateModule() { 
     return new job1(); 
    } 
} // namespace pp 

のは、私は私のPNaClモジュールで使用する乱数で2つの配列unsorted1unsorted2を作成し、古いC++のコードを持っているとしましょう:

#include <iostream> 
#include <stdint.h> 
#include <unistd.h> 
#include <array> 

// a function to create a random number between min and max 
int32_t rangeRandomAlg (int32_t min, int32_t max) { 
    int32_t num = max - min + 1; 
    int32_t remainder = RAND_MAX % num; 
    int32_t x; 
    do { 
     x = rand(); 
    } while (x >= RAND_MAX - remainder); 
    return min + x % num; 
} 

// a function to create arrays with random numbers 
void unsortedArrays(int32_t unsorted1[], int32_t unsorted2[],int32_t arrayElements, int32_t minNum, int32_t maxNum){ 
    for(int32_t i = 0; i <= arrayElements; i++) { 
     if (i < arrayElements/2) { 
      unsorted1[i] = rangeRandomAlg(minNum, maxNum); 
     } else { 
      unsorted2[i] = rangeRandomAlg(minNum, maxNum); 
     } 
    } 
} 

// the main function 
int32_t main(int32_t argc, char *argv[]) { 
    // declare all the zises 
    int32_t minNum = 0; 
    int32_t maxNum = 100; 
    int32_t arrayElements = maxNum; 

    // the arrays 
    int32_t unsorted1[arrayElements/2]; 
    int32_t unsorted2[arrayElements/2]; 

    // fill the arrays with random numbers 
    unsortedArrays(unsorted1, unsorted2, arrayElements, minNum, maxNum); 

    return 0; 
} 

私の問題は、私は非常に私はこれを統合する方法を理解していなかったということですコードをPNaClモジュールに入力し、HandleMessage()関数を使用してunsorted1とを送信しますアレイは、PostMesage()機能を使用してJavaScriptに戻ります。私はHandleMessage()機能の配列ではなく文字列で作業しなければならないことを知っています。

私は本当にこのネイティブクライアントの事を初めて知っているので、ここで助けを得ることを望みます。

+0

本当に手掛かりはありませんか? – TalG

答えて

2

さて、ここではに取得するためにいくつかの時間を要したソリューションです。これは、それに統合私のコードでPNaClモジュールの唯一のプリコンパイルされたC++コードである

// pepper includes 
#include "ppapi/cpp/instance.h" 
#include "ppapi/cpp/module.h" 
#include "ppapi/cpp/var_array.h" 
#include "ppapi/cpp/var.h" 
#include "json/json.h" 
#include <sstream> 

// cpp includes 
#include <stdint.h> 
#include <unistd.h> 
#include <array> 
#include <string> 

//static variables 
namespace { 
// The expected string sent by the browser. 
const char* const kHelloString = "Bereit fuer dein Modul"; 
// The string sent back to the browser upon receipt of a message 
// containing "hello". 
const char* const kReplyString = "PNaCl hat Ergebnisse geschickt"; 
} // namespace 

class job1Instance : public pp::Instance { 
public: 
     explicit job1Instance(PP_Instance instance): pp::Instance(instance) {} 
     virtual ~job1Instance() {} 

    virtual void HandleMessage(const pp::Var& message) { 
     /* 
     if (!message.is_string()) { 
      return; 
     } 
     std::string message_txt = message.AsString(); 
     pp::Var reply; 
     if (message_txt == kHelloString) { 
      reply = pp::Var(kReplyString); 
      PostMessage(kReplyString); 
     } 
     */ 

/*** my functions and data for the cpp code to integrate start here ***/ 
     // declare all the zises 
     int32_t minNum = 0; 
     int32_t maxNum = 100; 
     int32_t arrayElements = maxNum; 

     // the arrays 
     int32_t unsorted1[arrayElements/2]; 
     int32_t unsorted2[arrayElements/2]; 

     // fill the arrays with random numbers 
     unsortedArrays(unsorted1, unsorted2, arrayElements, minNum, maxNum); 
     std::string outRes1, outRes2; 
     arrayToString(unsorted1, arrayElements/2, outRes1); 
     arrayToString(unsorted2, arrayElements/2, outRes2); 
     PostMessage(outRes1); // send the unsorted1 array as a string to the JavaScript back 
    } 

private: 
    // function to create a random number between min and max 
    int32_t rangeRandomAlg (int32_t min, int32_t max) { 
     int32_t num = max - min + 1; 
     int32_t remainder = RAND_MAX % num; 
     int32_t x; 
     do { 
      x = rand(); 
     } while (x >= RAND_MAX - remainder); 
     return min + x % num; 
    } 

    // function to create arrays with random numbers 
    void unsortedArrays (int32_t unsorted1[], int32_t unsorted2[],int32_t arrayElements, int32_t minNum, int32_t maxNum) { 
     for(int32_t i = 0; i <= arrayElements; i++) { 
      if (i < arrayElements/2) { 
       unsorted1[i] = rangeRandomAlg(minNum, maxNum); 
      } else { 
       unsorted2[i] = rangeRandomAlg(minNum, maxNum); 
      } 
     } 
    } 

    // convert the arrays to string 
    void arrayToString (int32_t array[], int32_t arraySize, std::string& arrayString) { 
     for (int32_t i = 0; i <= arraySize; ++i){ 
      arrayString+= std::to_string(array[i]); 
      if (i != arraySize) { 
       arrayString+= ','; 
      } 
     } 
    } 
}; 
/*** my functions and data for the cpp code to integrate end here ***/ 


class job1 : public pp::Module { 
    public: 
     job1() : pp::Module() {} 
     virtual ~job1() {} 

     virtual pp::Instance* CreateInstance(PP_Instance instance) { 
     return new job1Instance(instance); 
    } 
}; 

namespace pp { 

    Module* CreateModule() { 
     return new job1(); 
    } 
} // namespace pp 

、それだけでoutRes1変数= unsorted1を送信します配列変数をindex.htmlファイルのJavaScriptコードの文字列として返します。 .nmfファイルとindex.htmlファイルは別々に書かなければなりません。もし誰かが私のコードを見たいのであれば、このPNaClモジュールのための基本的なコードを持っていて、私にコメントを書いてください。

関連する問題