source: java/main/src/main/java/com/framsticks/hosting/Server.java @ 98

Last change on this file since 98 was 98, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

CHANGELOG:
Get data also on tree expansion.

Use nice framstick icon for empty nodes.

Update panel after reload if it is current.

Add shallow reload procedure.

Cut Gui prefix from several tree classes.

Bring back counter of GuiTreeNode?.

Use IdentityHashMap? were it is more appriopriate.

Remove TreeListener?.

Do not use TreeListener? in GUI.

Minor change.

Done migration to GuiTreeModel?.

BrowserTest? in that version always crashes frams.linux.

Move rendering implementation into GuiAbstractNode?.

Use hand-crafted list in GuiTreeNode?.

Generally, it would be a great place for WeakIdentityHashMap?
(but there is none in Java Collection Framework).

Remove superfluous logging.

Fix bug in GuiTreeNode?.

Use IdentityHashMap? instead of HashMap?.

Improve structure update.

Filter out invalid uids in UniqueListAccess?.

Improve TreeCellRenderer?.

Add filtering in TrackConsole?.

Improve TreeModel?.

More changes.

More improvements.

More changes.

Remove TreeNode?.

Support MetaNode? in the GuiTreeModel?.

Implement more in GuiTreeModel?.

Add CompositeParam? interface to FramsClass? and AccessInterface?.

Allow access by number to UniqueList?.

Add UidComparator?.

Use TreeMap? as a default accessee in unique list.

It keeps order of keys.

Introduce classes to use with new TreeModel?.

Another step.

Migrate from TreeNode? to Node in many places.

Remove some uses of TreeNode? as DefaultMutableTreeNode?.

Remove Path from TreeNode? interface.

Remove Path from TreeNode?.

Add Path recration from node feature.

Reworking TreeCellRenderer?.

Minor change of TreeOperations? interface.

Remove last methods from TreeNode?.

Another minor step.

Do not store reference to TreeAtFrame? in TreeNode?.

Add proxy exceptionHandler to StatusBar?.

Move panels management to TreeAtFrame?.

Store localChanges in the NodeAtFrame?.

More cleanup.

Move name computing to TreeCellRenderer?.

Move tooltip and icon computations to TreeCellRenderer?.

More dispatches removed.

Remove most dispatching from TreeNode?.

TreeNode? does not actually redispatch tasks.

Make Tree embedded in Browser use SwingDispatcher?.

Make lazy binding of Tree with Dispatcher.

Minor changes.

Organizational change in AbstractTree?.

Make AbstractTree? compose from Thread instead of inherit from it.

Make SwingDispatcher? and AtOnceDispatcher? Joinable compatible.

Add ListPanelProvider?.

Improve Controls readonly and enabled handling.

Properly pass ExceptionHandlers? in more places.

Make Tree.get accept ValueParam?.

  • This is to allow access number of list elements.

Remove not needed get redirection in ClientAtServer?.

Rename tryResolve to tryGet.

Unify tryResolveAndGet into tryResolve.

Remove resolveTop from Tree interface.

Make Tree.get accept Future<Path>.

Use get to implement resolveTop also in ObjectTree?.

Unify resolveTop and get in RemoteTree?.

Another minor step.

More minor changes in tree operations.

Minor organizational changes.

In RemoteTree? first fetch info for root.

Reworking resolving.

Minor changes.

Make ListAccess? return proxy iterators (instead of creating temporary collection).

Let AccessInterface? return Iterable<Param>.

Improve resolving.

More improvements.

First working completion in ManagedConsole?.

Rename resolve to resolveTop.

This reflects the actuall functionality.

Change semantic of tryResolve and tryResolveAndGet.

