KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > audioconf > AudioConf


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.audioconf;
20
21 import org.lucane.applications.audioconf.audio.*;
22 import org.lucane.applications.audioconf.gui.*;
23 import org.lucane.client.*;
24 import org.lucane.client.widgets.DialogBox;
25 import org.lucane.common.*;
26 import org.lucane.common.net.ObjectConnection;
27
28 public class AudioConf extends Plugin
29 {
30     private ConnectInfo friend;
31     private ObjectConnection connection;
32     
33     private AudioRecorder recorder;
34     private AudioPlayer player;
35     
36     private Controller controller;
37     
38     private boolean stopped = false;
39     
40     public AudioConf()
41     {
42         //empty constructor for pluginloader
43
}
44     
45     private AudioConf(ConnectInfo friend)
46     {
47         this.friend = friend;
48     }
49     
50
51     public Plugin newInstance(ConnectInfo[] friends) {
52         return new AudioConf(friends.length > 0 ? friends[0] : null);
53     }
54     
55     public void load(ObjectConnection oc, ConnectInfo who, String JavaDoc data)
56     {
57         this.connection = oc;
58         this.friend = who;
59     }
60
61     public void start()
62     {
63         if(friend == null)
64         {
65             DialogBox.info(tr("err.noUserSelected"));
66             exit();
67             return;
68         }
69         
70         ConfigDialog cd = new ConfigDialog(this);
71         cd.show();
72     }
73
74     public void follow()
75     {
76         Logging.getLogger().info("receiving audio stream from " +friend.getName());
77         
78         try {
79             AudioConfig config = (AudioConfig)this.connection.read();
80             
81             String JavaDoc msg = tr("msg.acceptFrom");
82             msg = msg.replaceAll("%1", getFriendName());
83             
84             boolean accept = DialogBox.question(getTitle(), msg);
85             this.connection.write(Boolean.valueOf(accept));
86             
87             if(accept)
88                 startAll(config);
89         } catch (Exception JavaDoc e) {
90             e.printStackTrace();
91         }
92     }
93     
94     public void waitForAccept(AudioConfig config)
95     {
96         this.connection = Communicator.getInstance().sendMessageTo(this.friend, this.getName(), "");
97         try {
98             this.connection.write(config);
99             Boolean JavaDoc accepted = (Boolean JavaDoc)this.connection.read();
100             if(accepted.booleanValue())
101                 startAll(config);
102             else
103             {
104                 String JavaDoc msg = tr("msg.rejectedBy");
105                 msg = msg.replaceAll("%1", getFriendName());
106                 DialogBox.info(msg);
107             }
108         } catch (Exception JavaDoc e) {
109             DialogBox.info(tr("err.accept"));
110             e.printStackTrace();
111         }
112     }
113     
114     public void startRecorder(AudioConfig config)
115     {
116         recorder = new AudioRecorder(config);
117         recorder.addAudioListener(new Streamer(this, connection));
118         Thread JavaDoc thread = new Thread JavaDoc(recorder);
119         thread.start();
120     }
121     
122     public void startPlayer(AudioConfig config)
123     {
124         player = new AudioPlayer(this, config, new AudioConfInputStream(this, connection));
125         Thread JavaDoc thread = new Thread JavaDoc(player);
126         thread.start();
127     }
128     
129     //-- error handling
130

131     public void reportRecorderError(Exception JavaDoc e)
132     {
133         stopAndExit();
134     }
135     
136     public void reportPlayerError(Exception JavaDoc e)
137     {
138         stopAndExit();
139     }
140     
141     //-- for controller
142

143     public void startAll(AudioConfig config)
144     {
145         startPlayer(config);
146         startRecorder(config);
147         controller = new Controller(this);
148         controller.showController();
149     }
150     
151     public String JavaDoc getFriendName()
152     {
153         return friend.getName();
154     }
155     
156     public void stopAndExit()
157     {
158         //avoid infinite loop
159
if(this.stopped)
160             return;
161         this.stopped = true;
162             
163         Logging.getLogger().info("Stopping AudioConf");
164         
165         recorder.stop();
166         player.stop();
167         controller.hideController();
168         exit();
169     }
170 }
Popular Tags