📄 networkeddevicemonitorcontrol.java
字号:
this.setTransmitState (this.STARTED); synchronized(transmitPausedLock) { transmitPausedLock.notifyAll (); } } public long getImageCaptureDelay () { return this.imageCaptureDelay; } public void setRefreshRate (int refreshRate) { if(refreshRate >= this.MINIMUM_REFRESH_RATE && refreshRate <= this.MAXIMUM_REFRESH_RATE) { this.refreshRate = refreshRate; this.imageCaptureDelay = ((Long)refreshRateTable.get (new Integer (refreshRate))).longValue (); }else{ this.refreshRate = this.DEFAULT_REFRESH_RATE; this.imageCaptureDelay = ((Long)refreshRateTable.get (new Integer (this.DEFAULT_REFRESH_RATE))).longValue (); } } public int getRefreshRate () { return this.refreshRate; } public void setURL (String urlString) { if (LOG.isEnabledFor (Level.INFO)) { LOG.info ("setUrl " + urlString); } this.urlString = urlString; } private URL getURL (String urlString ) { URL url = null; if(urlString != null) { try { url = new URL (urlString); }catch(MalformedURLException mux) { mux.printStackTrace (); if (LOG.isEnabledFor (Level.INFO)) { LOG.info ("Bad URL!! Make sure URL starts wtih http://"); } setDeviceError ("Bad URL!! Make sure URL starts wtih http://"); notifyDeviceErrorListeners (); return null; } }else{ setDeviceError ("Bad URL!! Make sure URL starts wtih http://"); notifyDeviceErrorListeners (); return null; } return url; } public void setAuthenticated (boolean authenticated) { if (LOG.isEnabledFor (Level.INFO)) { //LOG.info ("authenticated "+authenticated); } this.authenticated = authenticated; } public boolean isAuthenticated () { return this.authenticated; } public void setUserName (String userName) { if (LOG.isEnabledFor (Level.INFO)) { //LOG.info ("userName "+userName); } this.userName = userName; } public void setPassword (String password) { if (LOG.isEnabledFor (Level.INFO)) { //LOG.info ("password " + password); } this.password = password; } public String getUserName () { return userName; } public String getPassword () { return password; } public String getURLString () { return urlString; } private String getCredentials () { String rtn = null; if( getUserName () != null && getPassword () != null) { rtn = userName+":"+password; } return rtn; } private String getEncodedCredentials () { String encodedCredentials = null; String credentials = getCredentials (); if (LOG.isEnabledFor (Level.INFO)) { //LOG.info ("username:password "+getUserName ()+":"+getPassword ()); //LOG.info ("credentials "+credentials); } if(credentials != null) { encodedCredentials = Base64.encode (credentials); } return encodedCredentials; } public void addErrorListener (DeviceMonitorControl.DeviceErrorListener listener) { synchronized(deviceErrorListeners) { deviceErrorListeners.add (listener); } } private void notifyDeviceErrorListeners () { // this isn't thread safe Iterator itr = deviceErrorListeners.iterator (); while(itr.hasNext ()) { ((DeviceMonitorControl.DeviceErrorListener)itr.next ()). deviceErrorEncountered (getDeviceError ()); } } private String getDeviceError () { return this.deviceError; } private void setDeviceError (String deviceError) { this.deviceError = deviceError; } private HttpURLConnection getHttpConnection (URL url, boolean authenticate) { HttpURLConnection camServer = null; try { camServer = (HttpURLConnection) url.openConnection (); }catch(IOException iox) { iox.printStackTrace (); setDeviceError ("CamServer I/O Error."); notifyDeviceErrorListeners (); return null; }catch(NullPointerException npx) { npx.printStackTrace (); if (LOG.isEnabledFor (Level.INFO)) { LOG.info ("URL is NULL!"); } setDeviceError ("Bad URL"); notifyDeviceErrorListeners (); return null; }catch(ClassCastException ccx) { ccx.printStackTrace (); if (LOG.isEnabledFor (Level.INFO)) { LOG.info ("This is not a HttpURLConnection"); } setDeviceError ("Incorrect URL connection type. Expected URL connection."); notifyDeviceErrorListeners (); return null; }catch(Exception x) { x.printStackTrace (); setDeviceError ("General CamServer Error."); notifyDeviceErrorListeners (); return null; } if(camServer == null) { if (LOG.isEnabledFor (Level.INFO)) { //LOG.info ("authenticating"); } setDeviceError ("CamServer Error."); notifyDeviceErrorListeners (); return null; } try{ camServer.setAllowUserInteraction (false); camServer.setDoInput (true); camServer.setRequestMethod ("GET"); }catch(ProtocolException px) { px.printStackTrace (); setDeviceError ("CamServer protocol error: " + px.getMessage ()); notifyDeviceErrorListeners (); return null; }catch(Exception x) { x.printStackTrace (); setDeviceError ("CamServer General Error: " + x.getMessage ()); notifyDeviceErrorListeners (); return null; } camServer.setRequestProperty ("Keep-Alive", "300"); if(authenticate) { if (LOG.isEnabledFor (Level.INFO)) { // LOG.info ("authenticating"); } camServer.setRequestProperty ("Authorization", "Basic " + getEncodedCredentials ()); }else{ if (LOG.isEnabledFor (Level.INFO)) { //LOG.info ("NOT authenticating"); } } return camServer; } private void setMimeType (String mimeType) { this.mimeType = mimeType; } public String getMimeType () { return this.mimeType; } public String getResponseString () { return this.responseString; } public void setResponseString (String responseString) { this.responseString = responseString; } private ByteArrayOutputStream requestURLImage () { if (LOG.isEnabledFor (Level.INFO)) { //LOG.info("requestURLImage"); } ByteArrayOutputStream imageByteArray = new ByteArrayOutputStream (); BufferedInputStream in = null; int contentLength = 0; HttpURLConnection camServer = getHttpConnection (getURL (getURLString ()), isAuthenticated ()); try{ camServer.connect (); }catch(SocketTimeoutException stx) { stx.printStackTrace (); setDeviceError ("CamServer Timeout Error"); notifyDeviceErrorListeners (); return null; }catch(IOException iox) { iox.printStackTrace (); setDeviceError ("CamServer I/O Error."); notifyDeviceErrorListeners (); return null; }catch(Exception x) { x.printStackTrace (); setDeviceError ("General CamServer Device Access Error."); notifyDeviceErrorListeners (); return null; } //dont do anythign more and return null if content length is nada or 1 try{ in = new BufferedInputStream ( camServer.getInputStream () ) ; }catch(IOException iox) { iox.printStackTrace (); setDeviceError ("CamServer I/O Error."); notifyDeviceErrorListeners (); return null; } try{ setResponseString (camServer.getResponseMessage ()); if (LOG.isEnabledFor (Level.INFO)) { //LOG.info ("response "+getResponseString ()); } setMimeType (camServer.getHeaderField ("Conent-Type")); if (LOG.isEnabledFor (Level.INFO)) { //LOG.info("mimeType "+getMimeType()); //LOG.info("content length "+camServer.getHeaderField ("Content-Length")); } contentLength = Integer.valueOf (camServer.getHeaderField ("Content-Length")).intValue (); }catch(IOException iox) { iox.printStackTrace (); setDeviceError ("CamServer I/O Error"); notifyDeviceErrorListeners (); return null; }catch(NumberFormatException nfx) { nfx.printStackTrace (); if (LOG.isEnabledFor (Level.INFO)) { LOG.info ("Content Length not Parsable"); } setDeviceError ("Content Length Not Reabable Error."); notifyDeviceErrorListeners (); return null; } // newline;CR i think // if (contentLength > 1) { contentLength-=2; } //dont do anythign more and return null if content length is nada or 1 try{ in = new BufferedInputStream ( camServer.getInputStream () ) ; }catch(IOException iox) { iox.printStackTrace (); setDeviceError ("CamServer I/O Exception"); notifyDeviceErrorListeners (); return null; } int contentLengthCounter = contentLength; while(contentLengthCounter > 0) { int readLength = 0; byte[] byteBuffer = new byte[this.SERVER_CHUNK_READ_SIZE]; //1460 try{ readLength = in.read (byteBuffer); }catch(IOException iox) { iox.printStackTrace (); setDeviceError ("CamServer Data Read Error");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -