2 TutorialRegistration.java
3 Copyright (C) 2010 Belledonne Communications SARL
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package org.linphone.core.tutorials;
21 import org.linphone.core.LinphoneAddress;
22 import org.linphone.core.LinphoneCall;
23 import org.linphone.core.LinphoneChatRoom;
24 import org.linphone.core.LinphoneCore;
25 import org.linphone.core.LinphoneCoreException;
26 import org.linphone.core.LinphoneCoreFactory;
27 import org.linphone.core.LinphoneCoreListener;
28 import org.linphone.core.LinphoneFriend;
29 import org.linphone.core.LinphoneProxyConfig;
30 import org.linphone.core.LinphoneCall.State;
31 import org.linphone.core.LinphoneCore.GlobalState;
32 import org.linphone.core.LinphoneCore.RegistrationState;
36 * This program is a _very_ simple usage example of liblinphone.
37 * Demonstrating how to initiate a SIP registration from a sip uri identity
38 * passed from the command line.
40 * First argument must be like sip:jehan@sip.linphone.org, second must be password.
42 * ex registration sip:jehan@sip.linphone.org secret
44 * Ported from registration.c
46 * @author Guillaume Beraudo
49 public class TutorialRegistration implements LinphoneCoreListener {
50 private boolean running;
51 private TutorialNotifier TutorialNotifier;
54 public TutorialRegistration(TutorialNotifier TutorialNotifier) {
55 this.TutorialNotifier = TutorialNotifier;
58 public TutorialRegistration() {
59 this.TutorialNotifier = new TutorialNotifier();
64 * Registration state notification listener
66 public void registrationState(LinphoneCore lc, LinphoneProxyConfig cfg,RegistrationState cstate, String smessage) {
67 write(cfg.getIdentity() + " : "+smessage+"\n");
70 public void show(LinphoneCore lc) {}
71 public void byeReceived(LinphoneCore lc, String from) {}
72 public void authInfoRequested(LinphoneCore lc, String realm, String username) {}
73 public void displayStatus(LinphoneCore lc, String message) {}
74 public void displayMessage(LinphoneCore lc, String message) {}
75 public void displayWarning(LinphoneCore lc, String message) {}
76 public void globalState(LinphoneCore lc, GlobalState state, String message) {}
77 public void newSubscriptionRequest(LinphoneCore lc, LinphoneFriend lf,String url) {}
78 public void notifyPresenceReceived(LinphoneCore lc, LinphoneFriend lf) {}
79 public void textReceived(LinphoneCore lc, LinphoneChatRoom cr,LinphoneAddress from, String message) {}
80 public void callState(LinphoneCore lc, LinphoneCall call, State cstate, String msg) {}
83 public static void main(String[] args) {
84 // Check tutorial was called with the right number of arguments
85 if (args.length != 2) {
86 throw new IllegalArgumentException("Bad number of arguments");
89 // Create tutorial object
90 TutorialRegistration tutorial = new TutorialRegistration();
92 // takes sip uri identity from the command line arguments
93 String userSipAddress = args[1];
94 // takes password from the command line arguments
95 String userSipPassword = args[2];
96 tutorial.launchTutorial(userSipAddress, userSipPassword);
97 } catch (Exception e) {
104 public void launchTutorial(String sipAddress, String password) throws LinphoneCoreException {
105 final LinphoneCoreFactory lcFactory = LinphoneCoreFactory.instance();
107 // First instantiate the core Linphone object given only a listener.
108 // The listener will react to events in Linphone core.
109 LinphoneCore lc = lcFactory.createLinphoneCore(this);
115 LinphoneAddress address = lcFactory.createLinphoneAddress(sipAddress);
116 String username = address.getUserName();
117 String domain = address.getDomain();
120 if (password != null) {
121 // create authentication structure from identity and add to linphone
122 lc.addAuthInfo(lcFactory.createAuthInfo(username, password, null));
125 // create proxy config
126 LinphoneProxyConfig proxyCfg = lcFactory.createProxyConfig(sipAddress, domain, null, true);
127 lc.addProxyConfig(proxyCfg); // add it to linphone
128 lc.setDefaultProxyConfig(proxyCfg);
132 // main loop for receiving notifications and doing background linphonecore work
135 lc.iterate(); // first iterate initiates registration
138 } catch(InterruptedException ie) {
139 write("Interrupted!\nAborting");
144 // Unregister then register again
145 lc.getDefaultProxyConfig().edit();
146 lc.getDefaultProxyConfig().enableRegister(false);
147 lc.getDefaultProxyConfig().done();
149 for (int i = 0; i < 20; i++) {
153 } catch(InterruptedException ie) {
154 write("Interrupted!\nAborting");
159 lc.getDefaultProxyConfig().edit();
160 lc.getDefaultProxyConfig().enableRegister(true);
161 lc.getDefaultProxyConfig().done();
163 for (int i = 0; i < 20; i++) {
167 } catch(InterruptedException ie) {
168 write("Interrupted!\nAborting");
174 // Automatic unregistration on exit
176 write("Shutting down linphone...");
177 // You need to destroy the LinphoneCore object when no longer used
183 public void stopMainLoop() {
188 private void write(String s) {
189 TutorialNotifier.notify(s);