001/*
002 * Copyright 2006 - 2013
003 *     Stefan Balev     <stefan.balev@graphstream-project.org>
004 *     Julien Baudry    <julien.baudry@graphstream-project.org>
005 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
006 *     Yoann Pigné      <yoann.pigne@graphstream-project.org>
007 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
008 * 
009 * This file is part of GraphStream <http://graphstream-project.org>.
010 * 
011 * GraphStream is a library whose purpose is to handle static or dynamic
012 * graph, create them from scratch, file or any source and display them.
013 * 
014 * This program is free software distributed under the terms of two licenses, the
015 * CeCILL-C license that fits European law, and the GNU Lesser General Public
016 * License. You can  use, modify and/ or redistribute the software under the terms
017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
019 * the Free Software Foundation, either version 3 of the License, or (at your
020 * option) any later version.
021 * 
022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
024 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Lesser General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * 
029 * The fact that you are presently reading this means that you have had
030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
031 */
032package org.graphstream.stream.rmi;
033
034import java.rmi.Naming;
035import java.rmi.RemoteException;
036import java.rmi.server.UnicastRemoteObject;
037import java.util.concurrent.ConcurrentHashMap;
038
039import org.graphstream.stream.Sink;
040
041public class RMISink extends UnicastRemoteObject implements RMIAdapterOut, Sink {
042        private static final long serialVersionUID = 23444722897331612L;
043
044        ConcurrentHashMap<String, RMIAdapterIn> inputs;
045
046        public RMISink() throws RemoteException {
047                super();
048                inputs = new ConcurrentHashMap<String, RMIAdapterIn>();
049        }
050
051        public RMISink(String name) throws RemoteException {
052                this();
053                bind(name);
054        }
055
056        public void bind(String name) {
057                try {
058                        Naming.rebind(String.format("//localhost/%s", name), this);
059                } catch (Exception e) {
060                        e.printStackTrace();
061                }
062        }
063
064        public void register(String url) throws RemoteException {
065                try {
066                        RMIAdapterIn in = (RMIAdapterIn) Naming.lookup(url);
067
068                        if (in != null)
069                                inputs.put(url, in);
070                } catch (Exception e) {
071                        e.printStackTrace();
072                }
073        }
074
075        public void unregister(String url) throws RemoteException {
076                if (inputs.containsKey(url))
077                        inputs.remove(url);
078        }
079
080        public void edgeAttributeAdded(String graphId, long timeId, String edgeId,
081                        String attribute, Object value) {
082                for (RMIAdapterIn in : inputs.values()) {
083                        try {
084                                in.edgeAttributeAdded(graphId, timeId, edgeId, attribute, value);
085                        } catch (Exception e) {
086                                e.printStackTrace();
087                        }
088                }
089        }
090
091        public void edgeAttributeChanged(String graphId, long timeId,
092                        String edgeId, String attribute, Object oldValue, Object newValue) {
093                for (RMIAdapterIn in : inputs.values()) {
094                        try {
095                                in.edgeAttributeChanged(graphId, timeId, edgeId, attribute,
096                                                oldValue, newValue);
097                        } catch (Exception e) {
098                                e.printStackTrace();
099                        }
100                }
101        }
102
103        public void edgeAttributeRemoved(String graphId, long timeId,
104                        String edgeId, String attribute) {
105                for (RMIAdapterIn in : inputs.values()) {
106                        try {
107                                in.edgeAttributeRemoved(graphId, timeId, edgeId, attribute);
108                        } catch (Exception e) {
109                                e.printStackTrace();
110                        }
111                }
112        }
113
114        public void graphAttributeAdded(String graphId, long timeId,
115                        String attribute, Object value) {
116                for (RMIAdapterIn in : inputs.values()) {
117                        try {
118                                in.graphAttributeAdded(graphId, timeId, attribute, value);
119                        } catch (Exception e) {
120                                e.printStackTrace();
121                        }
122                }
123        }
124
125        public void graphAttributeChanged(String graphId, long timeId,
126                        String attribute, Object oldValue, Object newValue) {
127                for (RMIAdapterIn in : inputs.values()) {
128                        try {
129                                in.graphAttributeChanged(graphId, timeId, attribute, oldValue,
130                                                newValue);
131                        } catch (Exception e) {
132                                e.printStackTrace();
133                        }
134                }
135        }
136
137        public void graphAttributeRemoved(String graphId, long timeId,
138                        String attribute) {
139                for (RMIAdapterIn in : inputs.values()) {
140                        try {
141                                in.graphAttributeRemoved(graphId, timeId, attribute);
142                        } catch (Exception e) {
143                                e.printStackTrace();
144                        }
145                }
146        }
147
148        public void nodeAttributeAdded(String graphId, long timeId, String nodeId,
149                        String attribute, Object value) {
150                for (RMIAdapterIn in : inputs.values()) {
151                        try {
152                                in.nodeAttributeAdded(graphId, timeId, nodeId, attribute, value);
153                        } catch (Exception e) {
154                                e.printStackTrace();
155                        }
156                }
157        }
158
159        public void nodeAttributeChanged(String graphId, long timeId,
160                        String nodeId, String attribute, Object oldValue, Object newValue) {
161                for (RMIAdapterIn in : inputs.values()) {
162                        try {
163                                in.nodeAttributeChanged(graphId, timeId, nodeId, attribute,
164                                                oldValue, newValue);
165                        } catch (Exception e) {
166                                e.printStackTrace();
167                        }
168                }
169        }
170
171        public void nodeAttributeRemoved(String graphId, long timeId,
172                        String nodeId, String attribute) {
173                for (RMIAdapterIn in : inputs.values()) {
174                        try {
175                                in.nodeAttributeRemoved(graphId, timeId, nodeId, attribute);
176                        } catch (Exception e) {
177                                e.printStackTrace();
178                        }
179                }
180        }
181
182        public void edgeAdded(String graphId, long timeId, String edgeId,
183                        String fromNodeId, String toNodeId, boolean directed) {
184                for (RMIAdapterIn in : inputs.values()) {
185                        try {
186                                in.edgeAdded(graphId, timeId, edgeId, fromNodeId, toNodeId,
187                                                directed);
188                        } catch (Exception e) {
189                                e.printStackTrace();
190                        }
191                }
192        }
193
194        public void edgeRemoved(String graphId, long timeId, String edgeId) {
195                for (RMIAdapterIn in : inputs.values()) {
196                        try {
197                                in.edgeRemoved(graphId, timeId, edgeId);
198                        } catch (Exception e) {
199                                e.printStackTrace();
200                        }
201                }
202        }
203
204        public void graphCleared(String graphId, long timeId) {
205                for (RMIAdapterIn in : inputs.values()) {
206                        try {
207                                in.graphCleared(graphId, timeId);
208                        } catch (Exception e) {
209                                e.printStackTrace();
210                        }
211                }
212        }
213
214        public void nodeAdded(String graphId, long timeId, String nodeId) {
215                for (RMIAdapterIn in : inputs.values()) {
216                        try {
217                                in.nodeAdded(graphId, timeId, nodeId);
218                        } catch (Exception e) {
219                                e.printStackTrace();
220                        }
221                }
222        }
223
224        public void nodeRemoved(String graphId, long timeId, String nodeId) {
225                for (RMIAdapterIn in : inputs.values()) {
226                        try {
227                                in.nodeRemoved(graphId, timeId, nodeId);
228                        } catch (Exception e) {
229                                e.printStackTrace();
230                        }
231                }
232        }
233
234        public void stepBegins(String graphId, long timeId, double step) {
235                for (RMIAdapterIn in : inputs.values()) {
236                        try {
237                                in.stepBegins(graphId, timeId, step);
238                        } catch (Exception e) {
239                                e.printStackTrace();
240                        }
241                }
242        }
243}