]> sjero.net Git - linphone/blob - coreapi/help/java/org/linphone/core/tutorials/TutorialBuddyStatus.java
e7db170b86e719cb9a7450a919929e66aafe8d96
[linphone] / coreapi / help / java / org / linphone / core / tutorials / TutorialBuddyStatus.java
1 /*
2 TutorialBuddyStatus
3 Copyright (C) 2010  Belledonne Communications SARL 
4
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.
9
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.
14
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.
18  */
19 package org.linphone.core.tutorials;
20
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;
36
37 /**
38  * 
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
44  * <br>
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
47  *
48  * Ported from buddy_status.c
49  *
50  * @author Guillaume Beraudo
51  *
52  */
53 public class TutorialBuddyStatus implements LinphoneCoreListener {
54
55         private boolean running;
56         private TutorialNotifier TutorialNotifier;
57
58
59         public TutorialBuddyStatus(TutorialNotifier TutorialNotifier) {
60                 this.TutorialNotifier = TutorialNotifier;
61         }
62
63         public TutorialBuddyStatus() {
64                 this.TutorialNotifier = new TutorialNotifier();
65         }
66
67
68
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
74                 try {
75                         // add this new friend to the buddy list
76                         lc.addFriend(lf);
77                 } catch (LinphoneCoreException e) {
78                         write("Error while adding friend [" + lf.getAddress().getUserName() + "] to linphone in the callback");
79                 }
80         }
81
82         public void notifyPresenceReceived(LinphoneCore lc, LinphoneFriend lf) {
83                 write("New state [" + lf.getStatus() +"] for user id ["+lf.getAddress().getUserName()+"]");
84         }
85
86
87         public void registrationState(LinphoneCore lc, LinphoneProxyConfig cfg,RegistrationState cstate, String smessage) {
88                 write(cfg.getIdentity() + " : "+smessage+"\n");
89         }
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) {}
101
102
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");
107                 }
108
109                 // Create tutorial object
110                 TutorialBuddyStatus tutorial = new TutorialBuddyStatus();
111                 try {
112                         // takes sip uri identity from the command line arguments 
113                         String userSipAddress = args[1];
114                         
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;
119
120                         tutorial.launchTutorial(userSipAddress,mySipAddress,mySipPassword);
121                 } catch (Exception e) {
122                         e.printStackTrace();
123                 }
124         }
125
126
127
128         public void launchTutorial(String sipAddress,String mySipAddress, String mySipPassword) throws LinphoneCoreException {
129                 final LinphoneCoreFactory lcFactory = LinphoneCoreFactory.instance();
130
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);
134
135
136                 try {
137
138                         // Create friend object from string address
139                         LinphoneFriend lf = lcFactory.createLinphoneFriend(sipAddress);
140                         if (lf == null) {
141                                 write("Could not create friend; weird SIP address?");
142                                 return;
143                         }
144
145                         if (mySipAddress != null) {
146                                 // Parse identity
147                                 LinphoneAddress address = lcFactory.createLinphoneAddress(mySipAddress);
148                                 String username = address.getUserName();
149                                 String domain = address.getDomain();
150
151
152                                 if (mySipPassword != null) {
153                                         // create authentication structure from identity and add to linphone
154                                         lc.addAuthInfo(lcFactory.createAuthInfo(username, mySipPassword, null));
155                                 }
156
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
164                                         try{
165                                                 Thread.sleep(1000);
166                                         } catch(InterruptedException ie) {
167                                                 write("Interrupted!\nAborting");
168                                                 return;
169                                         }
170                                 }
171                         }
172                         
173                         // configure this friend to emit SUBSCRIBE message after being added to LinphoneCore
174                         lf.enableSubscribes(true);
175                         
176                         // accept incoming subscription request for this friend
177                         lf.setIncSubscribePolicy(SubscribePolicy.SPAccept);
178                         try {
179                                 // add my friend to the buddy list, initiate SUBSCRIBE message
180                                 lc.addFriend(lf);
181                         } catch (LinphoneCoreException e) {
182                                 write("Error while adding friend " + lf.getAddress().getUserName() + " to linphone");
183                                 return;
184                         }
185                         
186                         // set my status to online 
187                         lc.setPresenceInfo(0, null, OnlineStatus.Online);
188                         
189                         
190                         // main loop for receiving notifications and doing background linphonecore work
191                         running = true;
192                         while (running) {
193                                 lc.iterate(); // first iterate initiates subscription
194                                 try{
195                                         Thread.sleep(50);
196                                 } catch(InterruptedException ie) {
197                                         write("Interrupted!\nAborting");
198                                         return;
199                                 }
200                         }
201
202
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
206                         lc.iterate();
207
208                         
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
213
214
215                 } finally {
216                         write("Shutting down...");
217                         // You need to destroy the LinphoneCore object when no longer used
218                         lc.destroy();
219                         write("Exited");
220                 }
221         }
222
223
224         public void stopMainLoop() {
225                 running=false;
226         }
227
228
229         private void write(String s) {
230                 TutorialNotifier.notify(s);
231         }
232
233
234 }