📄 bsvideoconferencebean.java
字号:
package edu.ou.kmi.buddyspace.core;
import edu.ou.kmi.buddyspace.gui.BSMainFrame;
import org.jabber.jabberbeans.util.JID;
import org.jabber.jabberbeans.*;
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.swing.*;
import java.util.*;
public class BSVideoConferenceBean implements PacketListener
{
static private final String botClass = "edu.ou.kmi.multibot.flashmeeting.FlashMeeting";
static private final String idTagKey = "!FMBID=";
// xml parser
private DocumentBuilder db = null;
static private int index = 0;
private MessengerBean msgBean = null;
private Vector videoConferenceListeners;
private Boolean authorization = null; // null=not-authenticated; false=can-open; true=can-create
// Maps ident strings to listeners.
// Request/Response pairs are identified by a unique string, so this map
// allows us to identify the listener which made the request. i.e. so
// we can direct the response back to the requestor.
private Map listenerIdentityMap = new HashMap();
private static final String CMD_AUTH = "auth";
private static final String CMD_BOOK = "book";
private static final String CMD_LAUNCH = "launch";
private static final String CMD_AVAIL = "avail";
public BSVideoConferenceBean()
{
videoConferenceListeners = new Vector();
try {
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException e) {
System.out.println(e);
}
}
/**
* Sets existing and connected <code>ConnectionBean</code>.
* Then <code>MessengerBean</code> is created and this is registered
* as listener for message packets.
*/
protected void setConnection(ConnectionBean connection)
{
if (msgBean != null)
msgBean.delPacketListener(this);
msgBean = new MessengerBean(connection);
if (msgBean != null)
msgBean.addPacketListener(this);
}
/**
* Returns currently used <code>ConnectionBean</code>.
*/
protected ConnectionBean getConnection()
{
return (msgBean != null) ? msgBean.getConnection() : null;
}
/**
* Returns currently used <code>MessengerBean</code>.
*/
public MessengerBean getMessengerBean()
{
return msgBean;
}
/**
* Frees all object bindings to allow object destroy
*/
protected void prepareToDestroy()
{
if (msgBean != null)
msgBean.delPacketListener(this);
msgBean = null;
removeAllVideoConferenceListeners();
}
public void requestAvailability(BSVideoConferenceListener listener)
{
request(listener, CMD_AVAIL, "?action=bookingcheck&bid=" + listener.getBookingID());
}
public void requestLaunch(BSVideoConferenceListener listener)
{
request(listener, CMD_LAUNCH, "?action=bookingcheck&bid=" + listener.getBookingID());
}
public void requestBooking(BSVideoConferenceListener listener)
{
if (authorization == null)
{
javax.swing.JOptionPane.showMessageDialog(null,
"The FlashMeeting booking system may not be responding.");
// launch should never be called if there is no authorization
// but just in case, try again.
requestAuthorization(listener);
return;
}
String bid = listener.getBookingID();
String jid = listener.getBookingJID();
String [] people = listener.getBookingPeople();
int maxUsers = people.length;
if (maxUsers < 3) maxUsers = 3;
String params = "&jid=" + jid + "&people=" + maxUsers + "&bid=" + bid + "&title=" + bid;
request(listener, CMD_BOOK, "?action=book" + params);
}
public void requestAuthorization(BSVideoConferenceListener listener)
{
String command = "?action=authenticate&jid=" + listener.getBookingJID();
request(listener, CMD_AUTH, command);
}
private void request(BSVideoConferenceListener listener, String type, String command)
{
JID bot = JID.fromString(BSMainFrame.flashMeetingBotJID);
index++;
String requestID = type + "-flashmeeting-" + index;
listenerIdentityMap.put(requestID, listener);
//System.out.println(command + " id=" + requestID);
sendMessage(bot, "data", botClass, command, requestID);
}
public void open(String url)
{
edu.ou.kmi.buddyspace.utils.NativeWebBrowser.launchURL(url);
}
private boolean sendMessage(JID jid, String type, String subject, String body, String id)
{
MessageBuilder msgBuilder = new MessageBuilder();
msgBuilder.setBody(body);
msgBuilder.setToAddress(jid);
msgBuilder.setSubject(subject);
msgBuilder.setIdentifier(id);
msgBuilder.setType(type);
try {
getMessengerBean().send((Message)msgBuilder.build());
} catch (InstantiationException e) {
e.printStackTrace();
return false;
}
return true;
}
public void sendFailed(PacketEvent event) { }
public void sentPacket(PacketEvent event) { }
public void receivedPacket(PacketEvent event)
{
Packet packet = event.getPacket();
if (!(packet instanceof Message)) return;
Message msg = (Message) packet;
String id = msg.getIdentifier();
if (id == null || id.length() == 0) return;
if (id.startsWith(idTagKey))
{
// Handle notification from client that created a booking.
// This packet is not sent by the bot.
String bid = id.substring(idTagKey.length());
BSVideoConferenceListener listener = getListenerForBID(bid);
fireVideoConferenceEvent(listener, CMD_AVAIL, "N/A", "N/A");
return;
}
if (!"data".equals(msg.getType())) return;
if (!botClass.equals(msg.getSubject())) return;
if (!msg.getFromAddress().toSimpleString().toLowerCase().startsWith(BSMainFrame.flashMeetingBotJID)) return;
BSVideoConferenceListener listener = getListenerForID(id);
if (listener == null) return;
StringTokenizer st = new StringTokenizer(id, "-");
if (!st.hasMoreTokens()) return; // should never happen
String type = st.nextToken();
//System.out.println("response id=" + id + "body=" + msg.getBody());
parseResult(listener, type, msg.getBody());
// mapping for this id no is longer required, so dump it.
listenerIdentityMap.remove(id);
}
public void notifyAvailability(JID jid, String body, String bid)
{
this.sendMessage(jid, "chat", null, body, idTagKey + bid);
}
final private static String [] errors = {
"No error",
"Server IP authorization error (contact email:kmi-msg@open.ac.uk)",
"Your JID is not authorised to book a flashmeeting",
"A valid action was not specified (contact email:kmi-msg@open.ac.uk)",
"You can not attempt to start a flashmeeting for this number of users (max=50)",
"There is insufficient remaining capacity on the flashmeeting server",
"No booking id parameter was specified when checking for existing booking",
"No currently active FlashMeeting for specified booking id"
};
private void parseResult(BSVideoConferenceListener listener, String type, String xml)
{
Document doc = parse(xml.getBytes());
int error = 0;
try {
error = Integer.parseInt(getTagContent(doc, "error"));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null,
"Error parsing response from FlashMeeting booking system",
"FlashMeeting booking error",
JOptionPane.ERROR_MESSAGE);
return;
}
if ((type.equals(CMD_AVAIL) || type.equals(CMD_LAUNCH)) && error == 7)
{
// This only happens in response to failed checkbooking request.
// This is the normal behaviour for the first user to hit the
// flashmeeting button in a groupchat.
fireVideoConferenceEvent(listener, type, null, null);
return;
}
if (type.equals(CMD_AUTH))
{
authorization = new Boolean(error == 0);
fireVideoConferenceEvent(listener, CMD_AUTH, null, null);
return;
}
if (error != 0)
{
JOptionPane.showMessageDialog(null, errors[error],
"FlashMeeting startup failed", JOptionPane.ERROR_MESSAGE);
return;
}
String people = getTagContent(doc, "people");
String date = getTagContent(doc, "date");
String duration = getTagContent(doc, "duration");
String url = getTagContent(doc, "url");
String bid = getTagContent(doc, "bid");
String booker = getTagContent(doc, "booker");
String info =
"start time: " + date + "\n" +
"duration: " + duration + " minutes\n" +
"for: " + people + " users\n" +
"id: " + bid + "\n" +
"booked by: " + booker + "\n";
fireVideoConferenceEvent(listener, type, url, info);
}
private String getTagContent(Document doc, String tag)
{
NodeList nodes = doc.getElementsByTagName(tag);
if (nodes == null || nodes.getLength() == 0) return null;
Node content = nodes.item(0).getFirstChild();
if (content == null) return null;
return content.getNodeValue();
}
public Document parse(byte[] xml)
{
try {
InputStream is = new ByteArrayInputStream(xml);
return db.parse(is);
} catch (SAXException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
}
return null;
}
public void addVideoConferenceListener(BSVideoConferenceListener listener)
{
if (listener == null) return;
if (!videoConferenceListeners.contains(listener)) {
videoConferenceListeners.addElement(listener);
}
}
public void removeVideoConferenceListener(BSVideoConferenceListener listener)
{
if (listener == null) return;
videoConferenceListeners.removeElement(listener);
}
public void removeAllVideoConferenceListeners()
{
videoConferenceListeners.clear();
}
private BSVideoConferenceListener getListenerForID(String id)
{
BSVideoConferenceListener listener = (BSVideoConferenceListener)listenerIdentityMap.get(id);
// Listener may have gone away while waiting for response
// so if that's the case, just dump it.
if (!videoConferenceListeners.contains(listener))
{
listenerIdentityMap.remove(id);
listener = null;
}
return listener;
}
private BSVideoConferenceListener getListenerForBID(String bid)
{
Iterator it = videoConferenceListeners.iterator();
while (it.hasNext())
{
BSVideoConferenceListener listener;
listener = (BSVideoConferenceListener)it.next();
if (bid.equals(listener.getBookingID())) return listener;
}
return null;
}
private void fireVideoConferenceEvent(BSVideoConferenceListener listener,
String type, String url, String info)
{
if (listener == null) return;
if (type.equals(CMD_LAUNCH)) listener.launch(url, info);
else if (type.equals(CMD_AVAIL)) listener.availability(url, info);
else if (type.equals(CMD_AUTH)) listener.authorization(authorization);
else if (type.equals(CMD_BOOK)) listener.booking(url, info);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -