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