SAS Client demo
This is the result:
It's mainly AWT stuff, but I didn't want to remove it. Just look for
comments to see how easily you can access the SAS!
and here is the Code
main window
/***************************************************************
SAS Client API
Use it to access a SAS, either as a publisher (e.g. a sensor) or as
an subscriber.
The Sensor Alert Service dispatches predefined alerts via the XMPP
protocol. The user supscribes to a predefined alert by means of
joining a MultiUserChat (muc). This framework provides software for
sources (sensors and nodes that generate alerts) sinks (clients for
receiving alerts and subscribeing to alerts) and a web service part,
which allows the discovery of the MUCs by means of the OGC
GetCapabilities interface.
Copyright (C) 2005 by Jan Torben Heuer, University of Muenster
Contact: jan.heuer@52north.org
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (see gnu-gpl v2.txt); if not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA or visit the web page of the Free
Software Foundation, http://www.fsf.org.
Created on: ${date}
***************************************************************/
package org.n52.sasim;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import org.n52.sasclient.SASConnection;
import org.n52.sasclient.subscriber.Subscription;
public class SASIM {
// to store the formular values
TextField url;
TextField xmpphost;
TextField xmpppassword;
TextField xmppuser;
List capList;
Button xmppconnect;
SASConnection sas;
/*
* instantiate a new SASIM
*/
public static void main(String[] args) {
new SASIM().getFrame().setVisible(true);
}
/*
* get a new Frame
*
* This is mainly AWT stuff, so ONLY SAS-related parts are commented
*/
private Frame getFrame() {
final Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
f.dispose();
System.exit(0);
}
});
f.setLayout(new BorderLayout());
f.setTitle("SASIM - the SASIMULATOR");
f.add(getNorthPanel(),BorderLayout.NORTH);
capList = new List(10);
f.add(capList,BorderLayout.CENTER);
f.add(getSouthPanel(),BorderLayout.SOUTH);
f.pack();
return f;
}
Panel getSouthPanel() {
Panel j = new Panel();
Button subscribe = new Button("Subscribe");
subscribe.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
/*
* create a new Subscription Object with the PublicationID from "capList
*
* sas is a SASConnection
*
*/
Subscription s = sas.createSubscription(capList.getSelectedItem());
new SubscriberFrame(s);
}
});
j.add(subscribe);
return j;
}
Panel getNorthPanel() {
Panel northpanel = new Panel();
northpanel.setLayout(new GridLayout(2,1));
Panel upper = new Panel();
url = new TextField("http://tml.uni-muenster.de/SAS",30);
upper.add(url);
Button ok = new Button("connect SAS");
ok.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
/*
* A new connection to the given url.
* There is no validation yet
*/
sas = new SASConnection(url.getText());
xmppconnect.setEnabled(true);
capList.removeAll();
/*
* getSubscriberList does a GetCapabilities request. That means retrieving a list of available publications
*
*/
for(Subscription item : sas.getSubscriberList()) {
capList.add(item.getPublicationID());
}
}
});
upper.add(ok);
northpanel.add(upper);
Panel lower = new Panel();
xmpphost = new TextField("tml.dnsalias.org",20);
lower.add(xmpphost);
xmppuser = new TextField("u1",10);
lower.add(xmppuser);
xmpppassword = new TextField("u1",10);
lower.add(xmpppassword);
xmppconnect = new Button("connect xmpp");
xmppconnect.setEnabled(false);
xmppconnect.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
/*
* login to the given xmppserver
*/
sas.xmpplogin(xmpphost.getText(),xmppuser.getText(),xmpppassword.getText());
}
});
lower.add(xmppconnect);
northpanel.add(lower);
return northpanel;
}
}
subscription frame
/***************************************************************
SAS Client API
Use it to access a SAS, either as a publisher (e.g. a sensor) or as
an subscriber.
The Sensor Alert Service dispatches predefined alerts via the XMPP
protocol. The user supscribes to a predefined alert by means of
joining a MultiUserChat (muc). This framework provides software for
sources (sensors and nodes that generate alerts) sinks (clients for
receiving alerts and subscribeing to alerts) and a web service part,
which allows the discovery of the MUCs by means of the OGC
GetCapabilities interface.
Copyright (C) 2005 by Jan Torben Heuer, University of Muenster
Contact: jan.heuer@52north.org
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (see gnu-gpl v2.txt); if not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA or visit the web page of the Free
Software Foundation, http://www.fsf.org.
Created on: ${date}
***************************************************************/
package org.n52.sasim;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
import org.jivesoftware.smack.GroupChat;
import org.jivesoftware.smack.XMPPException;
import org.n52.sasclient.subscriber.Subscription;
public class SubscriberFrame extends Frame implements Runnable {
private GroupChat groupchat;
private TextArea textField;
public SubscriberFrame(Subscription s) {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
/*
* leave the groupchat. This does NOT remove the subscription.
* You can anytime relogin.
*/
try {
if (groupchat.isJoined()) {
groupchat.leave();
}
} catch (IllegalStateException i) {
// xmpperror - close window anyway
groupchat=null;
}
dispose();
}
});
setTitle(s.getSubscriptionEndpoint());
textField = new TextArea();
this.add(textField);
this.pack();
this.setVisible(true);
try {
/*
* perform the subscription and start a new Thread, showing alerts
* imediately
*/
this.groupchat = s.subscribe();
new Thread(this).start();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
String message = "Groupchat joined\n";
while (groupchat != null && groupchat.isJoined()) {
/*
* This is all SMACK API stuff, but to easily show the
* functionality: nextMessage() waits for a new Packet (Message).
*/
message += "["+ (new Date()).toLocaleString()+ "] "+ groupchat.nextMessage().getBody() + "\n";
textField.setText(message);
textField.setSelectionStart(message.length());
}
message += "=== connection closed ===\n";
textField.setText(message);
}
}
--
JanTorbenHeuer - 10 Apr 2006