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