Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any better options than porting from C# to Java?

I have an existing library written in C# which wraps a much lower-level TCP/IP API and exposes messages coming down the wire from a server (proprietary binary protocol) as .NET events. I also provide method calls on an object which handles the complexities of marshalling convenient .NET types (like System.DateTime) down to the binary encodings and fixed-length structures that the API requires (for outgoing messages to the server). There are a fair number of existing applications (both internally and used by third parties) built on top of this .NET library.

Recently, we've been approached by someone who doesn't want to do all the legwork of abstracting the TCP/IP themselves, but their environment is strictly non-Windows (I assume *nix, but I'm not 100% sure), and they've intimated that their ideal would be something callable from Java.

What's the best way to support their requirements, without me having to:

  1. Port the code to Java now (including an unmanaged DLL that we currently P/Invoke into for decompression)
  2. Have to maintain two separate code-bases going forwards (i.e. making the same bug-fixes and feature enhancements twice)

One thing I've considered is to re-write most of the core TCP/IP functionality once into something more cross-platform (C / C++) and then change my .NET library to be a thin layer on top of this (P/Invoke?), and then write a similarly thin Java layer on top of it too (JNI?).

Pros:

  • I mostly spend my time writing things only once.

Cons:

  • Most of the code would now be unmanaged - not the end of the world, but not ideal from a productivity point of view (for me).
  • Longer development time (can't port C# sockets code to C / C++ as quickly as just porting to Java) [How true is this?]
  • At this point, the underlying API is mostly wrapped and the library is very stable, so there's probably not a lot of new development - it might not be that bad to just port the current code to Java and then have to make occasional bug-fixes or expose new fields twice in the future.
  • Potential instability for my existing client applications while the version they're running on changes drastically underneath them. (Off the top of my head I can think of 32/64 bit issues, endianness issues, and general bugs that may crop up during the port, etc.)

Another option I've briefly considered is somehow rigging Mono up to Java, so that I can leverage all of the existing C# code I already have. I'm not too clued up though on how smooth the developer experience will be for the Java developers who have to consume it though. I'm pretty sure that most of the code should run without trouble under Mono (bar the decompression P/Invoke which should probably just be ported to C# anyway).

I'd ideally not like to add another layer of TCP/IP, pipes, etc. between my code and the client Java app if I can help it (so WCF to Java-side WS-DeathStar is probably out). I've never done any serious development with Java, but I take some pride in the fact that the library is currently a piece of cake for a third-party developer to integrate into his application (as long as he's running .NET of course :)), and I'd like to be able to keep that same ease-of-use for any Java developers who want the same experience.

So if anyone has opinions on the 3 options I've proposed (port to Java & maintain twice, port to C and write thin language bindings for .NET and Java or, try and integrate Java and Mono), or any other suggestions I'd love to hear them.

Thanks

Edit: After speaking directly with the developer at the client (i.e. removal of broken telephone AKA Sales Department) the requirements have changed enough that this question no longer applies very well to my immediate situation. However, I'll leave the question open in the hopes that we can generate some more good suggestions.

In my particular case, the client actually runs Windows machines in addition to Solaris (who doesn't these days?) and is happy for us to write an application (Windows Service) on top of the library and provide a much more simplified and smaller TCP/IP API for them to code against. We will translate their simple messages into the format that the downstream system understands, and translate incoming responses back for them to consume, so that they can continue to interface with this downstream system via their Java application.

Getting back to the original scenario after thinking about this for a couple of weeks, I do have a few more comments:

  • A portable C-based library with different language bindings on top would probably be the way to go if you knew up front that you'd need to support multiple languages / platforms.

  • On *nix, can a single process host both a Java runtime and a Mono runtime simultaneously? I know in earlier versions of .NET you couldn't have two different .NET runtimes in the same process, but I believe they've fixed this with .NET 4? If this is possible, how would one communicate between the two? Ideally you'd want something as simple as a static method call and a delegate to raise responses with.

  • If there's no easy direct interface support between Java & Mono (methods & delegates, etc.), one might consider using something like ZeroMQ with Protocol Buffers or Apache Thrift as the message format. This would work in-process, inter-process and over the network because of ZeroMQ's support for different transports.

like image 445
Terence Lewis Avatar asked Apr 16 '11 23:04

Terence Lewis


People also ask

Is there something better than USB-C?

While most devices feature USB-C ports, there are several important reasons why you may prefer Thunderbolt over USB-C. Plus, with the release of Thunderbolt 4, this tech's standards are even higher – and easily surpass those of USB-C.

Is Type-C port better?

Type-C ports can transmit data at a higher rate. 4K videos can be transmitted via a USB 3.1 Type-C port. Type-C ports support relatively larger charging currents ranging from 3 A to 5 A, and support reverse charging. Type-C ports are more refined in structure and more safe in use.

Is C port better than HDMI?

If your laptop has both of those capabilities, USB-C is a convenient choice. Thunderbolt 3 is the fastest connection available on many computers, very few of which have HDMI 2.1 ports yet. For daisy-chaining two 4K monitors or connecting a 5K display, it's pretty much unbeatable.

Are there different Type-C ports?

USB-C ports are now found on all manner of devices, from simple external hard drives to high-end laptops and the latest smartphones. While every USB-C port looks the same, not every one offers the same capabilities. USB-C may now be ubiquitous, but it doesn't serve the same functions everywhere.


3 Answers

Spend more time getting the requirements nailed down before deciding on an implementation. Until you know what is required, you don't have any criteria for choosing between designs.

If it's a non-windows environment, it doesn't make sense to have .NET anywhere in there, for example.

like image 195
Larry Watanabe Avatar answered Nov 05 '22 10:11

Larry Watanabe


If you need something that runs on the Java Virtual Machine but looks a lot like C#, you should check out Stab. This will not help you with P/Invoke and the like but you may find it less work to port your C# code to Java and maintain it.

You should look into Mono though. I expect that all your C# code would run unmodified (except the parts that touch the unmanaged DLL).

I have not used it but jni4net is supposed to allow calling .NET code from Java. If your clients want a Java interface, this may be a solution.

I use Mono on Linux and the Mac all the time even when .NET compatibility is not a priority. I like C# and the .NET libraries and prefer the CLR to the JVM. Mono is MIT/X11 licensed which means that you can use it commercially if you like. Unlike some others, I see no reason to avoid technology championed by Microsoft while favouring technology championed by Oracle and IBM.

Using Mono will not help you with the unmanaged bits, although you can still P/Invoke into a native DLL. You will just have to port that DLL yourself or find some equivalent.

You may also want to look into Mono Ahead of Time compilation.

like image 26
Justin Avatar answered Nov 05 '22 09:11

Justin


Have you considered mono? It would most likely support your existing code in the non-windows environment. The trick would be calling it from java, but the mono folks might have something to help you out there, too.

like image 1
Joel Coehoorn Avatar answered Nov 05 '22 09:11

Joel Coehoorn