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