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