Alvin Parker

Ranch Hand
+ Follow
since Oct 01, 2013
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Alvin Parker

I hope I'm in the right forum.

I need to refactor an application that currently runs on the desktop (Win/Mac) and iOS. For the iOS part, I'm leaning towards just building it native in Objective C or Swift. For the desktop, I want to use Java so that I can have a single codebase for those systems. But, I have not used Java's UI since javax.swing.

Is Java the right tool for building the UI? Since this is a desktop app, I don't want to use JS as the front end. The current app is built in ActionScript. I'm moving out of AS due to the fact that this app has a large video component. To make it work with AS, I had to convert all MP4's to individual PNG's and blit them. Using the AS video functions, the user could not scrub video to frames they expected to scrub to. Now that it blits, it is precise and works great other than the fact that my videos are huge and it takes a long time to get them from/post them to the server. Still, I have a (mostly) working solution, but the goal of going with AS was to have one platform for iOS and desktop. Then came another problem. Features that work well on the desktop don't work very well in mobile so I have had to essentially split the codebase and make two versions of the app anyway. And, given that I'm blitting, iOS devices run out of memory very quickly (imagine just 10 seconds of video at 30 FPS and how many PNG's that is). Given that I have to have two code bases and the limitations I'm faced with, I may as well do all this native (or as native as possible where Java is concerned) and gain the optimizations of each platform's native features.

With AS, the UI is great and fairly easy to implement and maintain. I make the art, display it on the screen and can skin very easily. But, I've got no clue how to do all this with Java and what Java framework would be best for me to use to try to keep the same architectural strategy in place. Can someone give me some advice and/or point me to resources to help me make a defensible decision to use Java as the UI for the desktop version of my application? Is there a great example of an app that uses Java as the UI but that also does not look like it is using Java as the UI that I may be able to learn from?
10 years ago
Thank you very much Steve.

Since my objects do have two members related to a pool of immutable values (IPAddress and Port), I can compare on those members and safely know that the key I got from the Map is in fact the key I'm looking for (or override the .equals method as you state) so I will do that instead of just relying on the ConcurrentMap to know what I'm asking for.
11 years ago
I have a general question about Object.equals.

Let's say that I have a client Socket that sends an object. I read that object using InputStream.readObject();

Now I put that object in a Map as the Map's key. Later the same client outputs the object again but has changed a class member and the server reads the object in again. Will I be able to retrieve the value from the Map by simply passing in the newly read object (Map.get(secondTimeIReadObject)) or do I need to do something more sophisticated in my code?

I know I can test this myself and I will if I get no responses. I'm simply in a hurry to get something done for a customer and so thought I would ask here in case someone knows immediately.
11 years ago
I am not sure why this belongs in this forum rather than the one I started in. I have a problem with files suddenly stopping writing which I don't think has anything to do with the socket. The socket hanging is only because the write outs are not working and they only stop for the one client who is having write out problems. In fact, given the flow I describe, and the fact that I can see the socket keep talking AFTER the first write out hangs but then stop once it reaches the next write out of the next socket means that it is clearly hanging on file output. If anyone can help me with this, in any forum, I would be delighted. Thanks so much.

I am going to migrate to using an external script to get the server instances running. I will follow up here if simply having scripts start the JVM suddenly cures the problem (vs having Process start new JVM instances).
Thanks to anyone who can provide me some insight on how to fix this.

When I run the process described below on my dedicated Ubuntu server, all works as I expect. However, once I transferred to the Amazon EC2, the speeds got a lot faster but suddenly my code broke. Files that were writing out fine on the other machines I tested on don't write reliably on the EC2 server.

I created a load balance server which queues a client connection and gets a new socket started (in a new JVM instance) and waits for that socket to call back notifying that it is ready. Once ready, the new socket's endpoint is written out to the client, the client disconnects from the load balancer, connects to the new ServerSockt and starts streaming to it. When it is done, the socket calls back to the load balancer, the load balancer adds that endpoint back to the pool and the JVM associated with that endpoint closes.

For each item the client is uploading (and there are a series of artifacts of varying sizes), the above process repeats until I have a number of files in a directory (e.g. fileId.aextension, fileId.anotherextesion). Only one Thread (one JVM) can write to a file. However, several may be open writing to the same directory at the same time (or very, very close to the same time).

Once all artifacts for a particular topic are uploaded, the client may have more topics to upload and so the process above may be triggered to start anew as soon as the last artifact for the last topic has been received.

Here is the code that does the writing:



When it hits this part of the code, it works fine for a while but then eventually just stops writing files. While the JVM's open and close predictably throughout the uploads, eventually one will just not close (I suspect it has a file in its buffer but can't flush it). However, since it got all the bytes from the client, it tells the client to continue on. The client does so. A new ServerSocket then opens and it too won't close and at the end of that upload, it never responds to the client. Instead, it gets to the handler that causes the code above to run but then just goes silent. Once I kill the Java processes, the files write out.

