001/*
002 *  $Id: AjaxRestWebsocketListener.java 997 2015-08-21 00:06:40Z tgutwin $
003 *  $HeadURL:  $
004 *  $Revision: 997 $
005 *  $LastChangedDate: 2015-08-20 17:06:40 -0700 (Thu, 20 Aug 2015) $
006 *  $LastChangedBy: tgutwin $
007 *  Copyright (c) 2017 Tom B. Gutwin P.Eng. North Vancouver BC Canada
008 *
009 *  This program is free software; you can redistribute it and/or
010 *  modify it under the terms of the GNU General Public License
011 *  as published by the Free Software Foundation; either version 3
012 *  of the License, or any later version.
013 *
014 *  This program is distributed in the hope that it will be useful,
015 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
016 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 *  GNU General Public License for more details.
018 *
019 *  You should have received a copy of the GNU General Public License
020 *  along with this program; If not, see <http://www.gnu.org/licenses/>.
021 */
022
023package ca.bc.webarts.servlet;
024
025import java.util.concurrent.CountDownLatch;
026import java.util.concurrent.Future;
027import java.util.concurrent.TimeUnit;
028
029import org.eclipse.jetty.websocket.api.Session;
030import org.eclipse.jetty.websocket.api.WebSocketAdapter;
031
032/**
033  * A very basic event socket impl that the TomsWebsocketEchoServlet uses to handle webSocket Text Events.
034  **/
035public class TomsEventSocket extends WebSocketAdapter
036{
037  Session session_ = null;
038  private final CountDownLatch closeLatch = null;
039  public static boolean debugOut = true;
040
041
042  public TomsEventSocket()
043  {
044    super();
045    if (debugOut) System.out.println("TomsEventSocket()");
046    //this.closeLatch = new CountDownLatch(1);
047  }
048
049
050  @Override
051  public void onWebSocketConnect(Session sess)
052  {
053    super.onWebSocketConnect(sess);
054    if (debugOut) System.out.println("Socket Connected: " + sess);
055    session_ = sess;
056  }
057
058
059  @Override
060  public void onWebSocketText(String message)
061  {
062    super.onWebSocketText(message);
063    String img = "";
064    if (debugOut) System.out.println("Received TEXT message: " + message);
065    if(message.startsWith("img")) img=message.substring(3).trim();
066    try
067    {
068      if(session_!=null)
069      {
070        session_.getRemote().sendString("  Received: "+message);
071        if(img!=null && !"".equals(img) )
072        {
073          System.out.println("  )))) Socket Received image request for: " + img);
074          try
075          {
076            java.io.File fi = new java.io.File(img);
077            if(fi!=null)
078            {
079              byte[] fileContent=null;
080              fileContent = java.nio.file.Files.readAllBytes(fi.toPath());
081              java.nio.ByteBuffer buf = java.nio.ByteBuffer.wrap(fileContent);
082
083              session_.getRemote().sendBytes(buf);
084            }
085            else
086              session_.getRemote().sendString("  ERROR: cannot retrieve img file: "+img);
087          }
088          catch (java.io.IOException e)
089          {
090            session_.getRemote().sendString("  ERROR: cannot find img file: "+img);
091            // TODO Auto-generated catch block
092            e.printStackTrace();
093          }
094        }
095      }
096    }
097    catch (java.io.IOException ioEx)
098    {
099      System.out.println("ERROR: can't get remote host connection");
100    }
101  }
102
103
104  //@OnClose
105  @Override
106  public void onWebSocketClose(int statusCode, String reason)
107  {
108    super.onWebSocketClose(statusCode,reason);
109    System.out.println("Socket Closed: [" + statusCode + "] " + reason);
110    session_ = null;
111  }
112
113
114  //@OnError
115  @Override
116  public void onWebSocketError(Throwable cause)
117  {
118      super.onWebSocketError(cause);
119      cause.printStackTrace(System.err);
120  }
121}