File size: 5.6 KB
Line 
1package com.framsticks.hosting;
2
3import org.apache.log4j.Level;
4import org.apache.log4j.Logger;
5
6import com.framsticks.core.Tree;
7import com.framsticks.params.annotations.AutoAppendAnnotation;
8import com.framsticks.params.annotations.FramsClassAnnotation;
9import com.framsticks.params.annotations.ParamAnnotation;
10import com.framsticks.util.FramsticksException;
11import com.framsticks.util.dispatching.AbstractJoinable;
12import com.framsticks.util.dispatching.Dispatching;
13import com.framsticks.util.dispatching.Joinable;
14import com.framsticks.util.dispatching.JoinableCollection;
15import com.framsticks.util.dispatching.JoinableParent;
16import com.framsticks.util.dispatching.JoinableState;
17import com.framsticks.util.dispatching.RunAt;
18import com.framsticks.util.dispatching.Task;
19import com.framsticks.util.dispatching.ThrowExceptionHandler;
20
21import java.io.IOException;
22import java.net.InetSocketAddress;
23import java.net.ServerSocket;
24import java.net.Socket;
25import com.framsticks.util.dispatching.Thread;
26
27@FramsClassAnnotation
28public class Server extends AbstractJoinable implements JoinableParent {
29
30        private final static Logger log = Logger.getLogger(Server.class);
31
32        protected int port;
33
34        protected ServerSocket acceptSocket;
35        protected Tree hosted;
36        protected final JoinableCollection<ClientAtServer> clients = new JoinableCollection<ClientAtServer>();
37
38        public static class Accept {
39        };
40
41        protected Thread<Accept> acceptThread = new Thread<>();
42
43        /**
44         *
45         */
46        public Server() {
47                log.debug("created server");
48                port = 9009;
49        }
50
51        /**
52         * @return the port
53         */
54        @ParamAnnotation
55        public int getPort() {
56                return port;
57        }
58
59        /**
60         * @param port the port to set
61         */
62        @ParamAnnotation
63        public void setPort(int port) {
64                this.port = port;
65        }
66
67        @Override
68        protected void joinableInterrupt() {
69                Dispatching.drop(acceptThread, this);
70                Dispatching.drop(hosted, this);
71                Dispatching.drop(clients, this);
72                try {
73                        acceptSocket.close();
74                } catch (IOException e) {
75                        log.debug("exception caught during socket closing: ", e);
76                }
77                finish();
78        }
79
80        /**
81         * @return the hosted
82         */
83        public Tree getHosted() {
84                return hosted;
85        }
86
87        @AutoAppendAnnotation
88        public void setHosted(Tree hosted) {
89                if (this.hosted != null) {
90                        throw new FramsticksException().msg("hosted tree is already set").arg("current", this.hosted);
91                }
92                this.hosted = hosted;
93                acceptThread.setName(hosted.getName() + " acceptor");
94                clients.setObservableName(hosted.getName() + " clients");
95        }
96
97        @Override
98        public void childChangedState(Joinable joinable, JoinableState state) {
99                proceedToState(state);
100        }
101
102        @Override
103        protected void joinableStart() {
104                Dispatching.use(acceptThread, this);
105                Dispatching.use(hosted, this);
106                Dispatching.use(clients, this);
107                try {
108                        acceptSocket = new ServerSocket();
109                } catch (IOException e) {
110                        throw new FramsticksException().msg("failed to create server socket").cause(e);
111                }
112                tryBind(0);
113        }
114
115        @Override
116        @ParamAnnotation
117        public String getName() {
118                return hosted != null ? hosted.getName() : "server";
119        }
120
121        protected void acceptNext() {
122                if (!isRunning()) {
123                        log.debug("server is not in running state, aborting accepting");
124                        return;
125                }
126                acceptThread.dispatch(new RunAt<Accept>(hosted) {
127                        @Override
128                        protected void runAt() {
129                                try {
130                                        log.debug("accepting");
131                                        final Socket socket = acceptSocket.accept();
132                                        assert socket != null;
133                                        log.debug("accepted socket: " + socket.getInetAddress().getHostAddress());
134                                        hosted.dispatch(new RunAt<Tree>(this) {
135                                                @Override
136                                                protected void runAt() {
137                                                        ClientAtServer client = new ClientAtServer(Server.this, socket);
138                                                        clients.add(client);
139                                                        log.info("client connected: " + client);
140                                                }
141                                        });
142                                } catch (IOException e) {
143                                        log.log((isRunning() ? Level.ERROR : Level.DEBUG), "failed to accept socket: " + e);
144                                }
145                                acceptNext();
146                        }
147                });
148        }
149
150        protected void tryBind(int when) {
151                acceptThread.dispatch(new Task<Accept>(ThrowExceptionHandler.getInstance(), when) {
152                        @Override
153                        protected void runAt() {
154                                try {
155                                        acceptSocket.bind(new InetSocketAddress(port));
156                                        log.debug("started accepting on port " + port);
157                                        acceptNext();
158                                        return;
159                                } catch (IOException e) {
160                                        log.warn("failed to accept on port " + port + " (repeating): " + e);
161                                }
162                                tryBind(1000);
163                        }
164                });
165        }
166
167        @Override
168        protected void joinableFinish() {
169
170        }
171
172        @Override
173        protected void joinableJoin() throws InterruptedException {
174                Dispatching.join(acceptThread);
175                Dispatching.join(hosted);
176                Dispatching.join(clients);
177        }
178
179        // @Override
180        // public FramsClass getInfoFromCache(String id) {
181        //      assert isActive();
182        //      if (id == null) {
183        //              return null;
184        //      }
185        //      FramsClass cached = registry.getFramsClass(id);
186        //      if (cached != null) {
187        //              return cached;
188        //      }
189        //      try {
190        //              Class<?> nativeClass = Class.forName(id);
191        //              FramsClass framsClass = FramsClass.build().forClass(nativeClass);
192
193        //              if (!framsClass.getId().equals(id)) {
194        //                      log.error("no matching id");
195        //                      return null;
196        //              }
197
198        //              registry.registerReflectedClass(null, id, nativeClass);
199        //              registry.putFramsClass(framsClass);
200        //              return framsClass;
201        //      } catch (ClassNotFoundException ignored) {
202        //      } catch (ConstructionException e) {
203        //              log.error("failed to use info from cache: " + e);
204        //      }
205
206        //      return null;
207        // }
208
209        // @Override
210        // protected void fetchInfo(Path path, Future<FramsClass> future) {
211        //      assert isActive();
212
213        //      FramsClass framsClass = getInfoFromCache(path.getTop().getObject().getClass().getCanonicalName());
214        //      if (framsClass == null) {
215        //              log.error("failed to create frams class for: " + path.getTop().getObject().getClass());
216        //              future.result(null, new Exception());
217        //              return;
218        //      }
219        //      future.result(framsClass, null);
220        // }
221}
Note: See TracBrowser for help on using the repository browser.