]> sjero.net Git - linphone/blob - coreapi/help/java/org/linphone/core/tutorials/TutorialRegistration.java
bugfixes again for bandwidth management
[linphone] / coreapi / help / java / org / linphone / core / tutorials / TutorialRegistration.java
1 /*
2 TutorialRegistration.java
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.LinphoneCall.State;
31 import org.linphone.core.LinphoneCore.GlobalState;
32 import org.linphone.core.LinphoneCore.RegistrationState;
33
34
35 /**
36  * This program is a _very_ simple usage example of liblinphone.
37  * Demonstrating how to initiate a SIP registration from a sip uri identity
38  * passed from the command line.
39  *
40  * First argument must be like sip:jehan@sip.linphone.org, second must be password.
41  * <br>
42  * ex registration sip:jehan@sip.linphone.org secret
43  *
44  * Ported from registration.c
45  *
46  * @author Guillaume Beraudo
47  *
48  */
49 public class TutorialRegistration implements LinphoneCoreListener {
50         private boolean running;
51         private TutorialNotifier TutorialNotifier;
52
53
54         public TutorialRegistration(TutorialNotifier TutorialNotifier) {
55                 this.TutorialNotifier = TutorialNotifier;
56         }
57
58         public TutorialRegistration() {
59                 this.TutorialNotifier = new TutorialNotifier();
60         }
61
62         
63         /*
64          * Registration state notification listener
65          */
66         public void registrationState(LinphoneCore lc, LinphoneProxyConfig cfg,RegistrationState cstate, String smessage) {
67                 write(cfg.getIdentity() + " : "+smessage+"\n");
68         }
69
70         public void show(LinphoneCore lc) {}
71         public void byeReceived(LinphoneCore lc, String from) {}
72         public void authInfoRequested(LinphoneCore lc, String realm, String username) {}
73         public void displayStatus(LinphoneCore lc, String message) {}
74         public void displayMessage(LinphoneCore lc, String message) {}
75         public void displayWarning(LinphoneCore lc, String message) {}
76         public void globalState(LinphoneCore lc, GlobalState state, String message) {}
77         public void newSubscriptionRequest(LinphoneCore lc, LinphoneFriend lf,String url) {}
78         public void notifyPresenceReceived(LinphoneCore lc, LinphoneFriend lf) {}
79         public void textReceived(LinphoneCore lc, LinphoneChatRoom cr,LinphoneAddress from, String message) {}
80         public void callState(LinphoneCore lc, LinphoneCall call, State cstate, String msg) {}
81
82
83         public static void main(String[] args) {
84                 // Check tutorial was called with the right number of arguments
85                 if (args.length != 2) {
86                         throw new IllegalArgumentException("Bad number of arguments");
87                 }
88                 
89                 // Create tutorial object
90                 TutorialRegistration tutorial = new TutorialRegistration();
91                 try {
92                         // takes sip uri identity from the command line arguments
93                         String userSipAddress = args[1];
94                         // takes password from the command line arguments
95                         String userSipPassword = args[2];
96                         tutorial.launchTutorial(userSipAddress, userSipPassword);
97                 } catch (Exception e) {
98                         e.printStackTrace();
99                 }
100         }
101
102         
103         
104         public void launchTutorial(String sipAddress, String password) throws LinphoneCoreException {
105                 final LinphoneCoreFactory lcFactory = LinphoneCoreFactory.instance();
106
107                 // First instantiate the core Linphone object given only a listener.
108                 // The listener will react to events in Linphone core.
109                 LinphoneCore lc = lcFactory.createLinphoneCore(this);
110
111         
112                 try {
113                         
114                         // Parse identity
115                         LinphoneAddress address = lcFactory.createLinphoneAddress(sipAddress);
116                         String username = address.getUserName();
117                         String domain = address.getDomain();
118
119
120                         if (password != null) {
121                                 // create authentication structure from identity and add to linphone
122                                 lc.addAuthInfo(lcFactory.createAuthInfo(username, password, null));
123                         }
124
125                         // create proxy config
126                         LinphoneProxyConfig proxyCfg = lcFactory.createProxyConfig(sipAddress, domain, null, true);
127                         lc.addProxyConfig(proxyCfg); // add it to linphone
128                         lc.setDefaultProxyConfig(proxyCfg);
129
130                         
131                         
132                         // main loop for receiving notifications and doing background linphonecore work
133                         running = true;
134                         while (running) {
135                                 lc.iterate(); // first iterate initiates registration 
136                                 try{
137                                         Thread.sleep(50);
138                                 } catch(InterruptedException ie) {
139                                         write("Interrupted!\nAborting");
140                                         return;
141                                 }
142                         }
143
144                         // Unregister then register again
145                         lc.getDefaultProxyConfig().edit();
146                         lc.getDefaultProxyConfig().enableRegister(false);
147                         lc.getDefaultProxyConfig().done();
148                         
149                         for (int i = 0; i < 20; i++) {
150                                 lc.iterate();
151                                 try{
152                                         Thread.sleep(50);
153                                 } catch(InterruptedException ie) {
154                                         write("Interrupted!\nAborting");
155                                         return;
156                                 }
157                         }
158
159                         lc.getDefaultProxyConfig().edit();
160                         lc.getDefaultProxyConfig().enableRegister(true);
161                         lc.getDefaultProxyConfig().done();
162
163                         for (int i = 0; i < 20; i++) {
164                                 lc.iterate();
165                                 try{
166                                         Thread.sleep(50);
167                                 } catch(InterruptedException ie) {
168                                         write("Interrupted!\nAborting");
169                                         return;
170                                 }
171                         }
172
173                         
174                         // Automatic unregistration on exit
175                 } finally {
176                         write("Shutting down linphone...");
177                         // You need to destroy the LinphoneCore object when no longer used
178                         lc.destroy();
179                 }
180         }
181
182
183         public void stopMainLoop() {
184                 running=false;
185         }
186
187         
188         private void write(String s) {
189                 TutorialNotifier.notify(s);
190         }
191
192 }