Changeset 838


Ignore:
Timestamp:
02/05/14 11:15:35 (10 years ago)
Author:
jts21
Message:

Implement automatically downloading images from FrameShare servers. Also includes code for uploading images, but that's not ready yet.

Location:
trunk
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/ItemUtils.java

    r778 r838  
    2626import org.expeditee.items.widgets.WidgetCorner;
    2727import org.expeditee.items.widgets.WidgetEdge;
     28import org.expeditee.network.FrameShare;
    2829import org.expeditee.settings.folders.FolderSettings;
    2930
     
    344345         *         not found.
    345346         */
    346         public static Picture CreatePicture(Text source, ImageObserver observer) {
     347        public static Picture CreatePicture(Text source, ImageObserver observer, boolean tryRemote) {
    347348                String text = source.getText();
    348349                String path = "";
     
    398399                                path = file.getPath();
    399400
    400                         // if the image isn't found by now, give up.
     401                        // if the image isn't found by now, try remote servers
    401402                        file = new File(path);
    402403                        if (!file.exists() || file.isDirectory()) {
     404                        if(tryRemote && FrameShare.getInstance().loadImage(fileName, null)) {
     405                                // call CreatePicture again, but with tryRemote set to false so we won't get into an infinite loop
     406                                // if something goes wrong with finding the downloaded image
     407                                return CreatePicture(source, observer, false);
     408                        }
    403409                                return null;
    404410                        }
     
    417423                }
    418424
     425        }
     426       
     427        public static Picture CreatePicture(Text source, ImageObserver observer) {
     428                return CreatePicture(source, observer, true);
    419429        }
    420430
  • trunk/src/org/expeditee/network/DefaultServer.java

    r649 r838  
    1010        protected static final int FRAMENAME_PACKET_LENGTH = 1000;
    1111       
    12         private boolean _stop = false;
     12        protected boolean _stop = false;
    1313        protected DatagramSocket socket = null;
    1414
     
    1919        public DefaultServer(Runnable target) {
    2020                super(target);
     21        }
     22       
     23        protected DefaultServer(String name) {
     24                super(name);
    2125        }
    2226
  • trunk/src/org/expeditee/network/FrameShare.java

    r751 r838  
    22
    33import java.io.BufferedReader;
     4import java.io.File;
     5import java.io.FileOutputStream;
    46import java.io.IOException;
     7import java.io.InputStream;
    58import java.io.StringReader;
    69import java.io.StringWriter;
     
    811import java.net.DatagramSocket;
    912import java.net.InetAddress;
     13import java.net.Socket;
    1014import java.net.SocketException;
    1115import java.net.UnknownHostException;
     
    4751            _servers.add(new InfServer(_port));
    4852            _servers.add(new InfUpdate(_port));
     53            _servers.add(new ImageServer(_port));
     54            _servers.add(new ImageSaver(_port));
    4955
    5056        }
     
    203209
    204210                return frame;
     211        }
     212       
     213        /**
     214         * Downloads an image from the server (if it exists),
     215         * Saves it locally in the default image folder,
     216         * Then returns the filename
     217         *
     218         * @param imageName
     219         * @param peerName
     220         * @return true if the image was successfully downloaded and saved in the default images folder
     221         */
     222        public boolean loadImage(String imageName, String peerName) {
     223                boolean result = false;
     224                try {
     225                        if (peerName == null) {
     226                                for (Peer peer : _peers.values()) {
     227                                        try {
     228                                                result = getImage(imageName, peer);
     229                                                if(!result) {
     230                                                        continue;
     231                                                }
     232                                                peerName = peer.getName();
     233                                                break;
     234                                        } catch (Exception e) {
     235                                                e.printStackTrace();
     236                                        }
     237                                }
     238                        } else {
     239                                try {
     240                                        Peer peer = _peers.get(peerName.toLowerCase());
     241                                        if (peer != null) {
     242                                                result = getImage(imageName, peer);
     243                                        }
     244                                } catch (Exception e) {
     245                                        e.printStackTrace();
     246                                }
     247                        }
     248                } catch(Exception e) {
     249                        e.printStackTrace();
     250                }
     251                return result;
     252        }
     253       
     254        /**
     255         * Sends an image to the server,
     256         * So that other users can view the image on the server frames
     257         *
     258         * @param imageName
     259         * @param peerName
     260         */
     261        public void sendImage(String imageName, String peerName) {
     262               
     263        }
     264       
     265        private boolean getImage(String imageName, Peer peer) throws IOException {
     266                try(Socket socket = new Socket(peer.getAddress(), peer.getPort() + ImageServer.OFFSET)) {
     267                socket.setSoTimeout(NetworkSettings.FrameShareTimeout.get() * 2);
     268                byte[] fileName = imageName.getBytes();
     269                int fileNameLen = fileName.length;
     270                byte[] data = new byte[2 + fileNameLen];
     271                data[0] = (byte) ((fileNameLen >> 8) & 0xFF);
     272                data[1] = (byte) ((fileNameLen) & 0xFF);
     273                System.arraycopy(fileName, 0, data, 2, fileNameLen);
     274                socket.getOutputStream().write(data);
     275                socket.getOutputStream().flush();
     276                socket.shutdownOutput();
     277                InputStream is = socket.getInputStream();
     278            // file length is the first 4 bytes
     279            int fileLen = (is.read() << 24) | (is.read() << 16) | (is.read() << 8) | is.read();
     280            // if the above reads failed, fileLen will be -1
     281            if(fileLen <= 0) {
     282                return false;
     283            }
     284            System.out.println("Receiving " + new String(fileName, ImageServer.CHARSET) + " (" + fileLen + " bytes)");
     285            File f = new File(FrameIO.IMAGES_PATH + imageName);
     286            f.getParentFile().mkdirs();
     287            FileOutputStream out = new FileOutputStream(f);
     288            // file is the remaining bytes of the stream
     289            byte[] buf = null;
     290            if(fileLen > 1024) {
     291                buf = new byte[1024];
     292                while(fileLen > 1024) {
     293                        is.read(buf);
     294                        out.write(buf);
     295                        fileLen -= 1024;
     296                }
     297            }
     298            buf = new byte[fileLen];
     299            is.read(buf);
     300                out.write(buf);
     301                out.flush();
     302            out.close();
     303                socket.close();
     304                return true;
     305                } catch(Exception e) {
     306                        e.printStackTrace();
     307                        return false;
     308                }
    205309        }
    206310
Note: See TracChangeset for help on using the changeset viewer.