1

So I have a project where I need to create failover between two FIX quote hosts in case of failure.

The FixApplication (IApplication) OnLogout() is nice to hook an event to when a socket is dropped or you are logged out. This is simple. But this only works if the socket connection was successful to start off with.

If you start up the application and the host is down, no method is actually called for this anywhere. Not in IApplication, nor in the socket IInitiator. No error is thrown anywhere either, the QuickFix initiator will simply just silently retry.

I am using a Timer with a callback right now to manually poll every so often and check if Initiator IsLoggedOn() is false. Then triggering a failover.

But this is really clunky and not elegant.

Is there any other event or method I can hook into to receive the notice for socket network failures BEFORE a network connection and session is established successfully?

Thanks!

Timer t = new Timer(5000) 
t.Elapsed += CheckSocketConnected;
private void CheckSocketConnected(object source, ElapsedEventArgs e)
{
     var connected = socketInitiator.IsLoggedOn;
     if (!connected)
     {
         SwitchToAlternateProvider();
     }
}
  • 'If you start up the application and the host is down, no method is actually called for this anywhere.' - how many minutes did you wait? – ThingyWotsit Jun 04 '17 at 13:57
  • Waited 5 minutes. It just retries several times by itself (as you can see from its own debug logs). Have set the logontimeout to a short period, and this just triggers the reset. No actual event is ever called on Initiator or Application – Christopher Andrade Jun 04 '17 at 17:43
  • If the remote host is down how can you expect it to notify you that it is down? – rupweb Jun 04 '17 at 20:44
  • Because this is an event. The SocketInitiator makes connection calls after you initiate a .Start(). When the connection fails or timeouts according to the logonTimeOut settings, then surely it should notify you with an error, or event type message somewhere. Which doesnt seem to be happening. – Christopher Andrade Jun 05 '17 at 07:13
  • When I try it to a dummy socket I see a callback to onCreate then a "java.net.ConnectException: java.net.ConnectException: Connection timed out: no further information (Next retry in 10000 milliseconds)" in the event log but no call up through the interfaces when I expected a ToAdmin callback. – rupweb Jun 05 '17 at 15:25
  • 1
    Rupweb - Yup, exactly my dillema. Nothing else but an event log and a string message entry to go on. – Christopher Andrade Jun 05 '17 at 19:43

1 Answers1

2

Well, after realising the limitation of the QuickFix/N component, I would never receive any feedback from this library if the host was down.

My resolution was to just simply use the following piece of code to check if socket was open before starting the connection in QuickFix/n

bool IsPortOpen(string host, int port, TimeSpan timeout)
{
    try
    {
        using(var client = new TcpClient())
        {
            var result = client.BeginConnect(host, port, null, null);
            var success = result.AsyncWaitHandle.WaitOne(timeout);
            if (!success)
            {
                return false;
            }

            client.EndConnect(result);
        }

    }
    catch
    {
        return false;
    }
    return true;
}