Thursday, October 27, 2011

Management Tool For Windows Service

Background
I've created a Windows service that exposes a TCP socket to wireless clients that access it over (right now) WiFi, Edge, and 3G connections. The basic purpose of the service is to collect a user identifier from the client, process certain validations against it, and return the result back to the wireless client. The requirement is that appear to be instantaneous to the end user. That goal has been accomplished. On average, the round trip is under half a second. That appears immediate enough to the end user. There are occasional lags and timeouts, but there's nothing that can be done about that due to the fact that the wireless networks have their own stability issues. If the connection drops, too bad. Some devices hold a backup database which allows a fail over in the event of a server timeout, and this helps to hide the occasional server issue. A client side backup is not an option on all devices.
Problem
The data that the server uses to validate client requests is stored in a cache that lives in the memory space of the Windows service, so there's no easy way to see if the cache is being updated, completely built, missing items, or to look at the state of specific items. There is logging on the server, but there has to be the balance of logging issues and providing diagnostic information that can give details of the current state of the server.
Solution
We could go log file crazy, writing a bunch of various logs that serve different purposes, but that would very quickly get messy. The code would be more complex than necessary, provide additional overhead to the service, and create more potential points of failure in the service. A better option is to create a simple UI client application that the customer can use to ask the service for its current state, so that's what I'm doing.
I've created a WCF service endpoint using named pipes in the Windows service. This service end point exposes a handful of useful methods for gathering information, and running simple tasks against, the cache and processing stored in the Windows service. The UI client can then call methods exposed on the WCF service, and provide data that the customer would like to review. Extending the functionality of the WCF service is simple enough, although it does require a redeployment of the Windows service, and that is not always convenient in a production server.

Friday, October 7, 2011

Forcing Blackberry socket timeout

Maybe if I talk it through I can come up with a better solution because I don't like what I'm doing, but right now, I have nothing better.

Here's the situation.
I've written an application for a Blackberry. This application requires a socket connection to communicate with a remote resource. It works fine, but once in a while I'm told that the application fails to contact the remote server. It throws a connection timeout error, and I'm told that it takes 15 seconds for this to occur. This is not acceptable to the client, and they are requiring that I drive the timeout down to 3 seconds. My research has shown that there is a connection object that will allow me to set a connection timeout, but in order to use this object I have to utilize the BES server. This is not an option, so as far as I can tell, I'm stuck not being able to set a timeout on the connection.

Here's my solution.
I've created a thread object that wraps the process of connecting, sets any exceptions, and sets a boolean variable indicating that the connection attempt is complete. If there's no exception I can assume that a complete connection attempt was successful, and go about my business.
I start the thread.
I then go in to a loop that will loop for 3 seconds or until my boolean variable indicates that my connection is complete. In the loop I sleep for a little bit, and then check again.
I'm not happy with this solution. It feels like there's too much going on with this solution, but I'm just not really sure what else I can do to force the socket to not try to connect for more than 3 seconds.

// something like this.
private static void openConnection(final String url) throws Exception
{
    boolean connecting = true;
    Exception exception = null;

    new Thread()
    {
        public void run()
        {
            try
            {
                _connection = (StreamConnection)Connector.open(url);
            }
            catch(Exception e)
            {
                 _exception = e;
            }
            finally
            {
                 connecting = false;
            }
        }
    }.run();
    long timeout = getTime() + _threadTimeout; // 3000 ms added to the current time
    while(connecting || getTime() < timeout)
    {
        Thread.sleep(50);
    }
    if(null != exception)
    {
        throw exception;
    }
}

Friday, March 11, 2011

Complex objects with Ksoap2

My brain is fried, I won't even try to post sample code. You can find it HERE
He has supplied a very straightforward tutorial, that is clear, easy to follow, and very understandable. Good job! One major problem, though, I had a problem similar to many others. Although I could send an object to my .net web service and call my web method, when my object arrived at the web service destination it was null. I've spent the last several hours fighting with this, and I've had zero luck until it struck me.
Android code snippet:

MyComplexObject myObject = new MyComplexObject();
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setName("myObject");
propertyInfo.setValue(myObject);

.Net code:
[WebMethod]
public bool MyMethod(MyComplexObject entity)
{
}

Are you seeing the problem? Neither did I, because parameter names aren't supposed to matter, it's the underlying type that matters. I'll spell it out. In my java code I called my object "myObject" in my .net code I called my parameter "entity." As soon as I changed my parameter names so that both in the java code and the .Net code my instance/parameter was named "entity" I suddenly got my object deserializing in my web server! I don't know if this will solve your problem or not. KSoap2 seems to be a beast, and frankly if I can make it work, that will be enough for me without having to understand it for a while, but give it a shot. You may find that you get some nice .Net web service love.
Now, off to find out why I'm getting an org.ksoap2.serialization.SoapPrimitive exception when I get my response back from the server.
Good luck!

Wednesday, January 5, 2011

The app is complete...or is it?

