]> sjero.net Git - linphone/blob - coreapi/help/java/org/linphone/core/tutorials/TutorialRegistration.java
Merge commit '2e953b4'
[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                         proxyCfg.setExpires(2000);
128                         lc.addProxyConfig(proxyCfg); // add it to linphone
129                         lc.setDefaultProxyConfig(proxyCfg);
130
131
132                         
133                         // main loop for receiving notifications and doing background linphonecore work
134                         running = true;
135                         while (running) {
136                                 lc.iterate(); // first iterate initiates registration 
137                                 sleep(50);
138                         }
139
140
141                         // Unregister
142                         lc.getDefaultProxyConfig().edit();
143                         lc.getDefaultProxyConfig().enableRegister(false);
144                         lc.getDefaultProxyConfig().done();
145                         while(lc.getDefaultProxyConfig().getState() != RegistrationState.RegistrationCleared) {
146                                 lc.iterate();
147                                 sleep(50);
148                         }
149
150                         // Then register again
151                         lc.getDefaultProxyConfig().edit();
152                         lc.getDefaultProxyConfig().enableRegister(true);
153                         lc.getDefaultProxyConfig().done();
154
155                         while(lc.getDefaultProxyConfig().getState() != RegistrationState.RegistrationOk
156                                         && lc.getDefaultProxyConfig().getState() != RegistrationState.RegistrationFailed) {
157                                 lc.iterate();
158                                 sleep(50);
159                         }
160
161                         // Automatic unregistration on exit
162                 } finally {
163                         write("Shutting down linphone...");
164                         // You need to destroy the LinphoneCore object when no longer used
165                         lc.destroy();
166                 }
167         }
168
169         private void sleep(int ms) {
170                 try {
171                         Thread.sleep(ms);
172                 } catch(InterruptedException ie) {
173                         write("Interrupted!\nAborting");
174                         return;
175                 }
176         }
177
178         public void stopMainLoop() {
179                 running=false;
180         }
181
182         
183         private void write(String s) {
184                 TutorialNotifier.notify(s);
185         }
186
187 }