Exchange Object/Class data between Java and C++ - java

I need to pass data between c++ program and a Java GUI that is showing that data. I can put that data in a class but the c++ program could be running on linux(raspberry pie) and java may or may not be on windows. What options do i have?
Kindly help me for same machine processes and also if they are on different machines.
P.S.
On different machines internet connection is available.

You may want to implement some serialization.
I suggest using a simple textual serialization format like JSON (but you might consider also YAML or even XML). There are many JSON libraries available, like jansson (in C), JsonCpp (in C++) and several for Java.
Of course, you need some form of Inter-Process Communication. This can be sockets or pipes. Read e.g. Advanced Linux Programming or some other tutorial. Maybe have some Event Loop (e.g. libev, libevent) or even use JSON-RPC (or perhaps some HTTP server library)
You could use binary serialization like XDR or using libs11n but it is usually not worth the trouble.

Related

How to pass some data from python3 to java

Here is my problem: I need to create a discord bot that uses speech recognition to recognize voice commands and send the recognized strings to a Java program (a Spigot Minecraft plugin in this case) from that bot/python program. I've scanned all over and I couldn't find any suitable methods to do this. The only thing that I can think of is creating a file with the necessary data inside it or its name and use that as a "bridge" between the bot and the plugin, but I don't think it's a very orthodox or suitable method. (I tried jython, but it's stuck to python2.7 as far as I know).
I assume we are talking about separate Python and Java processes since you found Jython to be not useful for your case. I think there are really two questions in this: (a) how to get data from Python to Java and (b) what format to use.
For (a) you could use a named pipe or domain sockets (I believe they now also exist on Windows). For the differences between these two inter-process communication abstractions see here. Another option would be TCP but unless there is a possible scenario where the processes reside on different machines this is not likely to be the best.
The answer to (b) depends a bit on the nature of the data. JSON could be an option but you might also want to look at alternatives that might be faster. The ones that come to my mind are Apache Thrift and Google's Protocol Buffers.

Java - .Net object interchange, not web-based

I have a client-server system implemented in C#, and the client and server exchange .Net objects via serialization / deserialization and communicating via TCP/IP. This runs on a local network, it is not web-based or Internet-based.
Now I want to include Android clients connected by wifi. Again, this is local network only, not via the Internet and not web-based. The Android programming will be in Java. (I am aware of Mono for Android, but prefer not to get into that now.)
Is there some fairly simple way to implement object to object interchange between Java and .Net objects, provided, of course, that they are compatible?
I've looked a bit at JSON (Jackson on the Java end and Json.Net on the .Net end), and I'm guessing it can probably be done, but only with major efforts on remapping things at each end as soon as the objects become fairly complicated.
Any other suggestions? JSON-based or otherwise?
PS. My question is somewhat related to this one Mapping tool for converting Java's JSON to/from C#, but it never got a suitable answer, perhaps due to insufficient info in the question. Also, I don't care whether I end up using a JSON-based transport or XML or something else.
I would suggest either JSON or XML (which is based on a .xsd file) because these are independent of their respective implementations (instead of something like an ObjectOutputStream in java).
The problem of having this format between the two components (client and server) is that they need to be at the same version. My best practice is to have one underlying definition of the format (i use xml with an xsd file which specifies how the xml has to look like), then use jaxb to generated java classes. That way you can (un)marshal from/to xml in the java part.
I am very sure a similar thing exists in the world of .NET.
JSON is smaller than xml in size, i find xml to be more readable.
SO user "default locale" should get the honor for this, but he/she has only answered via a comment. So just to make it very clear what my choice was I'll answer my own question.
I've decided to go with Google Protocol Buffers, which in my opinion has much better support for moving objects back and forth between Java and .Net than JSON. Because I have a lot of experience with C#, and a lot of existing C#-defined classes, I've selected Marc Gravell's protobuf-net program for the .Net end, and Google's own support for the Android end (no - see edit). This implies that I'm defining the objects in C#, not in .proto files - protobuf-net generates the .proto files from which I then generate the Java code.
Incidentally, as the transport mechanism I'm using a little-known program called naga on the Android end. http://code.google.com/p/naga/ Naga seems to work fine, and is well-documented and has sample programs, and should be better known in my opinion.
EDIT:
OK, I've got it working now to my satisfaction. Here's what I'm using:
Google Protocol buffers as the interchange format: https://developers.google.com/protocol-buffers/
Marc Gravell's protobuf-net at the C# end: http://code.google.com/p/protobuf-net/
A program called called protostuff at the Java end: http://code.google.com/p/protostuff/
(I prefer protostuff to the official Google Java implementation of protocol buffers due to Google's implementation being based on the Java objects being immutable.)
Actually, I'm not using pure protocol buffers as the interchange format - I prefix the data with the name of the (outermost) class being transmitted. This makes the data self-identifying for deserializing at the other end.
You can also try wox (https://github.com/codelion/wox), it is a cross platform serialization library for Java and C# based on XML.

How to write a script that's parseable in both Java and C++

We've got Java and C++ implementations of our product, which is a distributed messaging system.
I'm writing a framework to run a system test across multiple servers. I need our "test coordinator" process to send instructions to each server telling it what to do (e.g. send 100 sample messages, or wait for a message, etc).
I'd really like to implement this by sending a script to each server containing its instructions. This allows me to create dumb test servers, with all the intelligence embedded in the test instructions.
If all my servers were Java I'd write this in Groovy or a similar language. But I'd like our C++ implementation to parse the same script. I'm wondering if I could execute Javascript to call Java/C++ classes to send messages etc.
Is there a neat way of doing this?
If all else fails I'll create an XML format to declaratively contain the test parameters (rather than imperatively containing the test instructions). But this would require the test servers to contain more test-specific intelligence than I'd like.
Any advice gratefully received.
Maybe have a look at LUA. It is well supported in C++ and it seems like there is support for Java as well (see this question)
There are JavaScript parsers for C++ and Java available, so that's definitely something you could use; I don't think that it's a good idea however: the Javascript code can of course interact with your Java code and your C++ code; the problem is, if you want your Javascript code to be ignorant of whether it runs on the C++ or Java basis, it doesn't actually make much sense to call Java/C++ from it, since these calls are again language- (and even engine-)specific.
The simpler solution might be to use a declarative format as you say, but I wouldn't go for XML unless the data needs to be exchanged with a broader audience; instead I'd use e.g. JSON, which is supported very nicely on Java and C++
The best solution (albeit probably also the one involving the highest effort to develop) is probably to develop your own DSL (domain specific language), and parsers for it in both C++ and Java.
This is the kind of inter-operability WSDL was originally designed to facilitate. I'm not sure why you'd want to invent an XML format to do what SOAP already does. I mean, you could, but it'd be a maintenance nightmare for the next guy. Plus, introducing another programming language (which is client side, unless you want to add NodeJS into the mix) to act as a glue layer between the two increases the complexity of the system.
What I would do is:
define a high level language agnostic interface
create implementations for the interface in Java and C++
If you're sending commands to the servers, it may be appropriate to look into using the Command pattern to encapsulate the set of known commands (which I assume is enumerated in your requirements). You can use Batch Commands using Composite.
Then your "Test Coordinator" would build a batch command, send it to the server of choice using the declared service, and your implementations could process those commands in language specific ways. Depending on the types of commands your system has, it may be appropriate to have each implementation delegate to Ant or Make.
You could consider using CORBA too... :)

Best way to send "messages" from PHP to Java on the same workstation

What is the best way to send "messages" from PHP script to Java program in real time. PHP script and Java programs are both working at the same work station with OS Windows. Maybe some kind of client/server? The main feature is real time; that's why I don't want to use files.
PS: I'm going to send logger messages (php) and display (java) them at OS system tray tooltip.
PPS: I'm real noob in Java; it will be my first Java program. :)
Thank you.
You could use sockets (probably UDP, but depends on your needs). This way, if in the future you will need to put scripts and Java programs on different machines, you'll be able to do that without modifing the code.
In addition, once you established a communication protocol between client and server, this solution is language independent. So it's easy switch from PHP to another scripting language (the same for Java).
This depend on how heavy weight your application is.
If it is your first program and it is just a little project, a possibility is to open a socket on the server, connect to it with a client and send the data as a string, make your php program the client and java program the server.
Their are things that you can borrow to avoid doing everything on the low level. But they will add weight to your program, for example using a JSON/XML parser to serialize(make the messages into bytes readable on both side) the message instead of using your own format.
Or, use a framework like JAX-RS to quickly and easily (for people familar with it, you may need some time to understand it because it is quite different from writing plain java program) to build a little web service like professionals would do.
Possibilities are:
Send your data as a POST to jsp page.
Make your java code read your php logs.
use queuing systems like RabbitMQ, AciveMQ, Redis etc.
For simplicity use a database table as exchange medium.
It is also easier to debug.
(It is asynchrone, one side, PHP or Java, may be down. Performance is fast, as DB-Server will keep as much in memory.)

Serialised communication between languages

My universities peer to peer communication course uses an in house client/server program for demonstration and (i think) extending it is part of the assessment. The program we use is written in java and uses serialisation for the network communication.
To get a better grip I want to try reimplementing the protocol used in objective c, but googling around I cant find any information on using serialised data between languages. I would like to keep this as simple as possible, ideally be able to drop my replacement server/client onto a network and have it behave.
Edit Didnt actually ask a question there.
Is it possible to communicate between the two serialised formats, How can I make this work without reverse engineering the format java uses.
I would recommend avoiding writing (de)serialization support of java's native serialization in another language.
If you can change the existing Java server and clients, use a more language agnostic serialization format.
Assuming that you are not allowed to make that sort of change, I would define the new protocol, and implement a bridge in Java. The bridge (process) would establish a connection on behalf of each client that connects to it, and translate messages between the Java serialized and language agnostic form. This will provide a good migration strategy.
Java serialization protocol (if it's built-in default Java serialization) is documented, so you won't have to reverse engineer it - check this article and this link. However, if you can, use JSON, XML or XML-RPC; it will be much simpler than creating Java serializer/deserializer in another language.

Categories