]> sjero.net Git - linphone/blob - coreapi/help/java/org/linphone/core/tutorials/TutorialChatRoom.java
backward compat fixes
[linphone] / coreapi / help / java / org / linphone / core / tutorials / TutorialChatRoom.java
1 /*
2 TutorialChatRoom.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  * It demonstrates how to send/receive SIP MESSAGE from a sip uri identity
40  * passed from the command line.
41  *
42  * Argument must be like sip:jehan@sip.linphone.org .
43  *
44  * ex chatroom sip:jehan@sip.linphone.org
45  * just takes a sip-uri as first argument and attempts to call it.
46  * 
47  * Ported from chatroom.c
48  *
49  * @author Guillaume Beraudo
50  *
51  */
52 public class TutorialChatRoom implements LinphoneCoreListener {
53         private boolean running;
54         private TutorialNotifier TutorialNotifier;
55
56
57         public TutorialChatRoom(TutorialNotifier TutorialNotifier) {
58                 this.TutorialNotifier = TutorialNotifier;
59         }
60
61         public TutorialChatRoom() {
62                 this.TutorialNotifier = new TutorialNotifier();
63         }
64
65         
66         
67         public void show(LinphoneCore lc) {}
68         public void byeReceived(LinphoneCore lc, String from) {}
69         public void authInfoRequested(LinphoneCore lc, String realm, String username) {}
70         public void displayStatus(LinphoneCore lc, String message) {}
71         public void displayMessage(LinphoneCore lc, String message) {}
72         public void displayWarning(LinphoneCore lc, String message) {}
73         public void globalState(LinphoneCore lc, GlobalState state, String message) {}
74         public void registrationState(LinphoneCore lc, LinphoneProxyConfig cfg,RegistrationState cstate, String smessage) {}
75         public void newSubscriptionRequest(LinphoneCore lc, LinphoneFriend lf,String url) {}
76         public void notifyPresenceReceived(LinphoneCore lc, LinphoneFriend lf) {}
77         public void callState(LinphoneCore lc, LinphoneCall call, State cstate, String msg){}
78         public void ecCalibrationStatus(LinphoneCore lc, EcCalibratorStatus status,int delay_ms, Object data) {}
79         public void callEncryptionChanged(LinphoneCore lc, LinphoneCall call,boolean encrypted, String authenticationToken) {}
80         public void notifyReceived(LinphoneCore lc, LinphoneCall call, LinphoneAddress from, byte[] event){}
81         
82         public void textReceived(LinphoneCore lc, LinphoneChatRoom cr,LinphoneAddress from, String message) {
83         write("Message ["+message+"] received from ["+from.asString()+"]");
84         }
85
86         
87         
88         public static void main(String[] args) {
89                 // Check tutorial was called with the right number of arguments
90                 // Takes the sip uri identity from the command line arguments
91                 if (args.length != 1) {
92                         throw new IllegalArgumentException("Bad number of arguments");
93                 }
94                 
95                 // Create tutorial object
96                 TutorialChatRoom tutorial = new TutorialChatRoom();
97                 try {
98                         String destinationSipAddress = args[1];
99                         tutorial.launchTutorial(destinationSipAddress);
100                 } catch (Exception e) {
101                         e.printStackTrace();
102                 }
103         }
104
105         
106         
107         public void launchTutorial(String destinationSipAddress) throws LinphoneCoreException {
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 = LinphoneCoreFactory.instance().createLinphoneCore(this);
112
113                 try {
114                         // Next step is to create a chat room
115                         LinphoneChatRoom chatRoom = lc.createChatRoom(destinationSipAddress);
116                         
117                         // Send message
118                         chatRoom.sendMessage("Hello world");
119                         
120                         // main loop for receiving notifications and doing background linphonecore work
121                         running = true;
122                         while (running) {
123                                 lc.iterate();
124                                 try{
125                                         Thread.sleep(50);
126                                 } catch(InterruptedException ie) {
127                                         write("Interrupted!\nAborting");
128                                         return;
129                                 }
130                         }
131
132                 } finally {
133                         write("Shutting down...");
134                         // You need to destroy the LinphoneCore object when no longer used
135                         lc.destroy();
136                         write("Exited");
137                 }
138         }
139
140
141         public void stopMainLoop() {
142                 running=false;
143         }
144
145         
146         private void write(String s) {
147                 TutorialNotifier.notify(s);
148         }
149
150         @Override
151         public void messageReceived(LinphoneCore lc, LinphoneChatRoom cr,
152                         LinphoneChatMessage message) {
153                 // TODO Auto-generated method stub
154                 
155         }
156
157
158 }