001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.objectmatrix.impl;
025    
026    import java.io.ByteArrayInputStream;
027    import java.io.ByteArrayOutputStream;
028    import java.io.ObjectInputStream;
029    import java.io.ObjectOutputStream;
030    import java.net.DatagramPacket;
031    import java.net.DatagramSocket;
032    import java.net.InetSocketAddress;
033    import java.net.SocketAddress;
034    
035    import org.ujmp.core.exceptions.MatrixException;
036    import org.ujmp.core.objectmatrix.stub.AbstractDenseObjectMatrix;
037    
038    public class RemoteObjectMatrixUDP extends AbstractDenseObjectMatrix {
039            private static final long serialVersionUID = 3889079475875267966L;
040    
041            private static final int BUFFERSIZE = 512;
042    
043            private static final int TIMEOUT = 1000;
044    
045            private DatagramPacket receivedPacket = null;
046    
047            private DatagramSocket socket = null;
048    
049            private SocketAddress destination = null;
050    
051            public RemoteObjectMatrixUDP(String server, int port) {
052                    try {
053                            socket = new DatagramSocket();
054                            socket.setSoTimeout(TIMEOUT);
055                            destination = new InetSocketAddress(server, port);
056                            receivedPacket = new DatagramPacket(new byte[BUFFERSIZE], BUFFERSIZE);
057                    } catch (Exception e) {
058                            throw new MatrixException("could not connnect to matrix", e);
059                    }
060            }
061    
062            public synchronized long[] getSize() {
063                    // TODO: not working
064                    try {
065                            ByteArrayOutputStream bos = new ByteArrayOutputStream(BUFFERSIZE);
066                            ObjectOutputStream oos = new ObjectOutputStream(bos);
067                            oos.writeInt(ServerObjectMatrixUDP.GETDIMENSIONCOUNT);
068                            // oos.writeInt(dimension);
069                            oos.flush();
070                            oos.close();
071                            DatagramPacket sentPacket = new DatagramPacket(bos.toByteArray(), bos.size(),
072                                            destination);
073                            socket.send(sentPacket);
074                            socket.receive(receivedPacket);
075                            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(receivedPacket
076                                            .getData()));
077                            int command = ois.readInt();
078                            if (command != ServerObjectMatrixUDP.GETDIMENSIONCOUNT) {
079                                    throw new MatrixException("could not set value");
080                            }
081                            ois.close();
082                            return null;
083                    } catch (Exception e) {
084                            throw new MatrixException("could not send packet", e);
085                    }
086            }
087    
088            public synchronized double getAsDouble(long... coordinates) {
089                    try {
090                            ByteArrayOutputStream bos = new ByteArrayOutputStream(BUFFERSIZE);
091                            ObjectOutputStream oos = new ObjectOutputStream(bos);
092                            oos.writeInt(ServerObjectMatrixUDP.GETDOUBLEVALUE);
093                            oos.writeObject(coordinates);
094                            oos.flush();
095                            oos.close();
096                            DatagramPacket sentPacket = new DatagramPacket(bos.toByteArray(), bos.size(),
097                                            destination);
098                            socket.send(sentPacket);
099                            socket.receive(receivedPacket);
100                            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(receivedPacket
101                                            .getData()));
102                            int command = ois.readInt();
103                            if (command != ServerObjectMatrixUDP.GETDOUBLEVALUE) {
104                                    throw new MatrixException("could not get value");
105                            }
106                            double result = ois.readDouble();
107                            ois.close();
108                            return result;
109                    } catch (Exception e) {
110                            throw new MatrixException("could not send packet", e);
111                    }
112            }
113    
114            public synchronized void setAsDouble(double value, long... coordinates) {
115                    try {
116                            ByteArrayOutputStream bos = new ByteArrayOutputStream(BUFFERSIZE);
117                            ObjectOutputStream oos = new ObjectOutputStream(bos);
118                            oos.writeInt(ServerObjectMatrixUDP.SETDOUBLEVALUE);
119                            oos.writeObject(coordinates);
120                            oos.writeDouble(value);
121                            oos.flush();
122                            oos.close();
123                            DatagramPacket sentPacket = new DatagramPacket(bos.toByteArray(), bos.size(),
124                                            destination);
125                            socket.send(sentPacket);
126                            socket.receive(receivedPacket);
127                            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(receivedPacket
128                                            .getData()));
129                            int command = ois.readInt();
130                            ois.close();
131                            if (command != ServerObjectMatrixUDP.SETDOUBLEVALUE) {
132                                    throw new MatrixException("could not set value");
133                            }
134                    } catch (Exception e) {
135                            throw new MatrixException("could not send packet", e);
136                    }
137            }
138    
139            public Object getObject(long... coordinates) {
140                    // TODO Auto-generated method stub
141                    return null;
142            }
143    
144            public void setObject(Object o, long... coordinates) {
145                    // TODO Auto-generated method stub
146    
147            }
148    
149    }