]> sjero.net Git - linphone/blob - coreapi/help/java/org/linphone/core/tutorials/TutorialBuddyStatus.java
Fix java tutorials
[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         public void notifyReceived(LinphoneCore lc, LinphoneCall call, LinphoneAddress from, byte[] event){}
102
103
104         public static void main(String[] args) {
105                 // Check tutorial was called with the right number of arguments
106                 if (args.length < 1 || args.length > 3 ) {
107                         throw new IllegalArgumentException("Bad number of arguments ["+args.length+"] should be 1, 2 or 3");
108                 }
109
110                 // Create tutorial object
111                 TutorialBuddyStatus tutorial = new TutorialBuddyStatus();
112                 try {
113                         // takes sip uri identity from the command line arguments 
114                         String userSipAddress = args[1];
115                         
116                         // takes sip uri identity from the command line arguments
117                         String mySipAddress = args.length>1?args[1]:null;
118                         // takes password from the command line arguments
119                         String mySipPassword =args.length>2?args[2]:null;
120
121                         tutorial.launchTutorial(userSipAddress,mySipAddress,mySipPassword);
122                 } catch (Exception e) {
123                         e.printStackTrace();
124                 }
125         }
126
127
128
129         public void launchTutorial(String sipAddress,String mySipAddress, String mySipPassword) throws LinphoneCoreException {
130                 final LinphoneCoreFactory lcFactory = LinphoneCoreFactory.instance();
131
132                 // First instantiate the core Linphone object given only a listener.
133                 // The listener will react to events in Linphone core.
134                 LinphoneCore lc = lcFactory.createLinphoneCore(this);
135
136
137                 try {
138
139                         // Create friend object from string address
140                         LinphoneFriend lf = lcFactory.createLinphoneFriend(sipAddress);
141                         if (lf == null) {
142                                 write("Could not create friend; weird SIP address?");
143                                 return;
144                         }
145
146                         if (mySipAddress != null) {
147                                 // Parse identity
148                                 LinphoneAddress address = lcFactory.createLinphoneAddress(mySipAddress);
149                                 String username = address.getUserName();
150                                 String domain = address.getDomain();
151
152
153                                 if (mySipPassword != null) {
154                                         // create authentication structure from identity and add to linphone
155                                         lc.addAuthInfo(lcFactory.createAuthInfo(username, mySipPassword, null));
156                                 }
157
158                                 // create proxy config
159                                 LinphoneProxyConfig proxyCfg = lcFactory.createProxyConfig(mySipAddress, domain, null, true);
160                                 proxyCfg.enablePublish(true);
161                                 lc.addProxyConfig(proxyCfg); // add it to linphone
162                                 lc.setDefaultProxyConfig(proxyCfg);
163                                 while (!proxyCfg.isRegistered()) {
164                                         lc.iterate(); //iterate until registration
165                                         try{
166                                                 Thread.sleep(1000);
167                                         } catch(InterruptedException ie) {
168                                                 write("Interrupted!\nAborting");
169                                                 return;
170                                         }
171                                 }
172                         }
173                         
174                         // configure this friend to emit SUBSCRIBE message after being added to LinphoneCore
175                         lf.enableSubscribes(true);
176                         
177                         // accept incoming subscription request for this friend
178                         lf.setIncSubscribePolicy(SubscribePolicy.SPAccept);
179                         try {
180                                 // add my friend to the buddy list, initiate SUBSCRIBE message
181                                 lc.addFriend(lf);
182                         } catch (LinphoneCoreException e) {
183                                 write("Error while adding friend " + lf.getAddress().getUserName() + " to linphone");
184                                 return;
185                         }
186                         
187                         // set my status to online 
188                         lc.setPresenceInfo(0, null, OnlineStatus.Online);
189                         
190                         
191                         // main loop for receiving notifications and doing background linphonecore work
192                         running = true;
193                         while (running) {
194                                 lc.iterate(); // first iterate initiates subscription
195                                 try{
196                                         Thread.sleep(50);
197                                 } catch(InterruptedException ie) {
198                                         write("Interrupted!\nAborting");
199                                         return;
200                                 }
201                         }
202
203
204                         // change my presence status to offline
205                         lc.setPresenceInfo(0, null, OnlineStatus.Offline);
206                         // just to make sure new status is initiate message is issued
207                         lc.iterate();
208
209                         
210                         lf.edit(); // start editing friend 
211                         lf.enableSubscribes(false); // disable subscription for this friend
212                         lf.done(); // commit changes triggering an UNSUBSCRIBE message
213                         lc.iterate(); // just to make sure unsubscribe message is issued
214
215
216                 } finally {
217                         write("Shutting down...");
218                         // You need to destroy the LinphoneCore object when no longer used
219                         lc.destroy();
220                         write("Exited");
221                 }
222         }
223
224
225         public void stopMainLoop() {
226                 running=false;
227         }
228
229
230         private void write(String s) {
231                 TutorialNotifier.notify(s);
232         }
233
234
235 }