Thread: Re: [Asterisk-java-users] unable to logoff()
Brought to you by:
srt
From: Peter H. <pe...@ra...> - 2006-04-01 13:14:31
|
Dear Henry, thanks for your message! First of all, hats off - you do very well as a newcomer to Java! First of all , run() is a method used with threads. If you intend to use threads, you need to inherit your class from java.lang.Thread like so: public class AMILogin extends Thread { ... } This class would then contain the run() method. To instantiate your class and start the thread you would then do this: AMILogin myLogin = null; myLogin = new AMILogin (); myLogin.getCredentials ("192.168.10.12", "admin", "admin"); myLogin.start; // -> This calls the run() method and starts the thread! Since you used the run() method and your exception message showed traces of GUI activity while at the same time AMILogin doesn't extend java.lang.Thread, I followed two possible scenarios: a) Class AMILogin is a stand-alone class having no inheritance from java.lang.Thread. To make this work, I * I added a main() method * I changed the name of the run() method to login() * instantiated AMILogin, did a login and exited. With this approach I had no errors. b) Class AMILogin does inherit from java.lang.Thread. * I added a main() method * Program instantiated AMILogin and started the thread. * Program logged off immediately. With this approach I got a NullPointerException in the logOff method as indicated. ========================================================================= Approach a: No java.lang.Thread: ========================================================================= --------------- source code 1 start ----------------------------- package org.rww.asterisk.trials; import java.io.IOException; import net.sf.asterisk.manager.AuthenticationFailedException; import net.sf.asterisk.manager.ManagerConnection; import net.sf.asterisk.manager.ManagerConnectionFactory; import net.sf.asterisk.manager.TimeoutException; public class AMILogin { String userName; String hostName; String passWord; ManagerConnection managerConnection; ManagerConnectionFactory factory; public void login() throws IOException, AuthenticationFailedException, TimeoutException { factory = new ManagerConnectionFactory(); this.managerConnection = factory.getManagerConnection(hostName, userName, passWord); managerConnection.login(); } public boolean checkLogin() { if (managerConnection.isConnected()) return true; else return false; } public void getCredentials(String h, String u, String p) { userName = u; hostName = h; passWord = p; } public void logOff() throws java.io.IOException, TimeoutException { managerConnection.logoff(); } public static void main (String [] args) { AMILogin l; try { l = new AMILogin (); l.getCredentials("192.168.10.12", "admin", "admin"); System.out.println("=======> logging in <======="); l.login(); System.out.println("\n=======> logging out <======="); l.logOff(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (AuthenticationFailedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } --------------- source code 1 end ----------------------------- The corresponding console output is: --------------- output 1 start --------------------------- =======> logging in <======= 01-Apr-2006 12:55:14 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Connecting to 192.168.10.12 port 5038 01-Apr-2006 12:55:27 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Connected via Asterisk Call Manager/1.0 01-Apr-2006 12:55:27 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Successfully logged in 01-Apr-2006 12:55:27 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Determined Asterisk version: Asterisk 1.0 =======> logging out <======= 01-Apr-2006 12:55:32 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Reached end of stream, terminating reader. 01-Apr-2006 12:55:32 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Closing socket. --------------- output 1 end --------------------------- So it worked on my machine. ========================================================================= Approach b: inherit from java.lang.Thread: ========================================================================= --------------- source code 2 start ----------------------------- package org.rww.asterisk.trials; import java.io.IOException; import net.sf.asterisk.manager.AuthenticationFailedException; import net.sf.asterisk.manager.ManagerConnection; import net.sf.asterisk.manager.ManagerConnectionFactory; import net.sf.asterisk.manager.TimeoutException; public class AMILogin extends Thread { String userName; String hostName; String passWord; ManagerConnection managerConnection; ManagerConnectionFactory factory; public void run() { factory = new ManagerConnectionFactory(); try { this.managerConnection = factory.getManagerConnection(hostName, userName, passWord); managerConnection.login(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (AuthenticationFailedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public boolean checkLogin() { if (managerConnection.isConnected()) return true; else return false; } public void getCredentials(String h, String u, String p) { userName = u; hostName = h; passWord = p; } public void logOff() throws java.io.IOException, TimeoutException { managerConnection.logoff(); } public static void main (String [] args) { AMILogin l; try { l = new AMILogin (); l.getCredentials("192.168.10.12", "admin", "admin"); System.out.println("=======> logging in <======="); l.start(); System.out.println("\n=======> logging out <======="); l.logOff(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } --------------- source code 2 end ----------------------------- Lo and behold, now the corresponding console output goes like this: --------------- output 1 start --------------------------- =======> logging in <======= =======> logging out <======= Exception in thread "main" java.lang.NullPointerException at org.rww.asterisk.trials.AMILogin.logOff(AMILogin.java:70) at org.rww.asterisk.trials.AMILogin.main(AMILogin.java:85) 01-Apr-2006 13:01:44 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Connecting to 192.168.10.12 port 5038 01-Apr-2006 13:01:57 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Connected via Asterisk Call Manager/1.0 01-Apr-2006 13:01:57 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Successfully logged in 01-Apr-2006 13:01:57 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Determined Asterisk version: Asterisk 1.0 --------------- output 1 end --------------------------- So, now I got the desired NullPointerException! The reason for this lies in the timing: The statements in the main() method: l = new AMILogin (); l.getCredentials("192.168.10.12", "admin", "admin"); System.out.println("=======> logging in <======="); l.start(); System.out.println("\n=======> logging out <======="); l.logOff(); make the following sequence happen: 1. Create new AMILogin 2. Get Credentials 3. Print "=======> logging in <======="; 4. Start Thread and RETURN IMMEDIATELY (!!) Now, my new AMILogin Thread starts. It 4.1. Logs into the asterisk server 4.2. Waits (!!) for the asterisk server's response At point 4.2., no ManagerConnection object exists yet as the call to this.managerConnection = factory.getManagerConnection(hostName, userName, passWord); hasn't returned and won't be for the next few seconds. Therefore the AMILogin member managerConnection remains null until the server has answered. But our main() function doesn't wait. Meanwhile it continues: 5. Print "\n=======> logging out <=======" 6. Call logoff (); And at this point, We get the NullPointerException, because in logOff() we call managerConnection.logoff(); on a non-existing object, as managerConnection is still null (since we are still waiting for the asterisk server's response). Then a few seconds later, the server does respond: 01-Apr-2006 13:01:44 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Connecting to 192.168.10.12 port 5038 01-Apr-2006 13:01:57 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Connected via Asterisk Call Manager/1.0 01-Apr-2006 13:01:57 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Successfully logged in 01-Apr-2006 13:01:57 net.sf.asterisk.util.impl.JavaLoggingLog info INFO: Determined Asterisk version: Asterisk 1.0 But now it's too late. What we have here is called a race condition - two thread race against each other and our application gets into a bad state. This is because the start() call is an asynchronous call - it sends the start message to the thread object and returns immediately. The opposite would be a synchronous call where the caller waits for the callee to finish. Programming wise, a call to a method / function is mostly synchronous, as the caller waits for the function to return. Some examples to illustrate this: Asynchronous call: Your boss asks you to create this report (will take four to six weeks to completion) and comes back the next day to get the result. Synchronous call: Your boss asks you to create this report (will take four to six weeks to completion) and waits for you to come back to him and then asks you for the result. In this example, the main() function is like that impatient boss - it tells your poor AMILogin object to login, but then doesn't wait and immediately wants to log out. AMILogin object shouts in protest - NullPointerException! as much as our poor employee would answer that impatient boss: "Sorry, NullReportException". Asynchronous call: You order a pizza by phone from a pizza shop. You know the pizza will come some time later and forget about the call and watch the news meanwhile. Synchronous call: You go into a pizza restaurant, sit down at a table, order a pizza and WAIT for the pizza to be finished. You certainly won't forget the order, as you can't do anything until the pizza's done. As I say, I don't know which scenario (threaded/non threaded) applies to your situation. But I hope this information helps. But there are two example applications on asterisk-java - head to http://svn.reucon.net/repos/asterisk-java-examples/trunk/ the ast-console application contains a longer text in the /doc directory explaining the application. Thanks again for your message! God bless, Peter -- dyslexics of the world - untie ! > Hi all, > i'am new to asterisk-java (to be honest to java too), so please be clement. > I am able to logon into my Asterisk server but have problem to logoff. > > #java Main > 31 mars 2006 16:11:36 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Connecting to 192.168.204.223 port 5038 > 31 mars 2006 16:11:37 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Connected via Asterisk Call Manager/1.0 > 31 mars 2006 16:11:37 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Successfully logged in > 31 mars 2006 16:11:37 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Determined Asterisk version: Asterisk 1.0 > Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException > at AMILogin.logOff(AMILogin.java:36) > at MainPage$1.mouseClicked(MainPage.java:50) > at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source) > at java.awt.Component.processMouseEvent(Unknown Source) > at javax.swing.JComponent.processMouseEvent(Unknown Source) > at java.awt.Component.processEvent(Unknown Source) > at java.awt.Container.processEvent(Unknown Source) > at java.awt.Component.dispatchEventImpl(Unknown Source) > at java.awt.Container.dispatchEventImpl(Unknown Source) > at java.awt.Component.dispatchEvent(Unknown Source) > at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source= > ) > at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) > at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) > at java.awt.Container.dispatchEventImpl(Unknown Source) > at java.awt.Window.dispatchEventImpl(Unknown Source) > at java.awt.Component.dispatchEvent(Unknown Source) > at java.awt.EventQueue.dispatchEvent(Unknown Source) > at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown So= > urce) > > at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Sour= > ce) > at java.awt.EventDispatchThread.pumpEvents(Unknown Source) > at java.awt.EventDispatchThread.pumpEvents(Unknown Source) > at java.awt.EventDispatchThread.run(Unknown Source) > > > here what i have... > > > import java.io.IOException; > import net.sf.asterisk.manager.AuthenticationFailedException; > import net.sf.asterisk.manager.ManagerConnection; > import net.sf.asterisk.manager.ManagerConnectionFactory; > import net.sf.asterisk.manager.TimeoutException; > > public class AMILogin > { > String userName; > String hostName; > String passWord; > ManagerConnection managerConnection; > ManagerConnectionFactory factory; > > public void run() throws IOException, AuthenticationFailedException, > TimeoutException > { > factory =3D new ManagerConnectionFactory(); > this.managerConnection =3D factory.getManagerConnection(hostName, > userName, passWord); > managerConnection.login(); > } > public boolean checkLogin() > { > if(managerConnection.isConnected()) > return true; > else > return false; > } > public void getCredentials(String h, String u, String p) > { > userName =3D u; > hostName =3D h; > passWord =3D p; > } > public void logOff()throws java.io.IOException, TimeoutException > { > managerConnection.logoff(); > } > } > > Has anyone an idea what is wrong? > thx in advance... > > henry. |
From: Peter H. <pe...@ra...> - 2006-04-04 23:52:32
|
Dear Henry, Sorry for the delay - I actually started on another example application to illustrate the connection and disconnection. The app is almost finished; I have to do little bis on it. I am also working on some documentation that explains that application. The app should be finished within the next one or two days, and I'll put it on the svn server. I hope this suffices. If you need a faster answer, please let me know. Thanks. Peter > Message: 1 > Date: Mon, 3 Apr 2006 11:16:55 +0200 > From: "Henry SCHMITT" <hen...@gm...> > To: ast...@li... > Subject: Re: [Asterisk-java-users] unable to logoff() > Reply-To: ast...@li... > > ------=_Part_11479_10019225.1144055815121 > Content-Type: text/plain; charset=ISO-8859-1 > Content-Transfer-Encoding: quoted-printable > Content-Disposition: inline > > Hi Peter, > > thx for the excellent clarification and for the link for ast-console. > > Here some additional information about what i am trying to do ( i think it > is something similar to the ast-console). May be you can give me a hint ( i > am a little confused and don't know what to use (with or without threads). > > I have a main that only does one thing -- dyslexics of the world - untie ! |
From: Henry S. <hen...@gm...> - 2006-04-05 07:14:31
|
Hi Peter, thx for the feedback. It is ok, i don't need a faster answer. richard On 4/5/06, Peter Hoppe <pe...@ra...> wrote: > > Dear Henry, > > Sorry for the delay - I actually started on another example application > to illustrate the connection and disconnection. The app is almost > finished; I have to do little bis on it. I am also working on some > documentation that explains that application. The app should be finished > within the next one or two days, and I'll put it on the svn server. > > I hope this suffices. If you need a faster answer, please let me know. > > Thanks. > Peter > > > > > > > Message: 1 > > Date: Mon, 3 Apr 2006 11:16:55 +0200 > > From: "Henry SCHMITT" <hen...@gm...> > > To: ast...@li... > > Subject: Re: [Asterisk-java-users] unable to logoff() > > Reply-To: ast...@li... > > > > ------=3D_Part_11479_10019225.1144055815121 > > Content-Type: text/plain; charset=3DISO-8859-1 > > Content-Transfer-Encoding: quoted-printable > > Content-Disposition: inline > > > > Hi Peter, > > > > thx for the excellent clarification and for the link for ast-console. > > > > Here some additional information about what i am trying to do ( i think > it > > is something similar to the ast-console). May be you can give me a hint > ( i > > am a little confused and don't know what to use (with or without > threads). > > > > I have a main that only does one thing > > > -- > dyslexics of the world - untie ! > > > ------------------------------------------------------- > This SF.Net email is sponsored by xPML, a groundbreaking scripting > language > that extends applications into web and mobile media. Attend the live > webcast > and join the prime developer group breaking into this new coding > territory! > http://sel.as-us.falkag.net/sel?cmd=3Dlnk&kid=3D110944&bid=3D241720&dat= =3D121642 > _______________________________________________ > Asterisk-java-users mailing list > Ast...@li... > https://lists.sourceforge.net/lists/listinfo/asterisk-java-users > |
From: Peter H. <pe...@ra...> - 2006-04-06 10:14:11
|
Dear Henry, I have now finished the example application that logs in and out of asterisk. I am still working on the readme file. You can find the example on http://svn.reucon.net/repos/asterisk-java-examples/trunk/ast-connection-vm/ there is an explanatory document in the /doc/readme sub directory (Still working on it). Basically, when I use asterisk-java the approach I take pertaining to asterisk connection is to use a separate class that extends java.lang.Thread and runs as a thread. This class I implement as a finite state machine. This construct is explained in more detail in the readme document. This approach has numerous advantages which outweigh the disadvantage of additional development overhead, and again, the readme document will tell you about this. Please give some feedback for further questions/comments. Peter > Message: 3 > Date: Wed, 05 Apr 2006 00:52:47 +0100 > From: Peter Hoppe <pe...@ra...> > To: ast...@li... > Subject: Re: [Asterisk-java-users] unable to logoff() > Reply-To: ast...@li... > > Dear Henry, > > Sorry for the delay - I actually started on another example application > to illustrate the connection and disconnection. The app is almost > finished; I have to do little bis on it. I am also working on some > documentation that explains that application. The app should be finished > within the next one or two days, and I'll put it on the svn server. > > I hope this suffices. If you need a faster answer, please let me know. > > Thanks. > Peter > > > > > > >>> Message: 1 >>> Date: Mon, 3 Apr 2006 11:16:55 +0200 >>> From: "Henry SCHMITT" <hen...@gm...> >>> To: ast...@li... >>> Subject: Re: [Asterisk-java-users] unable to logoff() >>> Reply-To: ast...@li... >>> >>> ------=_Part_11479_10019225.1144055815121 >>> Content-Type: text/plain; charset=ISO-8859-1 >>> Content-Transfer-Encoding: quoted-printable >>> Content-Disposition: inline >>> >>> Hi Peter, >>> >>> thx for the excellent clarification and for the link for ast-console. >>> >>> Here some additional information about what i am trying to do ( i think it >>> is something similar to the ast-console). May be you can give me a hint ( i >>> am a little confused and don't know what to use (with or without threads). >>> >>> I have a main that only does one thing > > > > -- dyslexics of the world - untie ! -- dyslexics of the world - untie ! |
From: Henry S. <hen...@gm...> - 2006-04-03 09:17:02
|
Hi Peter, thx for the excellent clarification and for the link for ast-console. Here some additional information about what i am trying to do ( i think it is something similar to the ast-console). May be you can give me a hint ( i am a little confused and don't know what to use (with or without threads). I have a main that only does one thing -------------------------------Main----------------------------------------= -------------------------------------------- public class Main { public static void main(String args[]) { LogIn logIn =3D new LogIn(); } } ---------------------------------------------------------------------------= ------------------------------------------------ In my LogIn i have a JTextField for the username, a JPasswordField for the password and an editable JComboBox with some predefined IP@ (or you can enter your IP@). With a JButton i am able to login into my Asterisk server. ------------------------------LogIn----------------------------------------= -------------------------------------------- sButtonOk.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent Event) { if((sTextFieldUser.getText().equals("")) || ( sTextFieldPasswd.getText().equals(""))) { JOptionPane.showMessageDialog(new JFrame(), "Required field cannot be left blank.\nEnter username AND password.","Error Message",JOptionPane.ERROR_MESSAGE); } else { try { AMILogin amiLogin =3D new AMILogin(); =20 amiLogin.getCredentials((String)sComboDomain.getSelectedItem(), sTextFieldUser.getText(), sTextFieldPasswd.getText()); amiLogin.logIn(); if(amiLogin.checkLogin()) { JOptionPane.showMessageDialog(new JFrame(),"Successfully logged in","Login Message.", JOptionPane.INFORMATION_MESSAGE); //System.out.println("Successfully logged in!!!!!"); setVisible(false); dispose(); MainPage mainPage =3D new MainPage(); } } catch (TimeoutException e) { JOptionPane.showMessageDialog(new JFrame(),"Error: Connection Timed out","Error Message.", JOptionPane.ERROR_MESSAGE); System.out.println("Error: "+e.getMessage()); } catch (AuthenticationFailedException e) { JOptionPane.showMessageDialog(new JFrame(),"Error: Authentication Failed.","Error Message", JOptionPane.ERROR_MESSAGE); System.out.println("Error: "+e.getMessage()); } catch (IOException e) { JOptionPane.showMessageDialog(new JFrame(),"Error: Connection Failed.","Error Message", JOptionPane.ERROR_MESSAGE); System.out.println("Error: "+e.getMessage()); } } } }); ---------------------------------------------------------------------------= ------------------------------------------------ I have changed the AMILogin as suggested in your post (no inheritance from java.lang.Thread.). ------------------------------AMILogin-------------------------------------= ----------------------------------------------- import java.io.IOException; import net.sf.asterisk.manager.AuthenticationFailedException; import net.sf.asterisk.manager.ManagerConnection; import net.sf.asterisk.manager.ManagerConnectionFactory; import net.sf.asterisk.manager.TimeoutException; public class AMILogin { String userName; String hostName; String passWord; ManagerConnection managerConnection; ManagerConnectionFactory factory; public void logIn() throws IOException, AuthenticationFailedException, TimeoutException { factory =3D new ManagerConnectionFactory(); this.managerConnection =3D factory.getManagerConnection(hostName, userName, passWord); managerConnection.login(); } public boolean checkLogin() { if(managerConnection.isConnected()) return true; else return false; } public void getCredentials(String h, String u, String p) { userName =3D u; hostName =3D h; passWord =3D p; } public void logOff()throws java.io.IOException, TimeoutException { managerConnection.logoff(); } } ---------------------------------------------------------------------------= ------------------------------------------------ Finally i have a MainPage with one JFrame, a JTextArea and a JButton for th= e logoff. ------------------------------------------MainPage-------------------------= ------------------------------------------- sButtonLogoff.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent Event) { AMILogin amiLogoff; try { amiLogoff =3D new AMILogin(); amiLogoff.logOff(); } catch (TimeoutException e) { JOptionPane.showMessageDialog(new JFrame(),"Error: Connection Timed out","Error Message.", JOptionPane.ERROR_MESSAGE); System.err.println("Error: "+e.getMessage()); } catch (IOException e) { JOptionPane.showMessageDialog(new JFrame(),"Error: Connection Failed.","Error Message", JOptionPane.ERROR_MESSAGE); System.err.println("Error: "+e.getMessage()); } } }); ---------------------------------------------------------------------------= ------------------------------------------------ Unfortunately i still have the NullPointerException. Sorry, but i don't kno= w where to start my troubleshooting... On 4/1/06, Peter Hoppe <pe...@ra...> wrote: > > Dear Henry, > > thanks for your message! First of all, hats off - you do very well as a > newcomer to Java! > > First of all , run() is a method used with threads. If you intend to use > threads, you need to inherit your class from java.lang.Thread like so: > > public class AMILogin extends Thread > { > ... > } > > This class would then contain the run() method. To instantiate your > class and start the thread you would then do this: > > AMILogin myLogin =3D null; > > myLogin =3D new AMILogin (); > myLogin.getCredentials ("192.168.10.12", "admin", "admin"); > myLogin.start; // -> This calls the run() method and starts the thread! > > > Since you used the run() method and your exception message showed traces > of GUI activity while at the same time AMILogin doesn't extend > java.lang.Thread, I followed two possible scenarios: > > a) Class AMILogin is a stand-alone class having no inheritance from > java.lang.Thread. > To make this work, I > * I added a main() method > * I changed the name of the run() method to login() > * instantiated AMILogin, did a login and exited. > > With this approach I had no errors. > > > b) Class AMILogin does inherit from java.lang.Thread. > * I added a main() method > * Program instantiated AMILogin and started the thread. > * Program logged off immediately. > > With this approach I got a NullPointerException in the logOff > method as indicated. > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > Approach a: No java.lang.Thread: > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > --------------- source code 1 start ----------------------------- > > package org.rww.asterisk.trials; > > import java.io.IOException; > import net.sf.asterisk.manager.AuthenticationFailedException; > import net.sf.asterisk.manager.ManagerConnection; > import net.sf.asterisk.manager.ManagerConnectionFactory; > import net.sf.asterisk.manager.TimeoutException; > > public class AMILogin > { > String userName; > > String hostName; > > String passWord; > > ManagerConnection managerConnection; > > ManagerConnectionFactory factory; > > public void login() throws IOException, AuthenticationFailedException= , > TimeoutException > { > factory =3D new ManagerConnectionFactory(); > this.managerConnection =3D factory.getManagerConnection(hostName, > userName, passWord); > managerConnection.login(); > } > > public boolean checkLogin() > { > if (managerConnection.isConnected()) > return true; > else > return false; > } > > public void getCredentials(String h, String u, String p) > { > userName =3D u; > hostName =3D h; > passWord =3D p; > } > > public void logOff() throws java.io.IOException, TimeoutException > { > managerConnection.logoff(); > } > > public static void main (String [] args) > { > AMILogin l; > > try > { > l =3D new AMILogin (); > l.getCredentials("192.168.10.12", "admin", "admin"); > System.out.println("=3D=3D=3D=3D=3D=3D=3D> logging in <=3D= =3D=3D=3D=3D=3D=3D"); > l.login(); > System.out.println("\n=3D=3D=3D=3D=3D=3D=3D> logging out <=3D= =3D=3D=3D=3D=3D=3D"); > l.logOff(); > } > catch (IOException e) > { > // TODO Auto-generated catch block > e.printStackTrace(); > } > catch (AuthenticationFailedException e) > { > // TODO Auto-generated catch block > e.printStackTrace(); > } > catch (TimeoutException e) > { > // TODO Auto-generated catch block > e.printStackTrace(); > } > catch (InterruptedException e) > { > // TODO Auto-generated catch block > e.printStackTrace(); > } > > } > } > > --------------- source code 1 end ----------------------------- > > The corresponding console output is: > > --------------- output 1 start --------------------------- > > =3D=3D=3D=3D=3D=3D=3D> logging in <=3D=3D=3D=3D=3D=3D=3D > 01-Apr-2006 12:55:14 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Connecting to 192.168.10.12 port 5038 > 01-Apr-2006 12:55:27 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Connected via Asterisk Call Manager/1.0 > 01-Apr-2006 12:55:27 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Successfully logged in > 01-Apr-2006 12:55:27 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Determined Asterisk version: Asterisk 1.0 > > =3D=3D=3D=3D=3D=3D=3D> logging out <=3D=3D=3D=3D=3D=3D=3D > 01-Apr-2006 12:55:32 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Reached end of stream, terminating reader. > 01-Apr-2006 12:55:32 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Closing socket. > > --------------- output 1 end --------------------------- > > > So it worked on my machine. > > > > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > Approach b: inherit from java.lang.Thread: > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > --------------- source code 2 start ----------------------------- > > package org.rww.asterisk.trials; > > import java.io.IOException; > import net.sf.asterisk.manager.AuthenticationFailedException; > import net.sf.asterisk.manager.ManagerConnection; > import net.sf.asterisk.manager.ManagerConnectionFactory; > import net.sf.asterisk.manager.TimeoutException; > > public class AMILogin extends Thread > { > String userName; > > String hostName; > > String passWord; > > ManagerConnection managerConnection; > > ManagerConnectionFactory factory; > > public void run() > { > factory =3D new ManagerConnectionFactory(); > try > { > this.managerConnection =3D factory.getManagerConnection > (hostName, > userName, passWord); > managerConnection.login(); > } > catch (IOException e) > { > // TODO Auto-generated catch block > e.printStackTrace(); > } > catch (AuthenticationFailedException e) > { > // TODO Auto-generated catch block > e.printStackTrace(); > } > catch (TimeoutException e) > { > // TODO Auto-generated catch block > e.printStackTrace(); > } > } > > public boolean checkLogin() > { > if (managerConnection.isConnected()) > return true; > else > return false; > } > > public void getCredentials(String h, String u, String p) > { > userName =3D u; > hostName =3D h; > passWord =3D p; > } > > public void logOff() throws java.io.IOException, TimeoutException > { > managerConnection.logoff(); > } > > public static void main (String [] args) > { > AMILogin l; > > try > { > l =3D new AMILogin (); > l.getCredentials("192.168.10.12", "admin", "admin"); > System.out.println("=3D=3D=3D=3D=3D=3D=3D> logging in <= =3D=3D=3D=3D=3D=3D=3D"); > l.start(); > > System.out.println("\n=3D=3D=3D=3D=3D=3D=3D> logging out = <=3D=3D=3D=3D=3D=3D=3D"); > l.logOff(); > } > catch (IOException e) > { > // TODO Auto-generated catch block > e.printStackTrace(); > } > catch (TimeoutException e) > { > // TODO Auto-generated catch block > e.printStackTrace(); > } > > } > } > > --------------- source code 2 end ----------------------------- > > Lo and behold, now the corresponding console output goes like this: > > --------------- output 1 start --------------------------- > > =3D=3D=3D=3D=3D=3D=3D> logging in <=3D=3D=3D=3D=3D=3D=3D > > =3D=3D=3D=3D=3D=3D=3D> logging out <=3D=3D=3D=3D=3D=3D=3D > Exception in thread "main" java.lang.NullPointerException > at org.rww.asterisk.trials.AMILogin.logOff(AMILogin.java:70) > at org.rww.asterisk.trials.AMILogin.main(AMILogin.java:85) > 01-Apr-2006 13:01:44 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Connecting to 192.168.10.12 port 5038 > 01-Apr-2006 13:01:57 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Connected via Asterisk Call Manager/1.0 > 01-Apr-2006 13:01:57 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Successfully logged in > 01-Apr-2006 13:01:57 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Determined Asterisk version: Asterisk 1.0 > > --------------- output 1 end --------------------------- > > So, now I got the desired NullPointerException! The reason for this lies > in the timing: The statements in the main() method: > > l =3D new AMILogin (); > l.getCredentials("192.168.10.12", "admin", "admin"); > System.out.println("=3D=3D=3D=3D=3D=3D=3D> logging in <= =3D=3D=3D=3D=3D=3D=3D"); > l.start(); > > System.out.println("\n=3D=3D=3D=3D=3D=3D=3D> logging out = <=3D=3D=3D=3D=3D=3D=3D"); > l.logOff(); > > make the following sequence happen: > > 1. Create new AMILogin > 2. Get Credentials > 3. Print "=3D=3D=3D=3D=3D=3D=3D> logging in <=3D=3D=3D=3D=3D=3D=3D"; > 4. Start Thread and RETURN IMMEDIATELY (!!) > > Now, my new AMILogin Thread starts. It > 4.1. Logs into the asterisk server > 4.2. Waits (!!) for the asterisk server's response > > At point 4.2., no ManagerConnection object exists yet as the call to > this.managerConnection =3D factory.getManagerConnection(hostName, > userName, passWord); > > hasn't returned and won't be for the next few seconds. Therefore the > AMILogin member > managerConnection > > remains null until the server has answered. > > > But our main() function doesn't wait. Meanwhile it continues: > 5. Print "\n=3D=3D=3D=3D=3D=3D=3D> logging out <=3D=3D=3D=3D=3D=3D=3D" > 6. Call logoff (); > > And at this point, We get the NullPointerException, because in logOff() > we call > managerConnection.logoff(); > > on a non-existing object, as managerConnection is still null (since we > are still waiting for the asterisk server's response). > > > Then a few seconds later, the server does respond: > 01-Apr-2006 13:01:44 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Connecting to 192.168.10.12 port 5038 > 01-Apr-2006 13:01:57 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Connected via Asterisk Call Manager/1.0 > 01-Apr-2006 13:01:57 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Successfully logged in > 01-Apr-2006 13:01:57 net.sf.asterisk.util.impl.JavaLoggingLog info > INFO: Determined Asterisk version: Asterisk 1.0 > > But now it's too late. > > What we have here is called a race condition - two thread race against > each other and our application gets into a bad state. This is because > the start() call is an asynchronous call - it sends the start message to > the thread object and returns immediately. The opposite would be a > synchronous call where the caller waits for the callee to finish. > Programming wise, a call to a method / function is mostly synchronous, > as the caller waits for the function to return. > > Some examples to illustrate this: > > Asynchronous call: Your boss asks you to create this report (will take > four to six weeks to completion) and comes back the next day to get the > result. > Synchronous call: Your boss asks you to create this report (will take > four to six weeks to completion) and waits for you to come back to him > and then asks you for the result. > > In this example, the main() function is like that impatient boss - it > tells your poor AMILogin object to login, but then doesn't wait and > immediately wants to log out. AMILogin object shouts in protest - > NullPointerException! as much as our poor employee would answer that > impatient boss: "Sorry, NullReportException". > > Asynchronous call: You order a pizza by phone from a pizza shop. You > know the pizza will come some time later and forget about the call and > watch the news meanwhile. > Synchronous call: You go into a pizza restaurant, sit down at a table, > order a pizza and WAIT for the pizza to be finished. You certainly won't > forget the order, as you can't do anything until the pizza's done. > > > > As I say, I don't know which scenario (threaded/non threaded) applies to > your situation. But I hope this information helps. > > But there are two example applications on asterisk-java - head to > http://svn.reucon.net/repos/asterisk-java-examples/trunk/ > > the ast-console application contains a longer text in the /doc directory > explaining the application. > > > Thanks again for your message! > > > God bless, > > Peter > > > -- > dyslexics of the world - untie ! > > > > > Hi all, > > i'am new to asterisk-java (to be honest to java too), so please be > clement. > > I am able to logon into my Asterisk server but have problem to logoff. > > > > #java Main > > 31 mars 2006 16:11:36 net.sf.asterisk.util.impl.JavaLoggingLog info > > INFO: Connecting to 192.168.204.223 port 5038 > > 31 mars 2006 16:11:37 net.sf.asterisk.util.impl.JavaLoggingLog info > > INFO: Connected via Asterisk Call Manager/1.0 > > 31 mars 2006 16:11:37 net.sf.asterisk.util.impl.JavaLoggingLog info > > INFO: Successfully logged in > > 31 mars 2006 16:11:37 net.sf.asterisk.util.impl.JavaLoggingLog info > > INFO: Determined Asterisk version: Asterisk 1.0 > > Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException > > at AMILogin.logOff(AMILogin.java:36) > > at MainPage$1.mouseClicked(MainPage.java:50) > > at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source) > > at java.awt.Component.processMouseEvent(Unknown Source) > > at javax.swing.JComponent.processMouseEvent(Unknown Source) > > at java.awt.Component.processEvent(Unknown Source) > > at java.awt.Container.processEvent(Unknown Source) > > at java.awt.Component.dispatchEventImpl(Unknown Source) > > at java.awt.Container.dispatchEventImpl(Unknown Source) > > at java.awt.Component.dispatchEvent(Unknown Source) > > at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown > Source=3D > > ) > > at java.awt.LightweightDispatcher.processMouseEvent(Unknown > Source) > > at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) > > at java.awt.Container.dispatchEventImpl(Unknown Source) > > at java.awt.Window.dispatchEventImpl(Unknown Source) > > at java.awt.Component.dispatchEvent(Unknown Source) > > at java.awt.EventQueue.dispatchEvent(Unknown Source) > > at > java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown So=3D > > urce) > > > > at > java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Sour=3D > > ce) > > at java.awt.EventDispatchThread.pumpEvents(Unknown Source) > > at java.awt.EventDispatchThread.pumpEvents(Unknown Source) > > at java.awt.EventDispatchThread.run(Unknown Source) > > > > > > here what i have... > > > > > > import java.io.IOException; > > import net.sf.asterisk.manager.AuthenticationFailedException; > > import net.sf.asterisk.manager.ManagerConnection; > > import net.sf.asterisk.manager.ManagerConnectionFactory; > > import net.sf.asterisk.manager.TimeoutException; > > > > public class AMILogin > > { > > String userName; > > String hostName; > > String passWord; > > ManagerConnection managerConnection; > > ManagerConnectionFactory factory; > > > > public void run() throws IOException, AuthenticationFailedException, > > TimeoutException > > { > > factory =3D3D new ManagerConnectionFactory(); > > this.managerConnection =3D3D factory.getManagerConnection(hostName, > > userName, passWord); > > managerConnection.login(); > > } > > public boolean checkLogin() > > { > > if(managerConnection.isConnected()) > > return true; > > else > > return false; > > } > > public void getCredentials(String h, String u, String p) > > { > > userName =3D3D u; > > hostName =3D3D h; > > passWord =3D3D p; > > } > > public void logOff()throws java.io.IOException, TimeoutException > > { > > managerConnection.logoff(); > > } > > } > > > > Has anyone an idea what is wrong? > > thx in advance... > > > > henry. > > > > ------------------------------------------------------- > This SF.Net email is sponsored by xPML, a groundbreaking scripting > language > that extends applications into web and mobile media. Attend the live > webcast > and join the prime developer group breaking into this new coding > territory! > http://sel.as-us.falkag.net/sel?cmd=3Dlnk&kid=3D110944&bid=3D241720&dat= =3D121642 > _______________________________________________ > Asterisk-java-users mailing list > Ast...@li... > https://lists.sourceforge.net/lists/listinfo/asterisk-java-users > |