So, I've finished my first app. Actually, it was a few weeks ago. I had it up on the web for anyone who happened to stumble by to download and install. I have my little ads in there, and it all seems to work. I let it bake on a couple of phones with a little testing. I saw one crash, which actually had to do with memory issues on one of the phones, life was good. I then built and signed my release .apk, and put that version up. I verified that I had the signing right, and the app still worked. I let it bake a little longer, did some more testing, and life was still good. It was time to take the big step, give The Google my $25, and become an official android developer so I could upload. Life was good.

I went in to try to upload, and I get a message telling me that the attribute android:name is not a string value. I've been fighting with this in my free time for about a week now, and so far, no love. Don't bother telling me that there's something wrong with my AndroidManifest.xml. I will respond with a "no shit, I've already figured out that is probably the case!" and then I may throw things at you. I'm still going over my files, and reaching out trying to get help with this. Rest assured that I will post when I find out what is actually wrong with my app.


UPDATE
After fighting with this I realized an additional issue. My app wouldn't run on anything above android ver 1.6. In spite of the extremely useful "Maybe there's something wrong with your androidManifest.xml file" answers...(roughly 1 millisecond of googling confirmed that for me, thank you very much, you mental giants)...I got fed up, and decided to rewrite the app from the ground up. Literally, line by line, from the ground up. I tested along the way, and had no issues, right up until putting in the elements to an in my androidManifest.xml, and there was the issue. The name attribute on my tag was a string resource. Doesn't sound like an issue to me, and in fact the google docs say to use a string resource. I was set up correctly, but it didn't work. I hard coded the value of the name attribute, and the problem vanished. That's it. The end.
I don't know why, but it didn't like using a string resource in the name attribute value.

Monday, December 20, 2010

Blackberry General Socket Exception and TCP sockets

Switching gears for a minute to the hell that is Blackberry development.
The situation is this.
I need to perform a TCP socket communication from a Blackberry Bold 9700 to a socket on a server that I have set up to listen for the requests. I won't go in to how to do that, there's plenty of documentation elsewhere. The problem is this: My app makes these socket calls very quickly, and consistently at 31 calls to the socket client my app fails with a "General Socket Exception." My app is then in a state where I have two choices. Shut down the device and restart, or let it sit for several minutes before attempting to make the socket call again. Neither option is acceptable. I've made attempts to ensure that my objects are closed and set to null. I've made every effort to ensure I don't have any leaks in my socket client.
There are alot of people running in to this, but nobody giving any real resolution. After fighting with this for a while, I've resolved my problem. Maybe it will help you with yours. As it turns out, my calls are happening too quickly for the Blackberry garbage collection to take out the trash. I'm using up my resources, and failing future calls. In order to get around this I've made use of the System.gc() method. Clean, simple, and the only solution that I've found that works. It cleans up the resources before I have a chance to use em all up, and allows my app to continue making the calls without issues.

Edit 10/7/2011
So, it's been forever, and the answer is above, but I thought I'd follow up. After getting with Blackberry engineers, asking for help, the only response I got was documentation on how to find memory leaks. Not helpful, I know where the leak is, I just don't know what to do about it. What ever, it appears that my test was much higher stress than actual production use. We have yet to encounter the issue in production use, and in fact, have had to make other adjustments that will slow the app even further. 

Friday, November 26, 2010

Libraries

Android Library
Goal: To create an Android application that essentially serves as a base application. It contains default values, it creates a standard database, it contains standard activities. Child applications will use the base application as is, only overriding strings and display settings. Finally, since I am still rocking a G1, it has to be built on 1.6.
1) As it happens, library applications are supported on Android 1.6, but you must be upgraded to release 3 in order to make that work. I fought with that one for about an hour before thinking to check the versions on my SDK. That’s an hour of creative swear words that I’ll never get back.
2) Create the base application, and make sure it runs. Check.
3) In Eclipse, go in to the project properties, select Android, and check the “library” checkbox. Run the application again, and make sure it still works. I’m pretty sure it worked, but I’ve had a glass or two of wine, so let’s try again just to be sure. Yup, we’re good.
4) Create the first extension application, under properties, select Android, and choose the library option and add the base application. Launch the application. Expected behavior would be to launch the default activity, but override the application string name. Could it be that easy? Of course not. From this point, open the AndroidManifest.xml for the child application. Under the element that points to the activity in the library application, add




From this point, I have my application launching the Activity from my library application, but using the string resources from my child application.

Now, back to XML and SQLite.

so here we go

I'm a C# developer who has had an interest in Android almost since the beginning, but I finally jumped on board as a consumer almost 2 years ago when I got my hands on a G1. From there I tossed around the idea of developing for it, but I didn't actually start until I got stuck doing a Blackberry project for work. That got me motivated to crack open the Android SDK at home, and actually write some code. I'm new to Android, and new to Java, so I imagine much of what I write about will be obvious to people who actually know what they're doing. That's ok. I'm fine with that.