From 518243041436707d1b1ec9c22295b7d1a0581a4a Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1s=C3=A1ry=20D=C3=A1niel?= Date: Tue, 17 Apr 2018 14:44:15 +0000 Subject: [PATCH] git-tfs-id: [http://tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube;C31038 --- .../META-INF/MANIFEST.MF | 1 + .../user/commons/nexio/NexioDispatcher.java | 125 ++-- .../commons/nexio/api/ClipEventListener.java | 2 + .../user/commons/nexio/api/Controller.java | 32 +- .../user/commons/nexio/api/MediaListener.java | 208 +++--- .../src/user/commons/nexio/api/Mediabase.java | 14 +- .../user/commons/nexio/api/MediabaseImpl.java | 19 +- .../api/samples/ListMetabaseContents.java | 607 +++++++++--------- .../pages/joblist.zul | 1 + .../pages/searchitems.zul | 4 +- .../resources/i3-label_hu.properties | 2 +- .../user/jobengine/zk/model/JobListModel.java | 32 +- .../user/jobengine/zk/model/SearchModel.java | 25 + .../META-INF/MANIFEST.MF | 1 + .../osgi/ws/nexio/NexioWSServlet.java | 18 +- 15 files changed, 545 insertions(+), 546 deletions(-) diff --git a/server/user.jobengine.osgi.commons/META-INF/MANIFEST.MF b/server/user.jobengine.osgi.commons/META-INF/MANIFEST.MF index 51f1af6f..cd541195 100644 --- a/server/user.jobengine.osgi.commons/META-INF/MANIFEST.MF +++ b/server/user.jobengine.osgi.commons/META-INF/MANIFEST.MF @@ -23,6 +23,7 @@ Export-Package: user.commons, user.commons.logging, user.commons.morpheus, user.commons.nexio, + user.commons.nexio.api, user.commons.nosql, user.commons.octopus, user.commons.rcc, diff --git a/server/user.jobengine.osgi.commons/src/user/commons/nexio/NexioDispatcher.java b/server/user.jobengine.osgi.commons/src/user/commons/nexio/NexioDispatcher.java index b17a4b69..4fffa70e 100644 --- a/server/user.jobengine.osgi.commons/src/user/commons/nexio/NexioDispatcher.java +++ b/server/user.jobengine.osgi.commons/src/user/commons/nexio/NexioDispatcher.java @@ -1,33 +1,26 @@ package user.commons.nexio; -import java.io.UnsupportedEncodingException; -import java.util.HashMap; import java.util.Iterator; -import java.util.List; -import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import javax.swing.event.EventListenerList; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.ibm.nosql.json.api.BasicDBObject; import com.ibm.nosql.json.api.DB; import com.ibm.nosql.json.api.DBCollection; -import com.ibm.nosql.json.api.DBCursor; -import user.commons.ListUtils; import user.commons.nexio.api.Clip; import user.commons.nexio.api.ClipEvent; import user.commons.nexio.api.ClipEventListener; import user.commons.nexio.api.ClipEventType; import user.commons.nexio.api.Controller; +import user.commons.nexio.api.MediaListener; +import user.commons.nexio.api.Mediabase; import user.commons.nexio.server.protocol.Connection; import user.commons.nexio.server.protocol.GetExtendedFieldCommand; import user.commons.nosql.NoSQLUtils; -import user.commons.remotestore.IProgressEventListener; /** * NexioWSServlet hozza letre. Indulaskor betolti a teljes nexio clip listat mongodb-be. Beregisztralja magat ClipEventListener-nek. Az erkezo esemenyekkel @@ -91,52 +84,50 @@ public class NexioDispatcher implements ClipEventListener { return str; } - private EventListenerList progressListenerList; private INexioChangeListener nexioChangeListener; private DB db; private String NEXIO_HOST = System.getProperty("nexio.host"); private Controller controller = null; + private boolean disabled; + + private Thread worker; + + private MediaListener mediaListener; + public NexioDispatcher() { this.db = NoSQLUtils.getNoSQLDB(); - } - - public void addProgressListener(IProgressEventListener listener) { - progressListenerList.add(IProgressEventListener.class, listener); + disabled = Boolean.parseBoolean(System.getProperty(NEXIO_DISABLE, "false")); } @Override public void clipEventPerformed(ClipEvent evt) { + //logger.info("ClipEvent {} {}", evt.getEventType(), evt.getClip()); + if (disabled) + return; try { - //DELETE-nel csak az id van kitoltve a CLIP-en, ezert exception jon!!! if (evt.getEventType() == ClipEventType.CLIP_ADDED) { - // if (isValidClip(evt.getClip())) { BasicDBObject jsClip = clipToJSON(evt.getClip()); String fileName = jsClip.getString(LONGNAMEID); if (fileName != null && !"".equals(fileName)) { - logger.debug("clipEventPerformed clip: {} {}", evt.getClip().getId(), fileName); - //cache??? - - // HashMap clipsForId = new HashMap(); - // Map clipsForXID = loadClipsFromMongo(clipsForId); saveClipIntoMongo(jsClip); } else { - logger.debug("clipEventPerformed clip: {}", evt.getClip() == null ? "null" : evt.getClip().getId()); + logger.warn("LONGNAMEID is empty! ", evt.getClip().getId()); } - // } } else if (evt.getEventType() == ClipEventType.CLIP_DELETED) { BasicDBObject jsClip = new BasicDBObject(); - jsClip.put(ID, evt.getClip().getId().get()); //Unique internal ID + jsClip.put(ID, evt.getClip().getId().get()); removeDeleted(jsClip); } else if (evt.getEventType() == ClipEventType.LISTENER_RESTART_NEEDED) { shutdown(); startup(); + logger.info("Nexio dispatcher restarted"); } - } catch (Exception exc) { - logger.error("", exc); + } catch (Exception e) { + logger.catching(e); } } @@ -288,36 +279,19 @@ public class NexioDispatcher implements ClipEventListener { } /* - + int c = connection.read(buffer, 0, 2); //2 bytes hosszan az idokozben hozzaadott id-k szamossaga if (c < 2) { throw new ProtocolException("c, 2, 2"); } //MSB, LSB - + return nofIds; */ return ret; } - private Map loadClipsFromMongo(Map clipsForId) { - Map result = new HashMap<>(); - DBCollection collection = db.getCollection(NexioDispatcher.CLIP_COLLECTION_NAME); - DBCursor find = collection.find(); - if (find.hasNext()) { - List clips = ListUtils.cast(find.toArray()); - result = ListUtils.map(clips, item -> item.getString(LONGNAMEID)); - for (BasicDBObject clip : clips) { - result.put(clip.getString(LONGNAMEID), clip); - if (clipsForId != null) { - clipsForId.put(clip.getString(ID), clip); - } - } - } - return result; - } - //dispatch events to NexioWSServlet public void onCreate(BasicDBObject data) { logger.debug("onCreate clip: " + data); @@ -343,8 +317,10 @@ public class NexioDispatcher implements ClipEventListener { onDelete(clipToDelete); } - public void removeProgressListener(IProgressEventListener listener) { - progressListenerList.remove(IProgressEventListener.class, listener); + @Override + public void restart() { + shutdown(); + startup(); } private void saveClipIntoMongo(BasicDBObject clip) { @@ -382,7 +358,19 @@ public class NexioDispatcher implements ClipEventListener { } public void shutdown() { - executor.shutdownNow(); + if (mediaListener != null) { + mediaListener.disconnect(); + mediaListener = null; + } + if (controller != null) { + controller.disconnect(); + controller = null; + } + try { + worker.join(); + } catch (InterruptedException e) { + logger.catching(e); + } } public void startup() { @@ -393,37 +381,34 @@ public class NexioDispatcher implements ClipEventListener { logger.catching(e); return; } - logger.info("NEXIO server connected"); - - this.progressListenerList = new EventListenerList(); - boolean disableNexio = Boolean.parseBoolean(System.getProperty(NEXIO_DISABLE)); - if (disableNexio) - return; - - this.dropAllClipsFromMongo(); - this.ensureIndexes(); + if (!disabled) { + this.dropAllClipsFromMongo(); + this.ensureIndexes(); + } boolean[] useMOSGateway = { Boolean.parseBoolean(System.getProperty(NEXIO_USE_MOS_GATEWAY)) }; - executor.submit(() -> { + + worker = new Thread(() -> { try { - if (useMOSGateway[0]) { - NexioDataMiner dm = new NexioDataMiner(); - dm.transferClips(); - } else { - copyClipsIntoMongo(); + logger.info("Initial clip import started"); + if (!disabled) { + if (useMOSGateway[0]) { + NexioDataMiner dm = new NexioDataMiner(); + dm.transferClips(); + } else { + copyClipsIntoMongo(); + } } - NexioDispatcher.this.controller.getMediabase().startMediaListener(); - NexioDispatcher.this.controller.getMediabase().getMediaListener().addClipEventListener(NexioDispatcher.this); + logger.info("Initial clip import completed"); + Mediabase mediabase = NexioDispatcher.this.controller.getMediabase(); + mediaListener = mediabase.createMediaListener(disabled, NexioDispatcher.this); + mediaListener.startListener(); } catch (Exception e) { logger.catching(e); } }); - } - - private String toUTF8(String iso88592) throws UnsupportedEncodingException { - byte[] bytes = iso88592.getBytes("ISO-8859-2"); - return new String(bytes, "UTF-8"); + worker.start(); } } diff --git a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/ClipEventListener.java b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/ClipEventListener.java index d7e45126..a3002c3d 100644 --- a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/ClipEventListener.java +++ b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/ClipEventListener.java @@ -2,4 +2,6 @@ package user.commons.nexio.api; public interface ClipEventListener { public void clipEventPerformed(ClipEvent evt); + + void restart(); } diff --git a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/Controller.java b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/Controller.java index c8ae94bf..739c99fb 100644 --- a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/Controller.java +++ b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/Controller.java @@ -1,9 +1,12 @@ package user.commons.nexio.api; -import user.commons.nexio.server.protocol.*; import java.io.IOException; import java.net.UnknownHostException; +import user.commons.nexio.server.protocol.Connection; +import user.commons.nexio.server.protocol.NexioServerProtocolImpl; +import user.commons.nexio.server.protocol.TCPConnection; + public class Controller { private Connection connection = null; @@ -25,24 +28,27 @@ public class Controller { mediabase = new MediabaseImpl(new NexioServerProtocolImpl(connection)); } - public void disconnect() throws IOException { - connection.disconnect(); + public void disconnect() { + try { + connection.disconnect(); + } catch (Exception e) { + } } - public Mediabase getMediabase() { - return mediabase; + public Connection getConnection() { + return this.connection; } - public String getHost(){ + public String getHost() { return this.host; } - - public int getPort(){ - return this.port; + + public Mediabase getMediabase() { + return mediabase; } - - public Connection getConnection(){ - return this.connection; + + public int getPort() { + return this.port; } - + } diff --git a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/MediaListener.java b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/MediaListener.java index e9c321d7..34326026 100644 --- a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/MediaListener.java +++ b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/MediaListener.java @@ -19,7 +19,7 @@ import user.commons.nexio.server.protocol.TCPConnection; * @author robi */ -public class MediaListener implements Runnable { +public class MediaListener { static private final char[] hexArray = "0123456789ABCDEF".toCharArray(); static private final Logger logger = LogManager.getLogger(); @@ -59,22 +59,16 @@ public class MediaListener implements Runnable { } private ArrayList clipEventListeners = new ArrayList(); - private boolean isRunning = false; - private Mediabase mediaBase = null; private int nexioPort = 557; private String nexioHost = null; - private Connection connection = null; - private ArrayList notificationList = new ArrayList(); - //KONSTRUKTOR public MediaListener(Mediabase _mediaBase, String _nexioHost, int _nexioPort) { this.mediaBase = _mediaBase; this.nexioHost = _nexioHost; this.nexioPort = _nexioPort; - startListener(); } public void addClipEventListener(ClipEventListener listener) { @@ -107,25 +101,48 @@ public class MediaListener implements Runnable { private void createNexioConnection() throws Exception { this.connection = TCPConnection.getInstance(this.nexioHost, this.nexioPort); - logger.info("Nexio MediaListener is connected to NEXIO " + this.nexioHost + " : " + this.nexioPort); + logger.info("NEXIO MediaListener is connected to " + this.nexioHost + ":" + this.nexioPort); + } + + public void disconnect() { + try { + if (connection != null) + connection.disconnect(); + } catch (Exception e) { + logger.catching(e); + } } private void fireEvent(String idString, int notificationNumber) throws Exception { - Id id = new Id(idString); ClipEvent evt = null; - if (notificationNumber == NN_ID_ADDED) { - evt = new ClipEvent(mediaBase.getClip(id), ClipEventType.CLIP_ADDED); - } else if (notificationNumber == NN_ID_DELETED) { - Clip deletedClip = new ClipImpl(mediaBase.getProtocol(), id); - evt = new ClipEvent(deletedClip, ClipEventType.CLIP_DELETED); - } else if (notificationNumber == NN_LISTENER_RESTART) { + if (notificationNumber == NN_LISTENER_RESTART) { evt = new ClipEvent(null, ClipEventType.LISTENER_RESTART_NEEDED); + } else { + Id id = new Id(idString); + if (notificationNumber == NN_ID_ADDED) { + evt = new ClipEvent(mediaBase.getClip(id), ClipEventType.CLIP_ADDED); + } else if (notificationNumber == NN_ID_DELETED) { + Clip deletedClip = new ClipImpl(mediaBase.getProtocol(), id); + evt = new ClipEvent(deletedClip, ClipEventType.CLIP_DELETED); + } } + for (ClipEventListener cel : this.clipEventListeners) { cel.clipEventPerformed(evt); } } + private void forceRestart() { + logger.info("MediaListener stopping..."); + try { + fireEvent(null, NN_LISTENER_RESTART); + // if (this.connection != null) + // this.connection.disconnect(); + } catch (Exception e) { + logger.catching(e); + } + } + private void handleNotification(int notificationNumber) throws Exception { //LIST_FIRST_ID_LIST /* @@ -149,138 +166,83 @@ public class MediaListener implements Runnable { } - private byte[] readBytes(int numberOfBytes) throws Exception { - byte[] ret = new byte[numberOfBytes]; - try { - for (int c = 0; c < numberOfBytes; c++) { - ret[c] = (byte) this.connection.read(); - } - } catch (Exception e) { - restartListener(); - throw e; - } - return ret; - } - - private byte[] readBytesOLD(int numberOfBytes) throws Exception { - try { - byte[] ret = new byte[numberOfBytes]; - this.connection.read(ret, 0, numberOfBytes); - return ret; - } catch (Exception e) { - restartListener(); - throw e; - } - } - - public void removeClipEventListener(ClipEventListener listener) { - this.clipEventListeners.remove(listener); - } - - private void restartListener() throws Exception { - this.isRunning = false; - Thread.sleep(3000); - try { - Thread.currentThread().interrupt(); - } catch (Exception e) { - } - try { - if (this.connection != null) { - this.connection.disconnect(); - } - } catch (Exception e) { - } - //new connection / new thread - //szóljunk a dispatcher-nek, hogy inicializálja újra magát - //fireEvent(null, NN_LISTENER_RESTART); - startListener(); - } - - @Override - public void run() { + private void listen() { try { //SEND CHANGE NOTIFICATION REQ writeBytes(CHANGE_NOTIFICATION_REQ); //ACK byte[] buffer = readBytes(2); - logger.info("Nexio MediaListener is waiting for events.."); - - while (isRunning) { - try { - - //BLOKKOLVA addig olvassa fel es gyujti a notification-oket amig mas adat nem jon. - //az elso alkalommal amikor nem notification-t kap, visszater. - buffer = collectNotifications(false); - - //LIST_FIRST_ID_LIST response - if (LIST_FIRST_ID_LIST_FOUND.equals(bytesToHex(buffer))) { - int notificationNumber = notificationList.get(0); - notificationList.remove(0); - - //read first id - buffer = readBytes(8); - String stringId = new String(buffer); - logger.debug("Nexio MediaListener first id: " + stringId); + logger.info("NEXIO MediaListener listeninig"); + + while (true) { + //BLOKKOLVA addig olvassa fel es gyujti a notification-oket amig mas adat nem jon. + //az elso alkalommal amikor nem notification-t kap, visszater. + buffer = collectNotifications(false); + + //LIST_FIRST_ID_LIST response + if (LIST_FIRST_ID_LIST_FOUND.equals(bytesToHex(buffer))) { + int notificationNumber = notificationList.get(0); + notificationList.remove(0); + + //read first id + buffer = readBytes(8); + String stringId = new String(buffer); + logger.debug("Nexio MediaListener first id: " + stringId); + fireEvent(stringId, notificationNumber); + + //read next ids + writeBytes(LIST_NEXT_ID_LIST); + //for (buffer = readBytes(2); LIST_NEXT_ID_LIST_FOUND.equals(bytesToHex(buffer)) && isRunning; buffer = readBytes(2)) { + for (buffer = collectNotifications(true); LIST_NEXT_ID_LIST_FOUND.equals(bytesToHex(buffer)); buffer = collectNotifications(true)) { + + byte[] idBuffer = readBytes(8); + stringId = new String(idBuffer); + logger.debug("Nexio MediaListener next id: " + stringId); fireEvent(stringId, notificationNumber); - //read next ids writeBytes(LIST_NEXT_ID_LIST); - //for (buffer = readBytes(2); LIST_NEXT_ID_LIST_FOUND.equals(bytesToHex(buffer)) && isRunning; buffer = readBytes(2)) { - for (buffer = collectNotifications(true); LIST_NEXT_ID_LIST_FOUND.equals(bytesToHex(buffer)) - && isRunning; buffer = collectNotifications(true)) { - - byte[] idBuffer = readBytes(8); - stringId = new String(idBuffer); - logger.debug("Nexio MediaListener next id: " + stringId); - fireEvent(stringId, notificationNumber); - - writeBytes(LIST_NEXT_ID_LIST); - } - logger.debug("Nexio MediaListener end of id list: " + bytesToHex(buffer)); - //SEND ACK - writeBytes(ACK); - - } else if (LIST_FIRST_ID_LIST_NOT_FOUND.equals(bytesToHex(buffer))) { - notificationList.remove(0); - logger.debug("Nexio MediaListener LIST_FIRST_ID_LIST ID not found!"); - } else { - logger.debug("Nexio MediaListener unknown answer has arrived for LIST_FIRST_ID_LIST: " + bytesToHex(buffer)); } - } catch (Exception exc) { - logger.error("", exc); + logger.debug("Nexio MediaListener end of id list: " + bytesToHex(buffer)); + //SEND ACK + writeBytes(ACK); + + } else if (LIST_FIRST_ID_LIST_NOT_FOUND.equals(bytesToHex(buffer))) { + notificationList.remove(0); + logger.debug("Nexio MediaListener LIST_FIRST_ID_LIST ID not found!"); + } else { + logger.debug("Nexio MediaListener unknown answer has arrived for LIST_FIRST_ID_LIST: " + bytesToHex(buffer)); } } - this.connection.disconnect(); } catch (Exception e) { - logger.error("", e); + logger.catching(e); + forceRestart(); } } - public void startListener() { - try { - createNexioConnection(); - this.isRunning = true; - Thread t = new Thread(this, "MediaListener"); - t.start(); - logger.info("Nexio MediaListener is starting..."); - } catch (Exception exc) { - logger.error("", exc); + private byte[] readBytes(int numberOfBytes) throws Exception { + byte[] ret = new byte[numberOfBytes]; + for (int c = 0; c < numberOfBytes; c++) { + ret[c] = (byte) this.connection.read(); } + return ret; } - public void stopListener() { - this.isRunning = false; + public void removeClipEventListener(ClipEventListener listener) { + this.clipEventListeners.remove(listener); } - private void writeBytes(byte[] toWrite) throws Exception { + public void startListener() { try { - this.connection.write(toWrite); - this.connection.flush(); + createNexioConnection(); + listen(); } catch (Exception e) { - restartListener(); - throw e; + logger.catching(e); } } + private void writeBytes(byte[] toWrite) throws Exception { + this.connection.write(toWrite); + this.connection.flush(); + } } diff --git a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/Mediabase.java b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/Mediabase.java index a5f8a6e7..b01ce276 100644 --- a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/Mediabase.java +++ b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/Mediabase.java @@ -1,21 +1,23 @@ package user.commons.nexio.api; -import user.commons.nexio.server.protocol.*; import java.io.IOException; import java.util.Iterator; +import user.commons.nexio.server.protocol.Id; +import user.commons.nexio.server.protocol.NexioServerProtocol; +import user.commons.nexio.server.protocol.ProtocolException; +import user.commons.nexio.server.protocol.Xid; + public interface Mediabase { + public MediaListener createMediaListener(boolean disabled, ClipEventListener listener); + public Clip getClip(Id id) throws IOException, ProtocolException; public Clip getClip(Xid xid) throws IOException, ProtocolException; public Iterator getClips() throws IOException, ProtocolException; - public MediaListener getMediaListener(); - - public void startMediaListener(); - public NexioServerProtocol getProtocol(); - + } diff --git a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/MediabaseImpl.java b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/MediabaseImpl.java index 12369a82..4c841bc8 100644 --- a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/MediabaseImpl.java +++ b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/MediabaseImpl.java @@ -71,6 +71,13 @@ class MediabaseImpl implements Mediabase { } } + @Override + public MediaListener createMediaListener(boolean disabled, ClipEventListener listener) { + mediaListener = new MediaListener(this, protocol.getConnection().getHost(), protocol.getConnection().getPort()); + mediaListener.addClipEventListener(listener); + return mediaListener; + } + @Override public Clip getClip(Id id) throws IOException, ProtocolException { if (id == null) { @@ -97,21 +104,9 @@ class MediabaseImpl implements Mediabase { return new Itr(); } - @Override - public MediaListener getMediaListener() { - return this.mediaListener; - } - @Override public NexioServerProtocol getProtocol() { return this.protocol; } - @Override - public void startMediaListener() { - if (this.mediaListener == null) { - this.mediaListener = new MediaListener(this, protocol.getConnection().getHost(), protocol.getConnection().getPort()); - } - } - } diff --git a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/samples/ListMetabaseContents.java b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/samples/ListMetabaseContents.java index 8eebed25..a197e622 100644 --- a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/samples/ListMetabaseContents.java +++ b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/samples/ListMetabaseContents.java @@ -1,131 +1,299 @@ package user.commons.nexio.api.samples; -import user.commons.nexio.server.protocol.*; -import user.commons.nexio.api.*; - import java.io.IOException; import java.net.UnknownHostException; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Iterator; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -public class ListMetabaseContents implements ClipEventListener{ - +import user.commons.nexio.api.ClipEvent; +import user.commons.nexio.api.ClipEventListener; +import user.commons.nexio.api.ClipNotFoundException; +import user.commons.nexio.api.Mediabase; +import user.commons.nexio.server.protocol.Connection; +import user.commons.nexio.server.protocol.Id; +import user.commons.nexio.server.protocol.ProtocolException; +import user.commons.nexio.server.protocol.TCPConnection; + +public class ListMetabaseContents implements ClipEventListener { + static private final char[] hexArray = "0123456789ABCDEF".toCharArray(); static private final Logger logger = LogManager.getLogger(); //MASK REQ RESP_NOT_NUM //00 00 00 01 0x00 IDs in Added List BC=0x01, NN=0x00, DATA=none //00 00 00 02 0x01 IDs in Deleted List BC=0x01, NN=0x01, DATA=none // 1+2 - static private final byte[] CHANGE_NOTIFICATION_REQ = { (byte) 0xC4, (byte)0xA5, 0,0,0,0x03}; //C4A5 + MASK=ADD+DELETE + static private final byte[] CHANGE_NOTIFICATION_REQ = { (byte) 0xC4, (byte) 0xA5, 0, 0, 0, 0x03 }; //C4A5 + MASK=ADD+DELETE //10 01 response immediately - static private final byte[] ACK = { (byte) 0x10, (byte)0x01}; - //CF A6 BC NN DATA NN=0 -> IDs are in added list, NN=1 -> IDs are in deleted list - static private final byte[] CHANGE_NOTIFICATION_RESP = { (byte) 0xCF, (byte)0xA6}; - //List First ID List (C1 4C) - static private final byte[] LIST_FIRST_ID_LIST_ADDED = { (byte) 0xC1, (byte)0x4C, (byte)0x02}; //2 added, 3 deleted - static private final byte[] LIST_FIRST_ID_LIST_DELETED = { (byte) 0xC1, (byte)0x4C, (byte)0x03}; //2 added, 3 deleted - //List First ID List (C0 4D) - static private final byte[] LIST_NEXT_ID_LIST = { (byte) 0xC0, (byte)0x4D}; - - + static private final byte[] ACK = { (byte) 0x10, (byte) 0x01 }; + //CF A6 BC NN DATA NN=0 -> IDs are in added list, NN=1 -> IDs are in deleted list + static private final byte[] CHANGE_NOTIFICATION_RESP = { (byte) 0xCF, (byte) 0xA6 }; + //List First ID List (C1 4C) + static private final byte[] LIST_FIRST_ID_LIST_ADDED = { (byte) 0xC1, (byte) 0x4C, (byte) 0x02 }; //2 added, 3 deleted + static private final byte[] LIST_FIRST_ID_LIST_DELETED = { (byte) 0xC1, (byte) 0x4C, (byte) 0x03 }; //2 added, 3 deleted + //List First ID List (C0 4D) + static private final byte[] LIST_NEXT_ID_LIST = { (byte) 0xC0, (byte) 0x4D }; + static private final String LIST_FIRST_ID_LIST_NOT_FOUND = "D04C"; static private final String LIST_NEXT_ID_LIST_NOT_FOUND = "D04D"; - + static private final String LIST_FIRST_ID_LIST_FOUND = "D84C"; static private final String LIST_NEXT_ID_LIST_FOUND = "D84D"; //static private final String LIST_FIRST_ID_LIST_FOUND = "DF4C"; //0x22, 0x23 foundhoz //static private final String LIST_NEXT_ID_LIST_FOUND = "D84D"; //0x22, 0x23 foundhoz - - - static private final int NN_ID_ADDED = 0; - static private final int NN_ID_DELETED = 1; - + + static private final int NN_ID_ADDED = 0; + static private final int NN_ID_DELETED = 1; + + public static String bytesToHex(byte[] bytes) { + char[] hexChars = new char[bytes.length * 2]; + for (int j = 0; j < bytes.length; j++) { + int v = bytes[j] & 0xFF; + hexChars[j * 2] = hexArray[v >>> 4]; + hexChars[j * 2 + 1] = hexArray[v & 0x0F]; + } + return new String(hexChars); + } + + private static Id getNextId(Connection connection) throws IOException, ProtocolException { + byte[] buffer = new byte[8]; + int c = connection.read(buffer, 0, 8); + if (c != 8) { + return null; //throw getException_InvalidResponseLength(c, 8, 8); + } else { + return new Id(new String(buffer)); + } + } + + private static int getNOFIds(Connection connection) throws IOException, ProtocolException { + byte[] buffer = new byte[2]; + int c = connection.read(buffer, 0, 2); + //2 bytes hosszan az idokozben hozzaadott id-k szamossaga + if (c < 2) { + throw new ProtocolException("c, 2, 2"); + } + //MSB, LSB + int nofIds = ((buffer[0] << 8) & 0xFF00) | (buffer[1] & 0x00FF); //00000001 10010000 + return nofIds; + } + + public static void main(String[] args) throws UnknownHostException, IOException, ProtocolException, ClipNotFoundException { + ListMetabaseContents lmc = new ListMetabaseContents(); + + /* + + + //MASK REQ RESP_NOT_NUM + //00 00 00 01 0x00 IDs in Added List BC=0x01, NN=0x00, DATA=none + //00 00 00 02 0x01 IDs in Deleted List BC=0x01, NN=0x01, DATA=none + byte[] CHANGE_NOTIFICATION_REQ = { (byte) 0xC4, (byte)0xA5, 0,0,0,0x03}; //+C4A5 + MASK + + //10 01 response immediately + byte[] ACK = { (byte) 0x10, (byte)0x01}; + + //CF A6 BC NN DATA NN=0 -> IDs are in added list, NN=1 -> IDs are in deleted list + byte[] CHANGE_NOTIFICATION_RESP = { (byte) 0xCF, (byte)0xA6}; + + //List First ID List (C1 4C) + byte[] LIST_FIRST_ID_LIST_ADDED = { (byte) 0xC1, (byte)0x4C, (byte)0x02}; //2 added, 3 deleted + byte[] LIST_FIRST_ID_LIST_DELETED = { (byte) 0xC1, (byte)0x4C, (byte)0x03}; //2 added, 3 deleted + + //List First ID List (C0 4D) + byte[] LIST_NEXT_ID_LIST = { (byte) 0xC0, (byte)0x4D}; + + + Connection connection = TCPConnection.getInstance(NEXIO_HOST, 557); + // connection.write(ID_LIST); + // connection.flush(); + + connection.write(CHANGE_NOTIFICATION_REQ); + connection.flush(); + //ACK + byte[] buffer = new byte[2]; + int c = connection.read(buffer, 0, 2); + + + while("1".equals("1")){ + + //CHANGE_NOTIFICATION_RESP (blocking read) + buffer = new byte[4]; + c = connection.read(buffer,0,4); //CFA6 BC NN -> 0=added, 1=deleted + System.out.println("change notification resp: " + bytesToHex(buffer)); + + //ACK sending back + connection.write(ACK); + connection.flush(); + + //LIST_FIRST_ID_LIST + + //0x01 Main ID Handle List D8 4C ID + //0x02 ID Handles Added List D8 4C ID + //0x03 ID Handles Deleted List D8 4C ID + //0x11 Main Extended ID List DF 4C BC XID * + //0x12 Extended IDs Added List DF 4C BC XID * + //0x13 Extended IDs Deleted List DF 4C BC XID * + //0x21 Main ID Handle/Extended ID List DF 4C BC ID XID * + //0x22 ID Handles/Extended IDs Added List DF 4C BC ID XID * + //0x23 ID Handles/Extended IDs Deleted List DF 4C BC ID XID * + + if(buffer[3] == 0){ + connection.write(LIST_FIRST_ID_LIST_ADDED); + System.out.println("NN ADDED"); + }else + if(buffer[3] == 1){ + connection.write(LIST_FIRST_ID_LIST_DELETED); + System.out.println("NN DELETED"); + } + connection.flush(); + + //READ FIRST ID + buffer = new byte[2]; + c = connection.read(buffer, 0, 2); + if("D84C".equals(bytesToHex(buffer))){ + System.out.println("reading first id..."); + + //added or deleted + buffer = new byte[8]; + c = connection.read(buffer, 0, 8); + System.out.println("FIRST ID: " + new String(buffer)); + + //C0 4D List Next ID List + connection.write(LIST_NEXT_ID_LIST); + connection.flush(); + + buffer = new byte[2]; + //D04D amikor elfogy + for(connection.read(buffer, 0, 2); bytesToHex(buffer).equals("D84D"); connection.read(buffer, 0, 2)){ + + buffer = new byte[8]; + c = connection.read(buffer, 0, 8); + System.out.println("NEXT ID: " + new String(buffer)); + + connection.write(LIST_NEXT_ID_LIST); + connection.flush(); + + buffer = new byte[2]; + } + //System.out.println("LIST_NEXT_ID_LIST RESP: " + bytesToHex(buffer)); + }else + if(bytesToHex(buffer).equals("D04C")){ + System.out.println("No one found for first ID!"); + }else{ + System.out.println("Error in list first id list response: " + bytesToHex( buffer)); + } + } + + + //int i = connection.read(); + connection.disconnect(); + */ + } + private ArrayList clipEventListeners = new ArrayList(); private boolean isRunning = false; + private Mediabase mediaBase = null; private int nexioPort = 557; - private String nexioHost = "10.228.43.38"; //echo: "10.10.1.55"; vmware: "10.228.43.18" + + private String nexioHost = "10.228.43.38"; //echo: "10.10.1.55"; vmware: "10.228.43.18" + private Connection connection = null; - - //KONSTRUKTOR - public ListMetabaseContents(){ - try{ + public ListMetabaseContents() { + try { this.isRunning = true; createNexioConnection(); this.testAddDelete(); - -/* - Controller controller = new Controller(nexioHost); - controller.connect(); - System.out.println("Connected."); -*/ - -/* - Iterator clips = mediabase.getClips(); - while (clips.hasNext()) { - Clip clip = clips.next(); - // Unique internal ID - String id = clip.getId().get(); - // Extended ID (Filename) - String xid = clip.getXid().get(); - // Number of frames - long duration = clip.getDuration(); - // Timestamp of last modification - Calendar modifiedTimestamp = clip.getModifiedTimestamp(); - //Video Format - int videoFormat = clip.getVideoFormat(); - //Video Bitrate - int videoBitrate = clip.getVideoBitrate(); - - System.out.println(String.format("ID = '%s'\tXID = '%s'\tDuration = '%s'\tModified = '%s'\tVideoFormat = '%s'\tVideoBitrate = '%s'", - id, xid, duration, modifiedTimestamp.getTime(), videoFormat, videoBitrate)); - } - //Clip clip = mediabase.getClip(new Xid("xdcam-pal-d10-imx30-8")); - //long duration2 = clip.getDuration(); - //System.out.println(clip + " dur: "+ duration2); -*/ + /* + Controller controller = new Controller(nexioHost); + controller.connect(); + System.out.println("Connected."); + */ + + /* + Iterator clips = mediabase.getClips(); + while (clips.hasNext()) { + Clip clip = clips.next(); + // Unique internal ID + String id = clip.getId().get(); + // Extended ID (Filename) + String xid = clip.getXid().get(); + // Number of frames + long duration = clip.getDuration(); + // Timestamp of last modification + Calendar modifiedTimestamp = clip.getModifiedTimestamp(); + //Video Format + int videoFormat = clip.getVideoFormat(); + //Video Bitrate + int videoBitrate = clip.getVideoBitrate(); + + System.out.println(String.format("ID = '%s'\tXID = '%s'\tDuration = '%s'\tModified = '%s'\tVideoFormat = '%s'\tVideoBitrate = '%s'", + id, xid, duration, modifiedTimestamp.getTime(), videoFormat, videoBitrate)); + } - }catch(Exception exc){ + //Clip clip = mediabase.getClip(new Xid("xdcam-pal-d10-imx30-8")); + //long duration2 = clip.getDuration(); + //System.out.println(clip + " dur: "+ duration2); + */ + + } catch (Exception exc) { exc.printStackTrace(); } } - - public void clipEventPerformed(ClipEvent evt){ + + @Override + public void clipEventPerformed(ClipEvent evt) { System.out.println("clipEventPerformed " + evt.getEventType() + ", " + evt.getClip()); } - - - public void testAddDelete(){ - try{ + + private void createNexioConnection() throws Exception { + this.connection = TCPConnection.getInstance(this.nexioHost, this.nexioPort); + System.out.println("Nexio MediaListener is connected to NEXIO " + this.nexioHost + " : " + this.nexioPort); + } + + private byte[] readBytes(int numberOfBytes) throws Exception { + try { + byte[] ret = new byte[numberOfBytes]; + this.connection.read(ret, 0, numberOfBytes); + return ret; + } catch (Exception e) { + //restartListener(); + throw e; + } + } + + @Override + public void restart() { + // TODO Auto-generated method stub + + } + + public void testAddDelete() { + try { //SEND CHANGE NOTIFICATION REQ writeBytes(CHANGE_NOTIFICATION_REQ); - + //ACK byte[] buffer = readBytes(2); System.out.println("Nexio MediaListener is waiting for events.."); - - while(isRunning){ - try{ + + while (isRunning) { + try { //CHANGE_NOTIFICATION_RESP (BLOCKING READ!) //TEST CODE - if(buffer.length == 2){ - buffer = readBytes(4); //CFA6 BC NN->0=added, 1=deleted + if (buffer.length == 2) { + buffer = readBytes(4); //CFA6 BC NN->0=added, 1=deleted } - + int notificationNumber = buffer[3]; System.out.println("Nexio MediaListener notification: " + bytesToHex(buffer)); - + //SEND ACK -// writeBytes(ACK); - + // writeBytes(ACK); + //LIST_FIRST_ID_LIST /* 0x01 Main ID Handle List D8 4C ID @@ -138,272 +306,101 @@ public class ListMetabaseContents implements ClipEventListener{ 0x22 ID Handles/Extended IDs Added List DF 4C BC ID XID * 0x23 ID Handles/Extended IDs Deleted List DF 4C BC ID XID * */ - if(notificationNumber == NN_ID_ADDED){ + if (notificationNumber == NN_ID_ADDED) { writeBytes(LIST_FIRST_ID_LIST_ADDED); - }else - if(notificationNumber == NN_ID_DELETED){ -writeBytes(ACK); + } else if (notificationNumber == NN_ID_DELETED) { + writeBytes(ACK); writeBytes(LIST_FIRST_ID_LIST_DELETED); } - + //LIST_FIRST_ID_LIST response buffer = readBytes(2); - if(LIST_FIRST_ID_LIST_FOUND.equals(bytesToHex(buffer))){ -/* - * //extended id version 0x22, 0x23 - //found 2, BC 1, ID 8, XID {BC-8} - buffer = readBytes(1); - int bc = buffer[0]; - buffer = readBytes(8); - String stringId = new String(buffer); - buffer = readBytes(bc-8); - String xid = new String(buffer); - System.out.println("Nexio MediaListener first id: " + stringId + ", xid: " + xid); -*/ - - //found 2, ID 8 + if (LIST_FIRST_ID_LIST_FOUND.equals(bytesToHex(buffer))) { + /* + * //extended id version 0x22, 0x23 + //found 2, BC 1, ID 8, XID {BC-8} + buffer = readBytes(1); + int bc = buffer[0]; + buffer = readBytes(8); + String stringId = new String(buffer); + buffer = readBytes(bc-8); + String xid = new String(buffer); + System.out.println("Nexio MediaListener first id: " + stringId + ", xid: " + xid); + */ + + //found 2, ID 8 //read first id buffer = readBytes(8); String stringId = new String(buffer); System.out.println("Nexio MediaListener first id: " + stringId); //fireEvent(stringId, notificationNumber); - + //read next ids writeBytes(LIST_NEXT_ID_LIST); - - for(buffer=readBytes(2); LIST_NEXT_ID_LIST_FOUND.equals(bytesToHex(buffer)) && isRunning; buffer=readBytes(2)){ - + + for (buffer = readBytes(2); LIST_NEXT_ID_LIST_FOUND.equals(bytesToHex(buffer)) && isRunning; buffer = readBytes(2)) { + byte[] idBuffer = readBytes(8); stringId = new String(idBuffer); System.out.println("Nexio MediaListener next id: " + stringId); //fireEvent(stringId, notificationNumber); - -/* - //extended id 0x22, 0x23 versions - //found 2, BC 1, ID 8, XID {BC-8} - buffer = readBytes(1); - bc = buffer[0]; - buffer = readBytes(8); - stringId = new String(buffer); - buffer = readBytes(bc-8); - xid = new String(buffer); - System.out.println("Nexio MediaListener next id: " + stringId + ", xid: " + xid); -*/ + + /* + //extended id 0x22, 0x23 versions + //found 2, BC 1, ID 8, XID {BC-8} + buffer = readBytes(1); + bc = buffer[0]; + buffer = readBytes(8); + stringId = new String(buffer); + buffer = readBytes(bc-8); + xid = new String(buffer); + System.out.println("Nexio MediaListener next id: " + stringId + ", xid: " + xid); + */ writeBytes(LIST_NEXT_ID_LIST); } System.out.println("Nexio MediaListener end of id list: " + bytesToHex(buffer)); -writeBytes(ACK); - + writeBytes(ACK); + //TEST CODE - if(bytesToHex(buffer).equals("CFA6")){ + if (bytesToHex(buffer).equals("CFA6")) { buffer = readBytes(2); - byte[] newBuffer = new byte[]{(byte)0xCF,(byte)0xA6, buffer[0], buffer[1]}; + byte[] newBuffer = new byte[] { (byte) 0xCF, (byte) 0xA6, buffer[0], buffer[1] }; buffer = newBuffer; } - - - }else - if(LIST_FIRST_ID_LIST_NOT_FOUND.equals(bytesToHex(buffer))){ + + } else if (LIST_FIRST_ID_LIST_NOT_FOUND.equals(bytesToHex(buffer))) { System.out.println("Nexio MediaListener LIST_FIRST_ID_LIST ID not found!"); - }else{ - System.out.println("Nexio MediaListener unknown answer has arrived for LIST_FIRST_ID_LIST: " + bytesToHex( buffer)); + } else { + System.out.println("Nexio MediaListener unknown answer has arrived for LIST_FIRST_ID_LIST: " + bytesToHex(buffer)); } - }catch(Exception exc){ + } catch (Exception exc) { exc.printStackTrace(); } } this.connection.disconnect(); - }catch(Exception e){ + } catch (Exception e) { e.printStackTrace(); } } - - private byte[] readBytes(int numberOfBytes) throws Exception{ - try{ - byte[] ret = new byte[numberOfBytes]; - this.connection.read(ret, 0, numberOfBytes); - return ret; - }catch(Exception e){ - //restartListener(); - throw e; - } - } - - private void writeBytes(byte[] toWrite) throws Exception{ - try{ + private void writeBytes(byte[] toWrite) throws Exception { + try { //testcode calc checksum -/* - byte[] finalbytes = Arrays.copyOf(toWrite, toWrite.length + 1); - long chksum = 0; - for(int c=0; c IDs are in added list, NN=1 -> IDs are in deleted list - byte[] CHANGE_NOTIFICATION_RESP = { (byte) 0xCF, (byte)0xA6}; - - //List First ID List (C1 4C) - byte[] LIST_FIRST_ID_LIST_ADDED = { (byte) 0xC1, (byte)0x4C, (byte)0x02}; //2 added, 3 deleted - byte[] LIST_FIRST_ID_LIST_DELETED = { (byte) 0xC1, (byte)0x4C, (byte)0x03}; //2 added, 3 deleted - - //List First ID List (C0 4D) - byte[] LIST_NEXT_ID_LIST = { (byte) 0xC0, (byte)0x4D}; - - - Connection connection = TCPConnection.getInstance(NEXIO_HOST, 557); -// connection.write(ID_LIST); -// connection.flush(); - - connection.write(CHANGE_NOTIFICATION_REQ); - connection.flush(); - //ACK - byte[] buffer = new byte[2]; - int c = connection.read(buffer, 0, 2); - - - while("1".equals("1")){ - - //CHANGE_NOTIFICATION_RESP (blocking read) - buffer = new byte[4]; - c = connection.read(buffer,0,4); //CFA6 BC NN -> 0=added, 1=deleted - System.out.println("change notification resp: " + bytesToHex(buffer)); - - //ACK sending back - connection.write(ACK); - connection.flush(); - - //LIST_FIRST_ID_LIST - - //0x01 Main ID Handle List D8 4C ID - //0x02 ID Handles Added List D8 4C ID - //0x03 ID Handles Deleted List D8 4C ID - //0x11 Main Extended ID List DF 4C BC XID * - //0x12 Extended IDs Added List DF 4C BC XID * - //0x13 Extended IDs Deleted List DF 4C BC XID * - //0x21 Main ID Handle/Extended ID List DF 4C BC ID XID * - //0x22 ID Handles/Extended IDs Added List DF 4C BC ID XID * - //0x23 ID Handles/Extended IDs Deleted List DF 4C BC ID XID * - - if(buffer[3] == 0){ - connection.write(LIST_FIRST_ID_LIST_ADDED); - System.out.println("NN ADDED"); - }else - if(buffer[3] == 1){ - connection.write(LIST_FIRST_ID_LIST_DELETED); - System.out.println("NN DELETED"); - } - connection.flush(); - - //READ FIRST ID - buffer = new byte[2]; - c = connection.read(buffer, 0, 2); - if("D84C".equals(bytesToHex(buffer))){ - System.out.println("reading first id..."); - - //added or deleted - buffer = new byte[8]; - c = connection.read(buffer, 0, 8); - System.out.println("FIRST ID: " + new String(buffer)); - - //C0 4D List Next ID List - connection.write(LIST_NEXT_ID_LIST); - connection.flush(); - - buffer = new byte[2]; - //D04D amikor elfogy - for(connection.read(buffer, 0, 2); bytesToHex(buffer).equals("D84D"); connection.read(buffer, 0, 2)){ - - buffer = new byte[8]; - c = connection.read(buffer, 0, 8); - System.out.println("NEXT ID: " + new String(buffer)); - - connection.write(LIST_NEXT_ID_LIST); - connection.flush(); - - buffer = new byte[2]; - } - //System.out.println("LIST_NEXT_ID_LIST RESP: " + bytesToHex(buffer)); - }else - if(bytesToHex(buffer).equals("D04C")){ - System.out.println("No one found for first ID!"); - }else{ - System.out.println("Error in list first id list response: " + bytesToHex( buffer)); - } - } - - - //int i = connection.read(); - connection.disconnect(); -*/ - } - - - - private static Id getNextId(Connection connection) throws IOException, ProtocolException{ - byte[] buffer = new byte[8]; - int c = connection.read(buffer, 0, 8); - if (c != 8){ - return null; //throw getException_InvalidResponseLength(c, 8, 8); - }else{ - return new Id(new String(buffer)); - } - } - - - private static int getNOFIds(Connection connection) throws IOException, ProtocolException{ - byte[] buffer = new byte[2]; - int c = connection.read(buffer, 0, 2); - //2 bytes hosszan az idokozben hozzaadott id-k szamossaga - if (c < 2) { - throw new ProtocolException("c, 2, 2"); - } - //MSB, LSB - int nofIds = ((buffer[0] << 8) & 0xFF00) | (buffer[1] & 0x00FF); //00000001 10010000 - return nofIds; - } - - - public static String bytesToHex(byte[] bytes) { - char[] hexChars = new char[bytes.length * 2]; - for ( int j = 0; j < bytes.length; j++ ) { - int v = bytes[j] & 0xFF; - hexChars[j * 2] = hexArray[v >>> 4]; - hexChars[j * 2 + 1] = hexArray[v & 0x0F]; - } - return new String(hexChars); - } - - } diff --git a/server/user.jobengine.osgi.server/pages/joblist.zul b/server/user.jobengine.osgi.server/pages/joblist.zul index 562472af..a0a82f94 100644 --- a/server/user.jobengine.osgi.server/pages/joblist.zul +++ b/server/user.jobengine.osgi.server/pages/joblist.zul @@ -23,6 +23,7 @@