Java Addicted Programmer

my escape way from the addiction……..

java and Instant Messaging (Part.2)

leave a comment »

ok we will continue to the next thing todo , loading contact list in a Instant Messaging an contact list alsp call as a roster, the roster lets you keep track of the availability (“presence”) of other users. A roster also allows you to organize users into groups such as “Friends” and “Co-workers”. Other IM systems refer to the roster as the buddy list, contact list, etc.A Roster instance is obtained using the XMPPConnection.getRoster() method, but only after successfully logging into a server.

Roster Entries

Every user in a roster is represented by a RosterEntry, which consists of:

  • An XMPP address (e.g. jsmith@example.com).
  • A name you’ve assigned to the user (e.g. “Joe”).
  • The list of groups in the roster that the entry belongs to. If the roster entry belongs to no groups, it’s called an “unfiled entry”.

The following code snippet prints all entries in the roster:

Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
    System.out.println(entry);

}

Methods also exist to get individual entries, the list of unfiled entries, or to get one or
all roster groups.

Presence

Roster

Every entry in the roster has presence associated with it. The Roster.getPresence(String user) method will return a Presence object with the user’s presence or null if the user is not online or you are not subscribed to the user’s presence. Note: typically, presence subscription is always tied to the user being on the roster, but this is not true in all cases.

A user either has a presence of online or offline. When a user is online, their presence may contain extended information such as what they are currently doing, whether they wish to be disturbed, etc. See the Presence class for further details.

Listening for Roster and Presence Changes

The typical use of the roster class is to display a tree view of groups and entries along with the current presence value of each entry. As an example, see the image showing a Roster in the Exodus XMPP client to the right.

The presence information will likely change often, and it’s also possible for the roster entries to change or be deleted. To listen for changing roster and presence data, a RosterListener should be used. The following code snippet registers a RosterListener with the Roster that prints any presence changes in the roster to standard out. A normal client would use similar code to update the roster UI with the changing information.

Roster roster = con.getRoster();
roster.addRosterListener(new RosterListener() {
    // Ignored events public void entriesAdded(Collection<String> addresses) {}
    public void entriesDeleted(Collection<String> addresses) {}
    public void entriesUpdated(Collection<String> addresses) {}
    public void presenceChanged(Presence presence) {
        System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
    }
});

Adding Entries to the Roster

Rosters and presence use a permissions-based model where users must give permission before they are added to someone else’s roster. This protects a user’s privacy by making sure that only approved users are able to view their presence information. Therefore, when you add a new roster entry it will be in a pending state until the other user accepts your request.

If another user requests a presence subscription so they can add you to their roster, you must accept or reject that request. Smack handles presence subscription requests in one of three ways:

  • Automatically accept all presence subscription requests.
  • Automatically reject all presence subscription requests.
  • Process presence subscription requests manually.

The mode can be set using the Roster.setSubscriptionMode(Roster.SubscriptionMode) method. Simple clients normally use one of the automated subscription modes, while full-featured clients should manually process subscription requests and let the end-user accept or reject each request. If using the manual mode, a PacketListener should be registered that listens for Presence packets that have a type of Presence.Type.subscribe.
very easy right, we already gathered all needed basic function  to write our IM client from scratch,so stay tunes guys  and happy coding…………….

Written by luvj

September 24, 2008 at 11:56 pm

Posted in Instan Messaging

java and Instant Messaging (Part.1)

with one comment

there a lot bunch of instant messaging services out there, but the most used and adopted is the jabber protocol,event GTalk is based on Jabber protocol one of the jabber features is jabber can talk to other protocols. For this purpose we will using SMACK API from igniterealtime

1.Create connection
XMPPConnection conn1 = new XMPPConnection(“your_messaging_server.com”);
conn1.connect();

one of the weakness this api its if your Instant messaging domain different with your server
domain you must change the SMACK connection class

conn1.login(“your_id”,“your_pass”,“your_resource”);

2.Setting your presence

there a available,chat,away,dnd(do not disturb),xa (extended away) as presence status choice

to set your presence status simply by write

Presence presence=new Presence(Presence.Type.available);

presence.setStatus(“in the office”);

conn1.sendPacket(presence);

3.Send a message

for sending message simply using

Message msg=new Message();
msg.setFrom(conn1.getUser());
msg.setTo(“your_friend@your_domain.com”);

msg.setBody(“hallo are you ther ?”);
conn1.sendPacket(msg);

very easy right , with combine this API and JSP/Servlet u can start write your own Meebo like IM service feel free to contact me if there any question, so see u next time guys and happy coding

Written by luvj

September 18, 2008 at 4:50 am

Posted in Instan Messaging

Hello World!

leave a comment »

hip huuray, at last had time to create a blog for my coding hobby,

i hope can help somebody or inspires some programer out there ha..ha..ha…, so wish me luck guys, roger and out ………….

void main(String Args[])

{

System.out.println(“Hello World!”);

}

Written by luvj

September 18, 2008 at 4:25 am

Posted in said a loud

Follow

Get every new post delivered to your Inbox.