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