source: java/main/src/main/java/com/framsticks/core/LocalInstance.java @ 88

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

HIGHLIGHTS:

  • loading f0 schema with XmlLoader?
  • use XmlLoader? to load configuration
  • introduce unified fork-join model of various entities

(Instances, Connections, GUI Frames, etc.),
all those entities clean up gracefully on
shutdown, which may be initialized by user
or by some entity

  • basing on above, simplify several organizing classes

(Observer, main class)

(to host native frams server process from Java level)

CHANGELOG:
Remove redundant Observer class.

Clean up in AbstractJoinable?.

Update ExternalProcess? class to changes in joining model.

Another sweep through code with FindBugs?.

Find bug with not joining RemoteInstance?.

Joining almost works.

Much improved joining model.

More improvement to joining model.

Add logging messages around joinable operations.

Rename methods in AbstractJoinable?.

Improve Joinable.

Rewrite of entity structure.

More simplifications with entities.

Further improve joinables.

Let Frame compose from JFrame instead of inheriting.

Add join classes.

Improvements of closing.

Add Builder interface.

Add FramsServerTest?.xml

FramsServer? may be configured through xml.

Make Framsticks main class an Observer of Entities.

Make Observer a generic type.

Remove variables regarding to removed endpoint.

Simplify observer (remove endpoints).

More changes to Observer and Endpoint.

Minor improvements.

Add OutputListener? to ExternalProcess?.

Improve testing of ExternalProcess?.

Add ExternalProcess? runner.

Rename the Program class to Framsticks.

Migrate Program to use XmlLoader? configuration.

First steps with configuration using XmlLoader?.

Fix several bugs.

Move all f0 classes to apriopriate package.

XmlLoader? is able to load Schema.

XmlLoader? is loading classes and props.

Add GroupBuilder?.

File size: 2.3 KB
Line 
1package com.framsticks.core;
2
3import com.framsticks.hosting.InstanceClient;
4import com.framsticks.util.dispatching.Thread;
5
6import org.apache.log4j.Logger;
7
8import java.io.IOException;
9import java.net.InetSocketAddress;
10import java.net.ServerSocket;
11import java.net.Socket;
12import java.util.HashSet;
13import java.util.Set;
14import com.framsticks.util.dispatching.RunAt;
15
16
17/**
18 * @author Piotr Sniegowski
19 */
20public abstract class LocalInstance extends Instance {
21
22        private static final Logger log = Logger.getLogger(LocalInstance.class.getName());
23
24        public LocalInstance() {
25
26        }
27
28        // @Override
29        // public void configure(Configuration config) {
30        //      Integer accept = config.getInteger("accept", null);
31        //      if (accept != null) {
32        //              try {
33        //                      acceptSocket = new ServerSocket();
34        //                      acceptSocket.setReuseAddress(true);
35        //                      acceptThread = new Thread<Accept>().setName(name + "-accept").start();
36        //                      tryBind(accept);
37        //              } catch (IOException e) {
38        //                      log.fatal("failed to create accept socket: " + e);
39        //              }
40        //      }
41        // }
42
43        protected final Set<InstanceClient> clients = new HashSet<InstanceClient>();
44        ServerSocket acceptSocket;
45
46        public static class Accept {
47        };
48
49        Thread<Accept> acceptThread;
50
51        protected void acceptNext() {
52                acceptThread.invokeLater(new RunAt<Accept>() {
53                        @Override
54                        public void run() {
55                                try {
56                                        final Socket socket = acceptSocket.accept();
57                                        assert socket != null;
58                                        log.debug("accepted socket: " + socket.getInetAddress().getHostAddress());
59                                        invokeLater(new RunAt<LocalInstance>() {
60                                                @Override
61                                                public void run() {
62                                                        InstanceClient client = new InstanceClient(LocalInstance.this, socket);
63                                                        clients.add(client);
64                                                        log.info("client connected: " + client);
65                                                }
66                                        });
67                                } catch (IOException e) {
68                                        log.error("failed to accept socket: " + e);
69                                }
70                                acceptNext();
71                        }
72                });
73        }
74
75        public void tryBind(final Integer accept) {
76                acceptThread.invokeLater(new RunAt<Accept>() {
77                        @Override
78                        public void run() {
79                                try {
80                                        acceptSocket.bind(new InetSocketAddress(accept));
81                                        log.debug("started accepting on port " + accept);
82                                        acceptNext();
83                                } catch (IOException e) {
84                                        log.fatal("failed to accept on port " + accept + ": " + e);
85                                }
86                                try {
87                                        java.lang.Thread.sleep(1000);
88                                } catch (InterruptedException ignored) {
89                                }
90                                tryBind(accept);
91                        }
92                });
93        }
94
95}
Note: See TracBrowser for help on using the repository browser.