No errors are throws because, I suspect, no errors are generated. It seems like this may be due to the speed of writes to the directory. I have tried putting the Threads to sleep to no avail. Does anyone have any thoughts or suggestions on how to make this work the same on EC2 as it does on every other computer I run it on?
Thanks Jesper for the honest feedback and warning.

It is a commercial client. And, I do want to make sure that what I leave them with does not make future developers want to shoot me. I am also interested in saving myself time and see no reason to learn something that does not rapidly add to my ability to achieve results for myself and clients. From continued research on this, it appears that there are extensive solutions for Java to communicate with native libraries, beyond JNI and there may be several languages that can help me accomplish goals. So my question remains... Knowing that I'm an entrenched Java programmer, what would be a good language to learn for those times when I need to do something natively that Java just can't do - and by native, I mean specifically desktop development? I know this is a subjective question. I am interested in others subjective responses. I don't care if I have to connect with JNI, RPC or JNA, I just want a language that is friendly to a Java programmer and has some way to accomplish direct Java instantiation and consumption.

EDIT:
The native part used to be in Java. It's based on the javax.sound packages. The program processes MIDI data. The problem we found is that on many systems the MIDI cable is not found or suddenly gets lost. Changing to a native solution fixed the problem. There were also sound drops during accompaniment - sound would play for some period of time, drop out and then pick back up in the middle of the accompaniment. I fixed that as well by sending MIDI signals myself and skipping javax.sound. But all of this requires native work and I don't like having solutions in place I don't understand / have a hard time supporting. There are other reasons I need to do native programming from time to time and I'd like to have a language I know fits the bill when it is needed.
11 years ago
My motivation for this question is that I have to support a client with a large Java / ActionScript implementation (solution runs on win and mac). To solve part of a problem they had, I needed to introduce some native software. I found a C++ package that had a JNI wrapper around it and used that. But, it's out of date, there's no support for it and I don't know C++ beyond a rudimentary level. I am considering learning another uninterpreted language and re-writing the native source in that due to the fact that I think it would be far slower to become proficient in C++ than other languages that may exist and given the dwindling C++ resources (even with C++11), I think I would be better off learning something growing rather than declining.

I have heard of Haskell and understand that's a natural language to learn. It also appears that there is some support for doing JNI type things with it. Has anyone faced this issue in the past? What languages have you found to be able to produce an artifact that Java can then call? I'm looking for suggestions on languages that I could learn that would be comfortable (and relatively fast) to learn and that will produce .dll or .dylib (or any other native artifact) that can be accessed within Java.

Thanks for the input.
11 years ago
Have you looked at how much memory your program is consuming? If you have a lot of system memory available and the JSON object is not larger than the memory you have left over, why not just add your compiler arguments (something like this would increase max memory to 8 GB if you have it 'java -Xmx8G -cp PATH_TO_CLASSES com.yourpackage.YourJSONObject ')? If you are running via Eclipse or some IDE, you can go into your project properties and add the -Xmx compiler argument so you have more memory for testing. If you can't do any of that, you must have a truly massive JSON file in which case you might have to look for a strategy where you can serialize the bytes and make smaller JSON objects perhaps with an object that maps them together. But your easiest solution seems to be just to try the compiler args.

