2013년 3월 21일 목요일

Serialization between C++ and Java

이전 블로그에서 이전 함 (원본 글 2013/03/21 작성)

여러 모듈에서 사용되는 arguments들을 공통된 format을 사용하여 native의 data를 Java 단으로 올리기 위해서 방법을 찾아 보려 했었다...

JNI에서는 기본적인 primitive data type은 변환이 가능하고 간단한 object type으로 mapping이 가능하다. 

Table 3-1 Primitive Types and Native Equivalents

Java Type

Native Type

Description

boolean

jboolean

unsigned 8 bits

byte

jbyte

signed 8 bits

char

jchar

unsigned 16 bits

short

jshort

signed 16 bits

int

jint

signed 32 bits

long

jlong

signed 64 bits

float

jfloat

32 bits

double

jdouble

64 bits

void

void

N/A

 


하지만 하위 모듈들에서 어떤 type을 사용할지 모른다는 제약으로 인해 ...

naive하게 native단과 java단 사이에 전달될 data structure를 단순화 하고 
전달하는 값은 arguments들을 Serialization한 값을 사용하는게 어떨까 했었다..

일단 Serialization 관련 해서 찾아보니 다음과 같다.

C++
- boost serialization library
 . 깔끔 하고 사용하기 편하다. 
 . serialization을 한 값을 보면 꽤나 단순한 형태이다.
 . 다만 boost library의 version에 따른 호환성이 있을 수 도 있지 않을까.... 라는 택도 없는 생각이..

Java
 . 뭐 java야 기본으로 지원하니까...
 . 좀 더 자세히 보자면 : http://javapapers.com/core-java/java-serialization/


Protocol Buffers 
 . Google에서 제공하는 open source
 . protocol을 format에 맞춰 기술하고 사용 언어에 맞춰 이를 컴파일 하여 사용
 . C++, Java, Python을 지원
 . 이종 언어간 (C++, Java, Python) protocol Data를 공유하고자 할 경우
 . Andorid상에서 native library를 사용하여 공유하고자 할 경우 

What Are Protocol Buffers?

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages – Java, C++, or Python.
The download provides the complete source code for the protocol buffer compiler and all the classes your generated code needs, together with building and installation instructions.
 우
SWIG
 . 사실 tutorial만 봐서는 사용할 수 있을지 모르겠다. 공통 interface description을 사용해서  여러 언어를 대상으로 코드를 생성 할 수 있을 것 같은데.... data의 변환 문제는 해결 할 수 없는 다른 범주의  solution 임

XX Library
. 찾다 보니 Java 진영쪽에서 C++ object들을 Java에서 사용할 수 있게끔 해주는 package가 있던데... 아직 완벽한것 같지 않고 제약이 있었던 것 같았다.. 
 . 링크를 다시 못찾아서 이렇게 정리만..



일단 간단하게 하려면 그냥 내가 serialization용 protocol을 만들어서 주고 받고 
공통의 container에 <name, value> 정도의 정보만 제공할 수 있을 것 같다...

좀 더 나아가면 가 종단에서는 Abstract factory를 만들어서 전달하고자 하는 data type에 따란
container object를 생성하되 각 object들은 Serialization interface를 구현하여 
JNI 단에서 전달하는 string을 parsing/serialize를 하는 방법도 있을 수 있겠다.

능력이 그거 밖에 안되네..

좀 심각하게 고민한다면 Protocol buffers를 고려해 볼 만 한데 그렇게 까지 중요한 부분이 아니라서.... 

댓글 없음:

댓글 쓰기