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.OnlineStatus;
31 import org.linphone.core.LinphoneCall.State;
32 import org.linphone.core.LinphoneCore.GlobalState;
33 import org.linphone.core.LinphoneCore.RegistrationState;
34 import org.linphone.core.LinphoneFriend.SubscribePolicy;
38 * This program is a _very_ simple usage example of liblinphone,
39 * demonstrating how to initiate SIP subscriptions and receive notifications
40 * from a sip uri identity passed from the command line.
41 * <br>Argument must be like sip:jehan@sip.linphone.org .
42 * ex budy_list sip:jehan@sip.linphone.org
43 * <br>Subscription is cleared on SIGINT
45 * Ported from buddy_status.c
47 * @author Guillaume Beraudo
50 public class TutorialBuddyStatus implements LinphoneCoreListener {
52 private boolean running;
53 private TutorialNotifier TutorialNotifier;
56 public TutorialBuddyStatus(TutorialNotifier TutorialNotifier) {
57 this.TutorialNotifier = TutorialNotifier;
60 public TutorialBuddyStatus() {
61 this.TutorialNotifier = new TutorialNotifier();
66 public void newSubscriptionRequest(LinphoneCore lc, LinphoneFriend lf,String url) {
67 write("["+lf.getAddress().getUserName()+"] wants to see your status, accepting");
68 lf.edit(); // start editing friend
69 lf.setIncSubscribePolicy(SubscribePolicy.SPAccept); // accept incoming subscription request for this friend
70 lf.done(); // commit change
72 // add this new friend to the buddy list
74 } catch (LinphoneCoreException e) {
75 write("Error while adding friend [" + lf.getAddress().getUserName() + "] to linphone in the callback");
79 public void notifyPresenceReceived(LinphoneCore lc, LinphoneFriend lf) {
80 write("New state [" + lf.getStatus() +"] for user id ["+lf.getAddress().getUserName()+"]");
84 public void registrationState(LinphoneCore lc, LinphoneProxyConfig cfg,RegistrationState cstate, String smessage) {
85 write(cfg.getIdentity() + " : "+smessage+"\n");
87 public void show(LinphoneCore lc) {}
88 public void byeReceived(LinphoneCore lc, String from) {}
89 public void authInfoRequested(LinphoneCore lc, String realm, String username) {}
90 public void displayStatus(LinphoneCore lc, String message) {}
91 public void displayMessage(LinphoneCore lc, String message) {}
92 public void displayWarning(LinphoneCore lc, String message) {}
93 public void globalState(LinphoneCore lc, GlobalState state, String message) {}
94 public void textReceived(LinphoneCore lc, LinphoneChatRoom cr,LinphoneAddress from, String message) {}
95 public void callState(LinphoneCore lc, LinphoneCall call, State cstate, String msg) {}
100 public static void main(String[] args) {
101 // Check tutorial was called with the right number of arguments
102 if (args.length < 1 || args.length > 3 ) {
103 throw new IllegalArgumentException("Bad number of arguments ["+args.length+"] should be 1, 2 or 3");
106 // Create tutorial object
107 TutorialBuddyStatus tutorial = new TutorialBuddyStatus();
109 // takes sip uri identity from the command line arguments
110 String userSipAddress = args[1];
112 // takes sip uri identity from the command line arguments
113 String mySipAddress = args.length>1?args[1]:null;
114 // takes password from the command line arguments
115 String mySipPassword =args.length>2?args[2]:null;
117 tutorial.launchTutorial(userSipAddress,mySipAddress,mySipPassword);
118 } catch (Exception e) {
125 public void launchTutorial(String sipAddress,String mySipAddress, String mySipPassword) throws LinphoneCoreException {
126 final LinphoneCoreFactory lcFactory = LinphoneCoreFactory.instance();
128 // First instantiate the core Linphone object given only a listener.
129 // The listener will react to events in Linphone core.
130 LinphoneCore lc = lcFactory.createLinphoneCore(this);
135 // Create friend object from string address
136 LinphoneFriend lf = lcFactory.createLinphoneFriend(sipAddress);
138 write("Could not create friend; weird SIP address?");
142 if (mySipAddress != null) {
144 LinphoneAddress address = lcFactory.createLinphoneAddress(mySipAddress);
145 String username = address.getUserName();
146 String domain = address.getDomain();
149 if (mySipPassword != null) {
150 // create authentication structure from identity and add to linphone
151 lc.addAuthInfo(lcFactory.createAuthInfo(username, mySipPassword, null));
154 // create proxy config
155 LinphoneProxyConfig proxyCfg = lcFactory.createProxyConfig(mySipAddress, domain, null, true);
156 proxyCfg.enablePublish(true);
157 lc.addProxyConfig(proxyCfg); // add it to linphone
158 lc.setDefaultProxyConfig(proxyCfg);
159 while (!proxyCfg.isRegistered()) {
160 lc.iterate(); //iterate until registration
163 } catch(InterruptedException ie) {
164 write("Interrupted!\nAborting");
170 // configure this friend to emit SUBSCRIBE message after being added to LinphoneCore
171 lf.enableSubscribes(true);
173 // accept incoming subscription request for this friend
174 lf.setIncSubscribePolicy(SubscribePolicy.SPAccept);
176 // add my friend to the buddy list, initiate SUBSCRIBE message
178 } catch (LinphoneCoreException e) {
179 write("Error while adding friend " + lf.getAddress().getUserName() + " to linphone");
183 // set my status to online
184 lc.setPresenceInfo(0, null, OnlineStatus.Online);
187 // main loop for receiving notifications and doing background linphonecore work
190 lc.iterate(); // first iterate initiates subscription
193 } catch(InterruptedException ie) {
194 write("Interrupted!\nAborting");
200 // change my presence status to offline
201 lc.setPresenceInfo(0, null, OnlineStatus.Offline);
202 // just to make sure new status is initiate message is issued
206 lf.edit(); // start editing friend
207 lf.enableSubscribes(false); // disable subscription for this friend
208 lf.done(); // commit changes triggering an UNSUBSCRIBE message
209 lc.iterate(); // just to make sure unsubscribe message is issued
213 write("Shutting down...");
214 // You need to destroy the LinphoneCore object when no longer used
221 public void stopMainLoop() {
226 private void write(String s) {
227 TutorialNotifier.notify(s);