Perfect design template to download / sync your contact list / sync to your Messenger library

There is a function to enter the MessengerClient class. The MessengerClient class has a LoggedIn event and an IsLoggedIn property.

When Login is called on the MessengerClient class, the contact list / list is fetched over a socket from the remote server and processed, and then the client is considered registered and IsLoggedIn returns true. The LoggedIn event fires inside the Login function AFTER that (and thus inside the same thread requesting login, which I believe is not bad).

Once logged in, the client receives ongoing updates from the remote server as they occur.

When processing the contact / list at login time, I believe that the ideal design for the end user is for all contact / list data to be processed before the customer is considered registered. This way, when the user receives the LoggedIn event, they can immediately get the contact information.

For example -

Here we have an end user handler for the LoggedIn event.

    void MsgrLoggedIn(object sender, EventArgs a)
    {
        _msgr.Contacts.Contains("billy@bob.com"); //returns true
    }

      

Since the entire contact list has been processed before the client is marked as registered and before the LoggedIn event was raised, the above statement returns true. Logically, I believe this is what the end user was expecting, since downloading and processing the contact list is part of the login operation.

Now I also like to raise events when a contact has been added to a contact list or added to a group. Following the logic I already talked about, there is obviously no point in raising the ContactAdded, ContactAddedToGroup, etc. events, since the data is being processed, as this would cause the end user to receive one of these events before the MessengerClient class becomes the even ones are marked as registered.

    void MsgrContactAdded(object sender, ContactEventArgs e)
    {
        _msgr.SendMessage(e.Contact, "hello there"); // throws NotLoggedInException
    }

      

Which, as shown above, will lead to bad things.

So what I really need to do is process the contact list data, raise the logged event, and then raise all other contact events.

To do this, I can just iterate over all contact objects, group objects, etc. and raise related events.

So far, right?

However, the problem is that apart from loading the contact list data on first login, I also have to be prepared to sync contact information if the client is logged out and then logged back in.

These will be events like ContactRemoved, ContactNameChanged, ContactRemovedFromGroup, etc.

So, this is no longer as easy as iterating over contacts, groups, etc., because now I have to account for deleted contacts or changes to their properties.

So I need an alternative way to "queue" these events to be raised after login.

I looked into having classes to represent each of the sync events - for example, SyncContactRemoved, SyncContactNameChanged, SyncContactAddedToGroup. With that, I can process the data and create a Sync * XXX * class for each event and add it to the list, which I can then repeat after logging in.

I also considered using methods on the objects themselves. those. Group.SyncContactsAdded, Contact.SyncNameChanged, MessengerClient.SyncContactsAdded. Then I could iterate over contacts / groups, etc. After logging in, check these properties, raise events if necessary, and then clear them.

Finally, I thought about having an Event class that contains EventHandler and EventArgs. Events can be queued in this way and then called one by one after logging in.

If any, which of these patterns would be considered more common practice. Or are there alternative ways to achieve this?

I apologize for asking such a long question, but this is not an easy question.

thank

+3


source to share


4 answers


I ended up storing events in a list and incrementing them after.

List<SomethingEventArgs> events;

foreach (SomethingEventArgs e in events)
    OnSomethingEvent(e);

      



It's not as smooth as I would like, but it makes a lot of sense and it works great.

0


source


Each contact has its own contact list.

public class Contact
{
  List<Contact> Contacts;
}

      

Each contact has its own events (PropertyChanged, LoggedIn, whatever)

public class Contact
{
  List<Contact> Contacts;
  public event OnPropertyChanged;
}

      



If a contact is logged, that contact is logged for all events of each contact in his contact list.

public void LogIn
{
  //Load Contact List for User - Do other stuff
  foreach(Contact c in Contacts)
    c.PropertyChanged += new PropertyChangedEventHandler(ContactPropertyChanged);
}

      

Now, if something happens in the contact list, those that are LoggedIn and have this contact in the list will receive the event.

public void ChangeProperty
{
  //Change Property and raise event!
}

      

0


source


which of these patterns will be considered the more common practice. Or are there alternative ways to achieve this?

So if I get this straight, you want to queue all the events that happen on the given contacts of the contact, and the first one is offline? So Contact A has contact B in its contact list, but Contact A is not working right now. Now Contact B is changing its profile and you want to queue this event ( ContactNameChanged

etc.) for Contact A?

I wouldn't do that. I would rather store some kind of sync token (datetime would be fine) that indicates when Contact A received Contact B data. Each contact has a property LastModified

that you compare to your sync token. If there has been any modification since the last sync, query for complete information on the modified contact.

For deleted contacts, you can simply cross the two lists. The client with the client reads his current list and one updated list from the server. By comparing the ones that will result in additions and deletions, and going through this list, you can also compare the above LastModified

-datetimes.

0


source


I believe you need to take a look at the state design pattern. Please note that the client has different states every time, for example:

Login state in this state will generate some special events such as LoginFailed or LoginSucceed, and this state will only provide one login method.

The chat state in which the user accepts messages and sends them to the group.

And what do you need.

This way your client class will implement a custom custom listener interface that will emit an actual event (an event is just a call to an interface method).

This way you separate the logic of different states in different classes. You will change states within your current state so that the user of your library cannot hack and get your library to work incorrectly.

I think this is the best solution for implementing asynchronous communication. Consider two state machines on the client and server side.

This is actually a lot of code, so I just gave you the concept.

http://en.wikipedia.org/wiki/State_pattern

0


source







All Articles