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