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.LinphoneCore.EcCalibratorStatus;
26 import org.linphone.core.LinphoneCoreException;
27 import org.linphone.core.LinphoneCoreFactory;
28 import org.linphone.core.LinphoneCoreListener;
29 import org.linphone.core.LinphoneFriend;
30 import org.linphone.core.LinphoneProxyConfig;
31 import org.linphone.core.OnlineStatus;
32 import org.linphone.core.LinphoneCall.State;
33 import org.linphone.core.LinphoneCore.GlobalState;
34 import org.linphone.core.LinphoneCore.RegistrationState;
35 import org.linphone.core.LinphoneFriend.SubscribePolicy;
39 * This program is a _very_ simple usage example of liblinphone,
40 * demonstrating how to initiate SIP subscriptions and receive notifications
41 * from a sip uri identity passed from the command line.
42 * <br>Argument must be like sip:jehan@sip.linphone.org .
43 * ex budy_list sip:jehan@sip.linphone.org
45 * Optionnally argument 2 can be registration sip identy.Argument 3 can be passord.
46 * ex: budy_list sip:jehan@sip.linphone.org sip:myidentity@sip.linphone.org mypassword
48 * Ported from buddy_status.c
50 * @author Guillaume Beraudo
53 public class TutorialBuddyStatus implements LinphoneCoreListener {
55 private boolean running;
56 private TutorialNotifier TutorialNotifier;
59 public TutorialBuddyStatus(TutorialNotifier TutorialNotifier) {
60 this.TutorialNotifier = TutorialNotifier;
63 public TutorialBuddyStatus() {
64 this.TutorialNotifier = new TutorialNotifier();
69 public void newSubscriptionRequest(LinphoneCore lc, LinphoneFriend lf,String url) {
70 write("["+lf.getAddress().getUserName()+"] wants to see your status, accepting");
71 lf.edit(); // start editing friend
72 lf.setIncSubscribePolicy(SubscribePolicy.SPAccept); // accept incoming subscription request for this friend
73 lf.done(); // commit change
75 // add this new friend to the buddy list
77 } catch (LinphoneCoreException e) {
78 write("Error while adding friend [" + lf.getAddress().getUserName() + "] to linphone in the callback");
82 public void notifyPresenceReceived(LinphoneCore lc, LinphoneFriend lf) {
83 write("New state [" + lf.getStatus() +"] for user id ["+lf.getAddress().getUserName()+"]");
87 public void registrationState(LinphoneCore lc, LinphoneProxyConfig cfg,RegistrationState cstate, String smessage) {
88 write(cfg.getIdentity() + " : "+smessage+"\n");
90 public void show(LinphoneCore lc) {}
91 public void byeReceived(LinphoneCore lc, String from) {}
92 public void authInfoRequested(LinphoneCore lc, String realm, String username) {}
93 public void displayStatus(LinphoneCore lc, String message) {}
94 public void displayMessage(LinphoneCore lc, String message) {}
95 public void displayWarning(LinphoneCore lc, String message) {}
96 public void globalState(LinphoneCore lc, GlobalState state, String message) {}
97 public void textReceived(LinphoneCore lc, LinphoneChatRoom cr,LinphoneAddress from, String message) {}
98 public void callState(LinphoneCore lc, LinphoneCall call, State cstate, String msg) {}
99 public void ecCalibrationStatus(LinphoneCore lc, EcCalibratorStatus status,int delay_ms, Object data) {}
100 public void callEncryptionChanged(LinphoneCore lc, LinphoneCall call,boolean encrypted, String authenticationToken) {}
103 public static void main(String[] args) {
104 // Check tutorial was called with the right number of arguments
105 if (args.length < 1 || args.length > 3 ) {
106 throw new IllegalArgumentException("Bad number of arguments ["+args.length+"] should be 1, 2 or 3");
109 // Create tutorial object
110 TutorialBuddyStatus tutorial = new TutorialBuddyStatus();
112 // takes sip uri identity from the command line arguments
113 String userSipAddress = args[1];
115 // takes sip uri identity from the command line arguments
116 String mySipAddress = args.length>1?args[1]:null;
117 // takes password from the command line arguments
118 String mySipPassword =args.length>2?args[2]:null;
120 tutorial.launchTutorial(userSipAddress,mySipAddress,mySipPassword);
121 } catch (Exception e) {
128 public void launchTutorial(String sipAddress,String mySipAddress, String mySipPassword) throws LinphoneCoreException {
129 final LinphoneCoreFactory lcFactory = LinphoneCoreFactory.instance();
131 // First instantiate the core Linphone object given only a listener.
132 // The listener will react to events in Linphone core.
133 LinphoneCore lc = lcFactory.createLinphoneCore(this);
138 // Create friend object from string address
139 LinphoneFriend lf = lcFactory.createLinphoneFriend(sipAddress);
141 write("Could not create friend; weird SIP address?");
145 if (mySipAddress != null) {
147 LinphoneAddress address = lcFactory.createLinphoneAddress(mySipAddress);
148 String username = address.getUserName();
149 String domain = address.getDomain();
152 if (mySipPassword != null) {
153 // create authentication structure from identity and add to linphone
154 lc.addAuthInfo(lcFactory.createAuthInfo(username, mySipPassword, null));
157 // create proxy config
158 LinphoneProxyConfig proxyCfg = lcFactory.createProxyConfig(mySipAddress, domain, null, true);
159 proxyCfg.enablePublish(true);
160 lc.addProxyConfig(proxyCfg); // add it to linphone
161 lc.setDefaultProxyConfig(proxyCfg);
162 while (!proxyCfg.isRegistered()) {
163 lc.iterate(); //iterate until registration
166 } catch(InterruptedException ie) {
167 write("Interrupted!\nAborting");
173 // configure this friend to emit SUBSCRIBE message after being added to LinphoneCore
174 lf.enableSubscribes(true);
176 // accept incoming subscription request for this friend
177 lf.setIncSubscribePolicy(SubscribePolicy.SPAccept);
179 // add my friend to the buddy list, initiate SUBSCRIBE message
181 } catch (LinphoneCoreException e) {
182 write("Error while adding friend " + lf.getAddress().getUserName() + " to linphone");
186 // set my status to online
187 lc.setPresenceInfo(0, null, OnlineStatus.Online);
190 // main loop for receiving notifications and doing background linphonecore work
193 lc.iterate(); // first iterate initiates subscription
196 } catch(InterruptedException ie) {
197 write("Interrupted!\nAborting");
203 // change my presence status to offline
204 lc.setPresenceInfo(0, null, OnlineStatus.Offline);
205 // just to make sure new status is initiate message is issued
209 lf.edit(); // start editing friend
210 lf.enableSubscribes(false); // disable subscription for this friend
211 lf.done(); // commit changes triggering an UNSUBSCRIBE message
212 lc.iterate(); // just to make sure unsubscribe message is issued
216 write("Shutting down...");
217 // You need to destroy the LinphoneCore object when no longer used
224 public void stopMainLoop() {
229 private void write(String s) {
230 TutorialNotifier.notify(s);