Also, the "code getting stuck" is not enough info to tell you what is wrong with that line. If it's a really big file, you would expect it to take a while to process the URL InputStream, right? If the program is freezing up with no errors being shown (and no out of memory), I've solved issues in the past like that by adding timeouts to my read functions. Essentially, I put it in a Thread and have the Thread sleep for 10 MS once it process some buffer. For instance, save 1 MB of byte[] sleep for 10 MS, save 1 MB sleep for 10 MS... until done.
11 years ago
Thanks for the information! It makes sense that the system would not bother to spend resources running if memory is allocated that does not need to be reclaimed. It just struck me as something to look out for, especially when I closed the JVM and see all the memory get dumped. I am going to put on the memory profiler. Thanks for an easy suggestion on that. I just put together a new system and need to get all these tools back on. Having a simple to set up profiler is going to be useful.
11 years ago
I have written a multi-threaded socket server, relying on the Executors framework. The way I structured the program where threads are concerned is to have services that run and when they finish, they cause a callback to happen (which often causes some other service to run). Because the observer does go through it's wired-together flow, I am sure all my Runnables are completing. During the course of handling requests, I set all members that are not used by other objects to null and then I have a clean up process to set everything to null when the client is done communicating.

This socket is for pushing pixels around so the memory use is fairly intensive. When I review the memory usage of the socket, this is what I find...

- My computer is consuming 5 GB of RAM when the socket starts (I have 32 GB on this one).
- Once the first client connects and messages begin going back and forth, RAM in use will quickly grow to 9 GB (we started with 5 so 4 GB for the socket).
- Once at 9 GB, GC seems to start running as subsequent calls will cause the memory usage to grow to about 9.5 GB but I never see it get above that.
- If I close the server, the 4 GB of allocated memory is suddenly free.

I am using a lot of ByteArrayInput and Output streams in this server and I am careful to de-allocate their buffers and any byte[] that happen to be locally scoped. I've gone through my services and I think I've got all my null references in place. And, given that the memory peaks and the system keeps working without consuming more memory, I'm guessing that my attempts to free memory are working. Why though would the memory not go back down to 5 after the first client is done? Does the garbage collector run only when memory is needed or does it run at some generally fixed rate and reclaim memory that is no longer in use? Does it not bother to run and reclaim memory if nothing is going on? I even do a call to System.gc() at the end of the client messaging processes but that has no affect on memory being reclaimed.

Seeing this memory never drop after it gets allocated gives me pause.

