Pari has posted 4 posts at DZone. View Full User Profile

Java and C++

03.14.2013
| 4555 views |
  • submit to reddit

Since I started my job as a developer, I spent 99% of my time on Java. I learn't at University C, C++ and lots of others, but didn't work a lot on them.

In my everyday work, I work on lots of different topics, too long to enumerate. One of them is Java and C++ interoperability. I heard lots of things, discussed with lots of people.

With all of that I never heard or saw simple things. I know that already wonderful and powerful framework exist in JAVA and C++.

I had in mind a simple idea that was considered impossible :) So as usual, after a while I gave up convincing and started implementing ...

I have been working on it since January. Some hours a day. You can find it here http://github.com/charb/rpc  It is just a start and lots of things to come.


In few words here is what it does:

  • Java like API with namespace used as Java package and same kind of objects hierarchy
  • Same reflection features once we follow certain rules
  • Same object marshalling and unmarshalling with same API. I already tested some cases and it works with Java
  • A RMI package like. I created it in Java and translated it to C++. I will submit the Java one later

It is far from being finished and I have lots of things in mind.

It is just a simple idea : have Java language feature natively in C++. My aim is not to shock C++ fan :)

For sure I need help :) For contributing, testing, code reviewing, giving ideas, ...

Take a look and please send me your comments.

Here are some code sample:

JServer* server2=new JServer(localEdPt,router2,cc2);
JObject* obj=server2->lookup(remoteEdPt,JIRegistry::getClazz());
cout<<obj->getClass()->getName();
cout.flush();
JProxy* proxy=(JProxy*)obj;
JObject* result=proxy->invoke("list",NULL);

class ServerSocketRunnable : public JRunnable{
        public:
            JServerSocket* server;
            ServerSocketRunnable(string host, int port){
                this->server=new JServerSocket(new JString(host), new JPrimitiveInt(port),new JPrimitiveInt(200));
                server->connect();
            }

            void run(){
                JSocket* socket=server->accept();
                JObjectOutputStream* oos=new JObjectOutputStream(socket->getOutputStream());
                JObjectInputStream* ois=new JObjectInputStream(socket->getInputStream());
                while (true){
                    cout<<"before reading \r\n";
                    cout.flush();
                    JObject* obj=ois->readObject();
                    cout<<obj->toString();
                    cout.flush();
                    cout<<"after reading \r\n";
                    cout.flush();

                    cout<<"before writing \r\n";
                    cout.flush();
                    oos->writeObject(obj);
                    cout<<"after writing \r\n";
                    cout.flush();
                    //JThread::sleep(1000);
                }
            }

            ~ServerSocketRunnable(){
            }
        };



JInteger* s=new JInteger(123456);
assert (s->getClass()->getName()=="java.lang.Integer");
JField* fvalue=s->getClass()->getField("value");
assert (((JPrimitiveInt*)fvalue->get(s))->get()==123456);




Published at DZone with permission of its author, Pari Charb.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)