tags:

views:

148

answers:

1

Hi all,

As writen in my title,

background: I have 2 different type of applications (WPF-silverlight) which can talk each other - Doctor application and Patient application - but it doesn't mean only 2 apps will run, for instance: i can run 3 doctor app and 7 patient app. and all of those apps is communicate using wcf via tcp connection. the communication is real time (like messenger app)

flow everytime there's an app online(run) i registering its connection at wcf, because i need to let the other apps know (real time) there's new client connected or there's new client disconnected.

problem: it's fine to let the other app know that there;s incoming app/client, but my problem is, how to let the other app know if this client was disconnected,

it's fine if the user close the application properly (eg. click the close button) - so in wpf, i can call the wcf to unregister the connection,

but how if the connection was terminated abnormal (eg. directly unplug your pc's power cable) is there any way for me to know this client still connected or not?

i realize this problem when i pres f5 in my VS2008 and close, and open again and close (repeated) and then, when i debug, there's a lot of connection is still stored there but actually the client is already destroyed.

so anybody have know this best solution? example is much appreciated

my code snippet:

Dictionary<Guid, Client> Connections = new Dictionary<Guid, Client>();
// above is the variable where i put the connections
    object syncObj = new object();

    public ITcpServiceCallback CurrentCallback { get { return OperationContext.Current.GetCallbackChannel<ITcpServiceCallback>(); } }

    // this function is called when the program started
    public List<Client> ShakeHand( Client client, RoleType appType ) {
        if( GetClientsByCallback( CurrentCallback ).Count < 1 && GetClientsByID( client.ID ).Count < 1 ) {
            List<Client> retVal = new List<Client>();
            lock( syncObj ) {
                if( appType == RoleType.Doctor ) {
                    List<Client> doctors = Helpers.GetDoctor( AppDomain.CurrentDomain.BaseDirectory + "App_Data/doctor.xml" );
                    foreach( Client doctor in doctors ) {
                        doctor.Status = ConnectionStatus.Offline;
                        foreach( Client con in Connections.Values ) {
                            if( con.Role == RoleType.Doctor && con.ID == doctor.ID ) {
                                doctor.Status = ConnectionStatus.Online;
                                break;
                            }
                        }
                        retVal.Add( doctor );
                    }
                } else { //b la.. bla similiar like if above
                }

                client.Callback = CurrentCallback;
                client.Status = ConnectionStatus.Online;

                // this is the code where i add the connection
                Connections.Add( Guid.NewGuid(), client ); 

            }
            return retVal;
        }
        return null;
    }

Thank you in Advance

+1  A: 

I think what you really need to implement is a publisher / subscriber service. All the doctor and patients applications have a link to the pub/sub service. When one an application starts up, it subscribes to the service and begins receiving notifications from all the other applications. When an application closes it unsubscribes from the events.

Take a look at this: http://msdn.microsoft.com/en-us/library/ms752254.aspx

and this: http://msdn.microsoft.com/en-us/magazine/cc163537.aspx

Scott P