diff options
Diffstat (limited to 'src/YalpClients/SwtClient/Model.java')
| -rwxr-xr-x | src/YalpClients/SwtClient/Model.java | 744 |
1 files changed, 744 insertions, 0 deletions
diff --git a/src/YalpClients/SwtClient/Model.java b/src/YalpClients/SwtClient/Model.java new file mode 100755 index 0000000..3c48615 --- /dev/null +++ b/src/YalpClients/SwtClient/Model.java @@ -0,0 +1,744 @@ +/*
+ * Copyright (c) 2006 Manuel Traut and Volker Dahnke
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors: Manuel Traut and Volker Dahnke
+ */
+
+package YalpClients.SwtClient;
+
+import YalpInterfaces.*;
+import YalpClients.*;
+
+import java.beans.XMLDecoder;
+import java.beans.XMLEncoder;
+import java.io.*;
+import java.net.Inet4Address;
+import java.net.MalformedURLException;
+import java.net.UnknownHostException;
+import java.sql.SQLException;
+import java.util.ArrayList;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.MessageBox;
+import org.eclipse.swt.widgets.Shell;
+
+import org.omg.CosNaming.*;
+import org.omg.CosNaming.NamingContextPackage.*;
+import org.omg.CORBA.*;
+
+/*
+ * Class Model
+ *
+ * <em>Implementiert die Schnittstellenfunktionen zur Steuerung des Servers
+ * (Datenbankinhalte schreiben, lesen; Streaming steuern</em>
+ *
+ * @author Volker Dahnke / Manuel Traut
+ *
+ * @version 0.6 14-12-2005<br>
+ *
+ * @see client.GUI
+ */
+
+public class Model {
+
+ public ServerControlInterface srvCon;
+ private Process vlcPlayer;
+ private Output actualStream;
+ private Output streamInfo;
+ private ArrayList<Media> findMedia;
+ private ArrayList<Media> editMedia;
+ private ArrayList<Media> toPlaylist;
+ private ArrayList<Media> playList;
+ private ArrayList<YalpUser> userList;
+ private ClientConfiguration settings;
+ private String clientIP;
+ private String curPasswd;
+ private AccessRights userGroup=AccessRights.DENY;
+ private String userName;
+ private Boolean playback = false;
+ private Boolean logoff= false;
+ private Session session;
+/*
+ * Instances srvConnection to Registry, DbConnection, ServerControl
+ * and VlcStreamer. Also instances Class variables
+ */
+ public Model(String[] argv) {
+
+ this.settings=new ClientConfiguration();
+ loadConfig("ClientConfiguration.xml");
+ writeConfig("ClientConfiguration.xml");
+
+ try {
+ ORB orb = ORB.init(argv, null);
+
+ org.omg.CORBA.Object objRef =
+ orb.resolve_initial_references("NameService");
+
+ NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
+
+ String name = "YALP_Server";
+ srvCon = ServerControlInterfaceHelper.narrow(ncRef.resolve_str(name));
+ } catch (Exception e) {// DEBUG
+ String errorStr="couldn't connect to YALP Server";
+
+ MessageBox messageBox = new MessageBox(
+ new Shell(Display.getDefault(),SWT.MIN), SWT.OK );
+
+ System.out.println(e);
+ messageBox.setMessage(errorStr);
+ messageBox.open();
+ System.exit(0);
+ }
+
+ // determine client IP Adress
+ try {
+ this.clientIP = Inet4Address.getLocalHost().getHostAddress();
+ System.out.println(this.clientIP);
+ } catch (UnknownHostException e) {
+ String errorStr = "Model.model: determine IP failed";
+
+ MessageBox messageBox = new MessageBox(
+ new Shell(Display.getDefault(),SWT.MIN), SWT.OK );
+
+ System.out.println(errorStr);
+ messageBox.setMessage(errorStr);
+ messageBox.open();
+ System.exit(0);
+ }
+ this.userList = new ArrayList<YalpUser>();
+ this.playList = new ArrayList<Media>();
+ this.toPlaylist = new ArrayList<Media>();
+ }
+
+/*
+ * write Configuration to XML File
+ *
+ * @param fileName
+ * @return boolean: true if succesful
+ */
+ public boolean writeConfig(String fileName) {
+ try {
+ FileOutputStream srvConfigFile = new FileOutputStream(fileName);
+ XMLEncoder srvConfigWriter = new XMLEncoder(srvConfigFile);
+ srvConfigWriter.writeObject(settings.serverIP);
+ srvConfigWriter.writeObject(settings.classServerPort);
+ srvConfigWriter.writeObject(settings.registryPort);
+ srvConfigWriter.writeObject(settings.vlcCommand);
+ srvConfigWriter.close();
+ } catch (FileNotFoundException fnfe) {
+ return false;
+ }
+ return true;
+ }
+
+/*
+ * load Configuration from XML File if possible
+ *
+ * @param fileName
+ * @return boolean: true if succesful
+ */
+ public boolean loadConfig(String fileName){
+ try{
+ FileInputStream srvConfigFile = new FileInputStream(fileName);
+ XMLDecoder srvConfigLoader = new XMLDecoder(srvConfigFile);
+ System.out.println(srvConfigFile);
+ settings.serverIP = (String)srvConfigLoader.readObject();
+ settings.classServerPort= (Integer)srvConfigLoader.readObject();
+ settings.registryPort = (Integer)srvConfigLoader.readObject();
+ settings.vlcCommand = (String)srvConfigLoader.readObject();
+ srvConfigLoader.close();
+ } catch(FileNotFoundException fnfe) {
+ System.out.println("Configuration not found, loading defaults...");
+ return false;
+ } catch(ClassCastException cce) {
+ System.out.println("Errors in Configuration, loading defaults...");
+ return false;
+ }
+ return true;
+ }
+
+/*
+ * get acutal srvConfig object
+ *
+ * @return ClientConfiguration: aktuelle Konfiguration
+ */
+ public ClientConfiguration getConfig(){
+ return this.settings;
+ }
+
+/*
+ * setzen und speichern eines ClientConfiguration Files
+ *
+ * @param set
+ * ClientConfiguration.xml
+ * @return boolean: true if succesful written Configuration
+ */
+ public boolean setConfig(ClientConfiguration set){
+ this.settings = set;
+ return this.writeConfig("ClientConfiguration.xml");
+ }
+
+/*
+ * Sends YalpUsername and passwd to Server and returns YalpUsergroup
+ *
+ * @param username
+ * @param passwd
+ * @return AccessRights
+ */
+ public AccessRights userVerify(String username,String passwd){
+ this.userName=username;
+ this.curPasswd = passwd;
+ YalpErrorHolder err = new YalpErrorHolder();
+ SessionHolder sess = new SessionHolder();
+ try{
+ // log on server
+ this.srvCon.clientLogon(this.userName,passwd,this.clientIP,sess,err);
+ this.session = sess.value;
+ this.userGroup = sess.value.me.level;
+ /*
+ } catch (RemoteException e) {
+ System.out.println("client.Model: Logon on Server failed 1"+ e);
+ e.printStackTrace();
+ System.out.println("client.Model: Logon on Server failed 1");
+ } catch (MalformedURLException e) {
+ System.out.println("client.Model: Logon on Server failed 2");
+ } catch (SQLException e) {
+ System.out.println("client.Model: Logon on Server failed 3");
+ } catch (NotBoundException e) {
+ System.out.println("client.Model: Logon on Server failed 4");
+ */
+ }catch(NullPointerException e){
+ System.out.println("YALP Server couldn't be reached");
+ this.userGroup = AccessRights.NO_YALP_SERVER;
+ }
+ return this.userGroup;
+ }
+
+/*
+ * Gets a list with all YalpUsers from the server
+ *
+ * @return ArrayList<YalpUser> all registered YalpUsers
+ */
+ public ArrayList<YalpUser> getUser(){
+ YalpErrorHolder err = new YalpErrorHolder();
+ try{
+ UsersHolder usr = new UsersHolder();
+ this.srvCon.getUser(usr, err);
+ this.userList = new ArrayList<YalpUser>(usr.value.length);
+ for(int i = 0; i < usr.value.length; i++)
+ this.userList.add(usr.value[i]);
+ }catch(Exception e){
+ System.out.println("Exceoption in Model userList "+ e);
+ }
+ return userList;
+ }
+
+/*
+ * Sends User change request to server
+ *
+ * @param user
+ * Configuration with changes on specific YalpUser
+ */
+ public void changeUser(YalpUser user, String passwd, Action action) {
+ YalpErrorHolder err = new YalpErrorHolder();
+ try{
+ this.srvCon.changeUser( user,
+ passwd,
+ action,
+ err );
+ System.out.println(err.value.descr);
+ }catch(Exception e){
+ System.out.println("Exceoption in Model changeUser "+ e);
+ }
+ }
+
+/*
+ * Sends Media change request to server and refreshes Model data
+ *
+ * @param media
+ * to change
+ * @return boolean: succesful changed media
+ */
+ public Boolean changeMedia(Media media, Action action){
+ YalpErrorHolder err = new YalpErrorHolder();
+ try{
+ this.srvCon.changeMedia(media, action, err);
+ if(err.value.code == YalpErrorCode.OK){
+ Media result;
+ for (int i=0;i<this.editMedia.size();i++){
+ result=this.editMedia.get(i);
+ /* t.b.d. alter to new db desgin
+ if (result.id==media.id){
+ result.type=media.type;
+ result.title=media.title;
+ result.author=media.author;
+ result.album=media.album;
+ result.category=media.category;
+ result.year=media.year;
+ result.duration=media.duration;
+ result.aBitrate=media.aBitrate;
+ result.vBitrate=media.vBitrate;
+ result.resolution=media.resolution;
+ result.ownerId=media.ownerId;
+ result.lastEdit=media.lastEdit;
+ result.path=media.path;
+ result.name=media.name;
+ return true;
+ }
+ */
+ }
+ }
+ } catch(Exception e) {
+ System.out.println("Exceoption in Model changeUser "+ e);
+ }
+ return false;
+ }
+
+/*
+ * adds Media with given id to Playlist
+ *
+ * @param id
+ */
+ public void add2PlayList(int id){
+ Boolean notfound=true;
+ for (int i=0;(i<this.findMedia.size())&& (notfound);i++){
+ Media result=this.findMedia.get(i);
+ if (result.id==id){
+ String name = result.name;
+ String path = result.path;
+ if(playback){
+ this.add2Stream(name, path);
+ }
+ this.playList.add(result);
+ this.toPlaylist.add(result);
+ notfound=false;
+ }
+ }
+ }
+
+/*
+ * Removes Media with given id from Playlist
+ *
+ * @param id
+ */
+ public void removeFromPlayList(int id){
+ /* t.b.d.
+ this.playList.remItem(id);
+ this.toPlaylist.remove(id);
+ */
+ }
+
+/*
+ * Returns List with items to play
+ *
+ * @return ArrayList<Media>: ArrayList to create Playlist from
+ */
+ public ArrayList<Media> getToPlaylist(){
+ return this.toPlaylist;
+ }
+
+/*
+ * Creates new Stream with actual playlist
+ *
+ * @return Streaminfo: about new created Stream, Client should use this,
+ * information to get the Information howto receive the stream
+ */
+ public Output createOutput(){
+ /* t.b.d. former createStream */
+ this.streamInfo.outputAction = Action.CREATE;
+ Media[] a = {};
+ this.streamInfo.playlist = this.playList.toArray(a);
+ return this.streamInfo;
+ }
+
+/*
+ * Sends search request to server and refreshes Model data
+ *
+ * @param request
+ * Search Request
+ * @param kind
+ * 1 - search in first Window; other ints: search in other window
+ * (admins edit view)
+ * @return int: number of results
+ */
+ public int search(String request, ArrayList<MediaType> types, int kind){
+ YalpErrorHolder err = new YalpErrorHolder();
+ MediasHolder result = new MediasHolder();
+ try{
+ MediaType[] a = {};
+ if (kind == 1){
+ srvCon.search(request, types.toArray(a), result, err);
+ for(int i = 0; i < result.value.length; i++)
+ this.findMedia.add(result.value[i]);
+ return this.findMedia.size();
+ } else {
+ srvCon.search(request, types.toArray(a), result, err);
+ for(int i = 0; i < result.value.length; i++)
+ this.editMedia.add(result.value[i]);
+ return this.editMedia.size();
+ }
+ } catch(Exception e){
+ System.out.println("Exceoption in Model search "+ e);
+ return 0;
+ }
+ }
+
+/*
+ * Returns the search result
+ *
+ * @param kind
+ * 1 - for searchMedia of mainwindow; 2 - for searchMedia of editView
+ * @return ArrayList<Media>: last found medias
+ */
+
+ public ArrayList<Media> getMedia(int kind){
+ if (kind==1) return this.findMedia;
+ else return this.editMedia;
+ }
+
+/*
+ * Retuns search result with given id
+ *
+ * @param id
+ * of the result
+ * @param kind
+ * 1 - for mainWindow, 2 for admins editView
+ * @return Media: searchMedia, with specified ID
+ */
+ public Media getMediaWithId(int id,int kind){
+ ArrayList<Media> resultList;
+ if (kind==1)resultList= this.findMedia;
+ else resultList=this.editMedia;
+ for (int i=0;(i<resultList.size());i++){
+ Media result=resultList.get(i);
+ if (result.id==id){
+ return result;
+ }
+ }
+ return null;
+ }
+
+/*
+ * Retuns id of YalpUser with given username
+ *
+ * @param username
+ * @return int: useriD
+ */
+ public int getOwnerid(String username){
+ ArrayList<YalpUser> ownerList =this.userList;
+ for (YalpUser owner:ownerList){
+ if (owner.name.equals(username)){
+ return owner.id;
+ }
+ }
+ return 0;
+ }
+
+/*
+ * Returns MediaType as string
+ *
+ * @param mediatype
+ * @return string
+ */
+ public String getTypeName(MediaType type)
+ {
+ switch(type.value())
+ {
+ case MediaType._IMAGE:
+ return "image";
+ case MediaType._SOUND:
+ return "sound";
+ case MediaType._VIDEO:
+ return "video";
+ default:
+ return "unknown";
+ }
+ }
+
+/*
+ * Returns AccessRights as string
+ *
+ * @param mediatype
+ * @return string
+ */
+ public String accessRightsName(AccessRights type)
+ {
+ switch(type.value())
+ {
+ case AccessRights._USER:
+ return "user";
+ case AccessRights._ADMIN:
+ return "admin";
+ case AccessRights._DENY:
+ return "no login";
+ default:
+ return "unknown";
+ }
+ }
+
+
+/*
+ * Sets class variable actualStream to given value
+ *
+ * @param aStream
+ * stream to srvControl via play, pause, etc
+ */
+ public void setActualStream(Output aStream){
+ this.actualStream = aStream;
+ }
+
+/*
+ * Starts Video Lan Player
+ *
+ * @return boolean: true if vlc can be started and a stream is actualStream
+ */
+ private boolean startVLC(){
+
+ String vlcCmd = settings.vlcCommand +
+ " --reset-srvConfig --video-on-top -I telnet --telnet-port 12345 ";
+/*
+ switch (this.actualStream.getAccess()){
+ case UDP:
+ vlcCmd += "udp:@";
+ break;
+ case HTTP:
+ vlcCmd += "http://";
+ break;
+ default:
+ System.out.println(
+ "client.Model.startVLC: playback this type is not supported" );
+ }
+*/
+ vlcCmd += this.actualStream.info.params;
+ System.out.println(vlcCmd);
+
+ try {
+ this.vlcPlayer = Runtime.getRuntime().exec(vlcCmd);
+ } catch (IOException e) {
+ System.out.println("client.Model.startVLC: starting vlcPlayer failed");
+ return false;
+ }
+ return true;
+ }
+
+/*
+ * Stops Video Lan Player
+ *
+ */
+ private void stopVLC(){
+ this.playback = false;
+ this.vlcPlayer.destroy();
+ }
+
+/*
+ * Starts playback of actual playlist
+ *
+ * @return void
+ */
+ public boolean play(){
+
+ this.playback = true;
+
+ this.actualStream = this.createOutput();
+ this.actualStream.destIp = this.clientIP;
+
+ YalpErrorHolder err = new YalpErrorHolder();
+
+ OutputHolder tmp = new OutputHolder(this.actualStream);
+ this.srvCon.control(tmp, err);
+ this.actualStream = tmp.value;
+
+ this.actualStream.outputAction = Action.START;
+ OutputHolder tmp2 = new OutputHolder(this.actualStream);
+ this.srvCon.control(tmp2, err);
+ this.actualStream = tmp2.value;
+ this.startVLC();
+ return true;
+ }
+
+/*
+ * Adds a new Media to Playlist. Only during playback
+ *
+ * @param name
+ * @param path
+ * @return boolean: true if succesful added
+ */
+ public boolean add2Stream(String name, String path){
+
+ YalpErrorHolder err = new YalpErrorHolder();
+ Media[] empty = {};
+ this.actualStream.playlist = empty;
+
+ Media media = new Media();
+ media.path = path;
+ media.fileName = name;
+
+ this.actualStream.playlist[this.actualStream.playlist.length] = media;
+ this.actualStream.outputAction = Action.CREATE;
+
+ OutputHolder tmp = new OutputHolder(this.actualStream);
+ this.srvCon.control(tmp, err);
+ this.actualStream = tmp.value;
+ return true;
+ }
+
+/*
+ * Stops playback
+ *
+ * @return boolean: false - if srvConnection to streamer failed
+ */
+ public boolean stop(){
+ YalpErrorHolder err = new YalpErrorHolder();
+ this.actualStream.outputAction = Action.STOP;
+ OutputHolder tmp = new OutputHolder(this.actualStream);
+ this.srvCon.control(tmp,err);
+ this.actualStream = tmp.value;
+ this.stopVLC();
+ return true;
+ }
+
+/*
+ * jumps to next media in playlist
+ *
+ * @return boolean: false - if srvConnection to streamer failed
+ */
+ public boolean next(){
+ YalpErrorHolder err = new YalpErrorHolder();
+ this.actualStream.outputAction = Action.FORWARD;
+ OutputHolder tmp = new OutputHolder(this.actualStream);
+ this.srvCon.control(tmp,err);
+ this.actualStream = tmp.value;
+ return true;
+ }
+
+/*
+ * Pauses playback
+ *
+ * @return boolean: false - if srvConnection to streamer failed
+ */
+ public boolean pause(){
+ YalpErrorHolder err = new YalpErrorHolder();
+ this.actualStream.outputAction = Action.PAUSE;
+ OutputHolder tmp = new OutputHolder(this.actualStream);
+ this.srvCon.control(tmp,err);
+ this.actualStream = tmp.value;
+ return true;
+ }
+
+/*
+ * Causes the server to clean up and shutdown
+ *
+ */
+ public void serverShutdown(){
+ if (this.playback)
+ this.stop();
+ this.srvCon.serverShutdown();
+ }
+
+/*
+ * Checks if the server is still alive if not Client shutdown
+ *
+ */
+ public void serverStillAlive(){
+ // try{
+ // srvCon.ping();
+ /* }catch (RemoteException e){
+ if (this.playback)
+ this.stopVLC();
+ String errorStr="Server shutdown - Client is going down too";
+
+ MessageBox messageBox = new MessageBox(
+ new Shell(Display.getDefault(),SWT.MIN), SWT.OK);
+
+ System.out.println(errorStr);
+ messageBox.setMessage(errorStr);
+ messageBox.open();
+ System.exit(0);
+ } */
+ }
+
+/*
+ * Returns client IP address
+ *
+ * @return String: ipAddress
+ */
+ public String getClientIp(){
+ return this.clientIP;
+ }
+
+/*
+ * Returns username of logged in user
+ *
+ * @return String: username
+ */
+ public String getYalpUserName(){
+ return this.userName;
+ }
+
+/*
+ * Returns indication if playback is runing
+ *
+ * @return boolean: true - if is play'in
+ */
+ public Boolean getPlayback(){
+ return this.playback;
+ }
+
+/*
+ * Get Total amount of streams played
+ *
+ * @return int: number of Streams totally played
+ *
+ public int getAllStreamNum(){
+ try{
+ return this.srvCon.getStreamCounter().getAllStreams();
+ }catch (Exception e){
+ System.out.println("Exception in Model.getAllStreamNum"+e);
+ }
+ return -1;
+ }
+ */
+
+/*
+ * Gets amount of Streams actual playing
+ *
+ * @return int: number of Streams play'in at the moment
+ *
+ public int getActualStreamNum(){
+ try{
+ return this.srvCon.getStreamCounter().getActualStreams();
+ }catch (Exception e){
+ System.out.println("Exception in Model.getActualStreamNum"+e);
+ }
+ return -1;
+ }
+ */
+
+/*
+ * Sets class variable logoff to true and logs off from server
+ */
+ public void logoff(){
+ YalpErrorHolder err = new YalpErrorHolder();
+ try{
+ this.logoff=true;
+ this.srvCon.clientLogoff(this.session, err);
+ }catch(Exception e) {// DEBUG
+ System.out.println("Model Exeptions in logoff");
+ e.printStackTrace();
+ }
+ }
+
+/*
+ * Returns indication if client is logging of
+ *
+ * @return Boolean: true - if is logging of
+ */
+ public Boolean getLogoff(){
+ return this.logoff;
+ }
+}
|
