001package csheets.ext.share.core;
002
003import java.io.*;
004import java.net.Socket;
005import java.util.*;
006import java.util.logging.*;
007
008import csheets.core.Cell;
009
010/**
011 * Class that implement the server in the extension
012 * 
013 * @author Andre
014 * 
015 */
016public class ThreadServer extends Observable implements Runnable {
017        /** the cells we will pass throw network */
018        private final Cell[][] cells;
019        /** server socket */
020        private final Socket sock;
021        /** connection password */
022        private final String password;
023        /** the observer class */
024        private final Observer observer;
025
026        /**
027         * Create internaly a new client
028         * 
029         * @param port
030         *            the connection port
031         * @param cells
032         *            the cells we will pass throw network
033         * @param password
034         *            the connection password
035         * @param observer
036         *            the observer class
037         */
038        public ThreadServer(Cell[][] cells, Socket sock, String password,
039                        Observer observer) {
040                this.cells = cells;
041                this.sock = sock;
042                this.password = password;
043                this.observer = observer;
044        }
045
046        /**
047         * Method that will send the information throw network
048         * 
049         * @param cells
050         *            value that will be shared throw network
051         * @param sock
052         *            the socket of connection
053         * @throws IOException
054         *             throw this exception if the I/O have errors
055         */
056        private synchronized void send(Cell[][] cells, Socket sock)
057                        throws IOException {
058                boolean isAlive = true;
059                while (isAlive) {
060
061                        DataInputStream in = new DataInputStream(sock.getInputStream());
062                        if (in.readUTF().equals(password)) {
063                                DataOutputStream sendProps = new DataOutputStream(
064                                                sock.getOutputStream());
065                                sendProps.writeUTF(Server.getInstance().getProperties());
066                                for (int i = 0; i < cells.length; i++) {
067                                        for (int j = 0; j < cells[i].length; j++) {
068                                                CellNetwork cell = new CellNetwork(
069                                                                cells[i][j].getContent(), i, j, true);
070                                                ObjectOutputStream outStream = new ObjectOutputStream(
071                                                                sock.getOutputStream());
072                                                outStream.writeObject(cell);
073                                        }
074                                }
075
076                                CellNetwork cell = new CellNetwork("", 0, 0, false);
077                                ObjectOutputStream outStream = new ObjectOutputStream(
078                                                sock.getOutputStream());
079                                outStream.writeObject(cell);
080                        }
081                        if (in.readUTF().equals("Close yourself")) {
082                                isAlive = false;
083                        }
084                }
085        }
086
087        /**
088         * method to send changes to all clients Not called yet because don't be
089         * operation
090         * 
091         * @param cells
092         *            the cells to be shared
093         * @throws IOException
094         *             throws if a I/O exception occurs
095         * @throws InterruptedException
096         *             throws if a thread exception occurs
097         */
098
099        private synchronized void sendAllClients(Cell[][] cells)
100                        throws IOException, InterruptedException {
101
102                while (true) {
103                        Thread.sleep(100);
104
105                        if (Server.getInstance().getListener().getFlag() == true) {
106
107                                for (int c = 0; c < Server.getInstance().getSockets().size(); c++) {
108
109                                        OutputStream out = Server.getInstance().getSockets().get(c)
110                                                        .getOutputStream();
111                                        DataOutputStream outStreamMessage = new DataOutputStream(
112                                                        out);
113                                        outStreamMessage.writeUTF("server- send me updated data");
114                                        for (int i = 0; i < cells.length; i++) {
115                                                for (int j = 0; j < cells[i].length; j++) {
116                                                        CellNetwork cell = new CellNetwork(
117                                                                        cells[i][j].getContent(), i, j, true);
118                                                        ObjectOutputStream outStream = new ObjectOutputStream(
119                                                                        Server.getInstance().getSockets().get(c)
120                                                                                        .getOutputStream());
121                                                        outStream.writeObject(cell);
122                                                }
123                                        }
124
125                                        CellNetwork cell = new CellNetwork("", 0, 0, false);
126                                        ObjectOutputStream outStream = new ObjectOutputStream(
127                                                        Server.getInstance().getSockets().get(c)
128                                                                        .getOutputStream());
129                                        outStream.writeObject(cell);
130                                        Server.getInstance().getListener().setFlag(false);
131                                }
132
133                        }
134
135                }
136
137        }
138
139        /**
140         * running thread
141         */
142        @Override
143        public void run() {
144                try {
145                        addObserver(observer);
146                        while (true) {
147
148                                send(cells, sock);
149
150                                sendAllClients(cells);
151                        }
152
153                } catch (Exception e) {
154
155                        Logger.getLogger(ThreadServer.class.getName()).log(Level.SEVERE,
156                                        null, e);
157                        setChanged();
158                        notifyObservers();
159                        clearChanged();
160                }
161        }
162}