Not logged in. · Lost password · Register
Forum: agsXMPP SDK Support RSS
Avatar
parthy #1
Member since Mar 2007 · 9 posts
Group memberships: Members
Show profile · Link to this post
Subject: Yet another sending a message question
Hi guys,

This is yet another sending messages question. My requirement is to have notifications sent via a Jabber client to Jabber servers. Here is a very simple working client (all it does is login and sends a message to a jabber user):

    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        ///

        private agsXMPP.XmppClientConnection conn;
        public Class1()
        {
            conn = new XmppClientConnection("jabber.com");
            conn.OnLogin += new ObjectHandler(runMeOnLogin);
            conn.OnReadXml += new XmlHandler(runMeOnReadXml);
            conn.OnWriteXml += new XmlHandler(runMeOnWriteXml);
           
            conn.Open("partheinstein", "password!", "test");
           
            Console.ReadLine();
        }

        [STAThread]
        static void Main(string[] args)
        {
            Class1 c = new Class1();

        }

        public void runMeOnReadXml(object sender, string xml)
        {
            Console.WriteLine(xml);
        }
       
        public void runMeOnWriteXml(object sender, string xml)
        {
            Console.WriteLine(xml);
        }

        public void runMeOnLogin(Object sender)
        {
           
            try
            {
               
                conn.Send(new Message("parthy@jabber.com", MessageType.chat, "hello"));
            }
            catch(Exception e)
            {
                Console.WriteLine("test");
                Console.WriteLine(e.ToString());
            }

        }

        public void sendMessage()
        {
           
            try
            {
               
                conn.Send(new Message("parthy@jabber.com", MessageType.chat, "hello"));
            }
            catch(Exception e)
            {
                Console.WriteLine("test");
                Console.WriteLine(e.ToString());
            }

        }
    }

As per some earlier posts, the conn.Open("partheinstein", "password!", "test"); is asynchronous and returns immediately (executing runMeOnLogin method).

Here is my question:

How can I create a jabber client (thread) which will be blocked as soon it logs in? So that I can invoke sendMessage(string to, string message) method on the same thread and expect the message to be delivered. Essentially need to somehow make the thread to remember that it has already logged in.

This is how my client invocation should be:

Class1 c = new Class1();
c.sendMessage(to, message);


Please note that I am not using any GUI (with its threads) and all I want is to be able to send/receive jabber messages to/from a jabber server. No funky jabber client. There will be an external program which will invoke this client and will pass in the message and recipient address.

Thank you,
parthy
Avatar
Jabberer #2
Member since Feb 2006 · 249 posts
Group memberships: Members
Show profile · Link to this post
you have to work with Monitors from System.Threading or similar stuff to stop your thread until you get the Login Event and then resume it.
Look at Monitors in the MSDN.
Software Developer
AG-Software
Avatar
parthy #3
Member since Mar 2007 · 9 posts
Group memberships: Members
Show profile · Link to this post
Hi again,

I am having trouble with the following code:

    class Class1
    {

        private agsXMPP.XmppClientConnection conn;

        public Class1()
        {
            conn = new XmppClientConnection("livejournal.com");
            conn.OnLogin += new ObjectHandler(runMeOnLogin);
            conn.OnReadXml += new XmlHandler(runMeOnReadXml);
            conn.OnWriteXml += new XmlHandler(runMeOnWriteXml);
            conn.Open("partheinstein", "pass", "test");

        }

        [STAThread]
        static void Main(string[] args)
        {
            //
            // TODO: Add code to start application here
            //
           
            Class1 c = new Class1();

            Thread trd = new Thread(new ThreadStart(c.login));
           
            trd.Start();
            Console.WriteLine("!!!!!!!!!!!!!!!!"+trd.IsAlive);
            Thread.Sleep(4);
            Console.WriteLine("@@@@@@@@@@@@@@@@"+trd.IsAlive);
            trd.Resume(); [color=crimson]<== this results in an exception saying that the thread cannot be resumed because it has already finished, eventhough I suspended it[/color]
            Console.ReadLine();



        }
        public void runMeOnReadXml(object sender, string xml)
        {
            Console.WriteLine(xml);
        }
       
        public void runMeOnWriteXml(object sender, string xml)
        {
            Console.WriteLine(xml);
        }

        public void runMeOnLogin(Object sender)
        {
            Thread.CurrentThread.Suspend();
            sendMessage();
           
        }

        public void sendMessage()
        {
            try
            {   
                conn.Send(new Message("parthy@jabber.com", MessageType.chat, "hi++"));
            }
            catch(Exception e)
            {
                Console.WriteLine("test");
                Console.WriteLine(e.ToString());
            }

        }
    }

Why cant I suspend this thread?

I am trying to figure out how to use monitors as you had mentioned. But I would really appreciate if you can provide some guidance on it:

This is the psuedocode I have it mind:

1. Start a client thread from main thread
2. Make the client thread to login (using XMPPClientconnection.open() method)
3. Suspend the thread till LoginEvent occurs (I have no idea how to do this)
4. When the LoginEvent occurs, resume it and send the message to the Jabber server
5. Once the client successfully sends the message, relogin. Iterate through step 3 to 5.

But this is not what I want. All I need is a client which can login and stay in logged-in state. If I need is to send a message to the Jabber server, all i have to do is client.sendMsg(). Since the client is already logged in, the message will be sent to the server. Please note that I will invoking this sendMsg() method many times before logging out.

Any partial code or logic will be very helpful.

Thank you.
parthy
Avatar
Alex #4
Member since Feb 2003 · 4245 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello,

try Monitor.Wait() for suspending your Thread and Monitor.Pulse() to resume it.
If you search with Google for Monitors in .NET you find many examples.
Also in the MSDN are good examples.

Alex
Alexander Gnauck
AG-Software
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Forum: agsXMPP SDK Support RSS