11 years ago
Given that this has sat here for a few days, I'm guessing my question is too vague or that I'm trying to do something that is rarely needed to be done by programmers. Is there anyone who knows of a tutorial or resources I can find that will help me take existing C++ code, make a DLL useable by JNI from it and explain how to use the compiler flags with MinGW (or VC++ if that's easier)? Alternatively, is there anyone who can give me just the command line args I would need to run MinGW to produce the artifact I'm aiming for?
11 years ago
I need to construct a dynamic link library from existing C++ code. My understanding of C++ is rudimentary. I have, however, used JNI in the past to communicate with DLL's but they've always been DLL's provided to me by the vendor with well-documented API's so writing a wrapper and getting going has been easy.

Please keep in mind that I cannot use Runtime.exe() for this (at least I don't see how I could). The C++ code takes an observer which it notifies of messages when it has them. If there's some way to build the system so that I can use Runtime.exe, I'm open to that and feel that would be a better cross-platform solution anyway if I can get away with it. However, the target system that will use the DLL has already been built and it does work with an old DLL that I was able to secure for a previous version of the C++ code. Still, I'd be happy to modify the Java side if it means getting a better and quicker solution from the C++ side.

The C++ source is from an open source project (rtmidi). I know that Java has MIDI packages. We have found javax.sound to be unreliable when it comes to many people's systems - sound drops out when students are playing along with our accompaniments and MIDI cables get lost and / or cannot be found at all. I was able to use an old version of the rtmidi packages (someone made a wrapper for them for the old version) and prove that none of these problems exist with this package. However, there's a memory leak problem in the old C++ code, they've reported it as fixed but the project is not well supported and the build files are for the old version and don't work when I simply change the C++ source and update the directory name in the build scripts. I now need to make new build files and have no clue how to get a DLL out.

The instructions for building on Windows are to add the pre-compiler flags as such :

1. "__WINDOWS_MM__". This flag tells the C++ program which API to load when you call it. So this flag needs to be somewhere in my build command such that the DLL will automatically send this flag when I invoke "new" on the RtMidi.
2. "winmm.lib" is a dependency I think. However, I am not sure if this is needed if you're not using Visual C++. I would prefer to use MinGW if I can.
3. "multithreaded"

That's about the extent of the instructions they give. The rest just says "compiler specific".

So... can someone please tell me what I need to do (or resources I need to go study) to make a DLL that I can then call with JNI to invoke the DLL and C++ code?

My directory structure is /rtmidi-2.0.1 which has the RtMidi.h, RtError.h and RtMidi.cpp in it. There is an old MS Visual Studio build script for version 1.0 (not 2.0.1) provided but it won't make a DLL (but will compile) and my attempts at doing so have all failed. Can someone help me understand how to create this DLL in such a way that I can then write my JNI wrapper and be on my way, please? If I need to provide more info, please do let me know and thanks so much for your help!
11 years ago
Thanks Jeanne. I can see how DI can make testing and extending easier. I appreciate your feedback.
11 years ago
I am doing my best to write better software. Having been a long-time Java programmer and then learning ActionScript, I've come to appreciate some of the workings of ActionScript. I'm a self-taught programmer (about 13 years ago I learned not counting BASIC in high school). AS quickly taught me what async means and was a shock having worked in Java.

Now I've been into AS for a number of years and there are some lessons I have learned from it that I have applied to my latest server-side workings. I want to be able to identify and learn design patterns better. I want to know when I should use them and when I should not (or when better alternatives are available). If any are inclined to help me, I'd like to post what I did for this latest project and to get feedback on if I could do it better and what is right and wrong about my design. Here are the bullets.

1. The program has an entry point which starts a Thread pool and a ServerSocket.
2. Client connects to ServerSocket - ServerSocket gets a Thread and starts "MainService"
3. One MainService exists for each Client that connects. MainService is an IService:



4. A single handler is created by MainService. That handler is MainServiceHandler and it's an IServiceHandler



5. MainServiceHandler is sort of a "super handler" in that it has a reference to every kind of handler (in a Map) that is needed for my implementation. In each IServiceHandler's handleMessage() I implement any custom flow needed and then invoke the service.
6. When the service is done running, the MainService is notified. It in turn simply hands the message off to MainServiceHandler which causes some other handler to fire which causes some other IService to run and so forth until the workflow has been completed.

By designing in this way, I feel I've created a fully async flow. In each of my IService.startService() methods, I get a Thread from the Thread pool and then have it execute the IService runnable in question. Nothing moves forward until that Thread is done.

There are awkward parts to my design, I'm sure. I could have made another layer of abstraction and added IServiceHandlers to that object, for instance. But, in general, is my design sensible? I did it this way for a few reasons :

1. I can report where I am in process with this flow so I can feedback to the client as to percent done.
2. I have created a flow graph so to speak. If I have to modify anything, it will be contained to three objects - the MainServiceHandler, the IServiceHandler and IService in question. MainServiceHandler pretty well describes the entire implementation.
3. It should be obvious and simple to future developers how to extend the system with more services - even though it's probably a more verbose process than it has to be.
4. I can be sure that some long running processes complete before the system moves on.
5. I should never have deadlock issues as there's only one Thread in service (per client) per IService and they run iteratively.

Besides helping me understand if there's a better way to accomplish software construction, I have a question about frameworks as well.

For the first time, I integrated Guice into this process. I was excited to use it and I see some benefits to it. However, it seems Guice primarily gives you a centralized Object map and makes code really verbose and take longer to develop. Given that all of my objects, other than the IServiceHandler implementations don't take dependencies in their constructors, does it make any sense for a framework like Guice to be introduced to the process I describe above? What do I gain by this?



Versus this :



Thanks for entertaining my questions.
11 years ago
Great suggestions. I'm happy to report I got it working and I learned a few things from your posts as well.

I don't work for Rocksmith, but have a You Rock MIDI guitar. If you want to learn how to play guitar (in beta) or piano (or drums) and you have a MIDI cable and a MIDI instrument, I can help you out.

I solved this by using an Observer. Thee's a little more latency (notes were at 1 MS before and now are up to 5 MS apart). But now everyone is playing nice.

Thanks so much!