Ignore:
Timestamp:
06/24/13 13:38:40 (11 years ago)
Author:
psniegowski
Message:

HIGHLIGHTS:

  • upgrade to Java 7
    • use try-multi-catch clauses
    • use try-with-resources were appropriate
  • configure FindBugs? (use mvn site and then navigate in browser to the report)
    • remove most bugs found
  • parametrize Dispatching environment (Dispatcher, RunAt?) to enforce more control on the place of closures actual call

CHANGELOG:
Rework FavouritesXMLFactory.

FindBugs?. Thread start.

FindBugs?. Minor change.

FindBugs?. Iterate over entrySet.

FindBugs?. Various.

FindBug?.

FindBug?. Encoding.

FindBug?. Final fields.

FindBug?.

Remove synchronization bug in ClientConnection?.

Experiments with findbugs.

Finish parametrization.

Make RunAt? an abstract class.

More changes in parametrization.

More changes in parametrizing dispatching.

Several changes to parametrize tasks.

Rename Runnable to RunAt?.

Add specific framsticks Runnable.

Add JSR305 (annotations).

Add findbugs reporting.

More improvements to ParamBuilder? wording.

Make FramsClass? accept also ParamBuilder?.

Change wording of ParamBuilder?.

Change wording of Request creation.

Use Java 7 exception catch syntax.

Add ScopeEnd? class.

Upgrade to Java 7.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • java/main/src/main/java/com/framsticks/communication/ClientConnection.java

    r84 r85  
    2121import java.util.regex.Matcher;
    2222import java.util.regex.Pattern;
     23import com.framsticks.util.dispatching.RunAt;
    2324
    2425/**
     
    2930        private final static Logger log = Logger.getLogger(ClientConnection.class);
    3031
    31         protected final Map<String, Subscription> subscriptions = new HashMap<String, Subscription>();
     32        protected final Map<String, Subscription<?>> subscriptions = new HashMap<>();
    3233
    3334        public String getAddress() {
     
    9394
    9495        private static class EventFire extends InboundMessage {
    95                 public final Subscription subscription;
    96 
    97                 private EventFire(Subscription subscription) {
     96                public final Subscription<?> subscription;
     97
     98                private EventFire(Subscription<?> subscription) {
    9899                        this.subscription = subscription;
    99100                }
     
    107108                public void eof() {
    108109                        finishCurrentFile();
    109                         Dispatching.invokeLaterOrNow(subscription.getDispatcher(), new Runnable() {
    110                                 @Override
    111                                 public void run() {
    112                                         subscription.getEventCallback().call(getFiles());
    113                                 }
    114                         });
    115                 }
    116         }
    117 
    118         private static class SentQuery extends InboundMessage {
     110
     111                        subscription.dispatchCall(getFiles());
     112                }
     113        }
     114
     115        private static class SentQuery<C> extends InboundMessage {
    119116                Request request;
    120                 ResponseCallback callback;
    121                 Dispatcher dispatcher;
     117                ResponseCallback<? extends C> callback;
     118                Dispatcher<C> dispatcher;
    122119
    123120                public void startFile(String path) {
     
    139136                        return request.toString();
    140137                }
    141         }
    142         private Map<Integer, SentQuery> queryMap = new HashMap<Integer, SentQuery>();
     138
     139                public void dispatchResponseProcess(final Response response) {
     140                        Dispatching.invokeLaterOrNow(dispatcher, new RunAt<C>() {
     141                                @Override
     142                                public void run() {
     143                                        callback.process(response);
     144                                }
     145                        });
     146                }
     147        }
     148        private Map<Integer, SentQuery<?>> queryMap = new HashMap<>();
    143149
    144150
     
    163169        }
    164170
    165         private SentQuery currentlySentQuery;
    166 
    167         public void send(Request request, ResponseCallback callback) {
    168                 send(request, AtOnceDispatcher.instance, callback);
    169         }
    170 
    171         public void send(Request request, Dispatcher dispatcher, ResponseCallback callback) {
     171        private SentQuery<?> currentlySentQuery;
     172
     173
     174        public <C extends Connection> void send(Request request, ResponseCallback<C> callback) {
     175                //TODO RunAt
     176                send(request, AtOnceDispatcher.getInstance(), callback);
     177        }
     178
     179        public <C> void send(Request request, Dispatcher<C> dispatcher, ResponseCallback<? extends C> callback) {
    172180
    173181                if (!isConnected()) {
     
    175183                        return;
    176184                }
    177                 final SentQuery sentQuery = new SentQuery();
     185                final SentQuery<C> sentQuery = new SentQuery<C>();
    178186                sentQuery.request = request;
    179187                sentQuery.callback = callback;
    180188                sentQuery.dispatcher = dispatcher;
    181189
    182                 senderThread.invokeLater(new Runnable(){
     190                senderThread.invokeLater(new RunAt<Connection>(){
    183191                        @Override
    184192                        public void run() {
     193                                Integer id;
    185194                                synchronized (ClientConnection.this) {
    186195
     
    192201                                                }
    193202                                        }
    194                                 }
    195                                 Integer id = stashQuery(sentQuery);
     203                                        if (requestIdEnabled) {
     204                                                queryMap.put(nextQueryId, sentQuery);
     205                                                id = nextQueryId++;
     206                                        } else {
     207                                                currentlySentQuery = sentQuery;
     208                                                id = null;
     209                                        }
     210                                }
    196211                                String command = sentQuery.request.getCommand();
    197212                                StringBuilder message = new StringBuilder();
     
    223238        }
    224239
    225         public void subscribe(final String path, final SubscriptionCallback callback) {
    226                 send(new RegistrationRequest().setPath(path), new ResponseCallback() {
     240        public <C> void subscribe(final String path, final Dispatcher<C> dispatcher, final SubscriptionCallback<? extends C> callback) {
     241                send(new RegistrationRequest().path(path), new ResponseCallback<Connection>() {
    227242                        @Override
    228243                        public void process(Response response) {
     
    233248                                }
    234249                                assert response.getFiles().isEmpty();
    235                                 Subscription subscription = new Subscription(ClientConnection.this, path, response.getComment());
     250                                Subscription<C> subscription = new Subscription<C>(ClientConnection.this, path, response.getComment(), dispatcher);
    236251                                log.debug("registered on event: " + subscription);
    237252                                synchronized (subscriptions) {
     
    241256                                if (subscription.getEventCallback() == null) {
    242257                                        log.info("subscription for " + path + " aborted");
    243                                         subscription.unsubscribe(new LoggingStateCallback(log, "abort subscription"));
     258                                        subscription.unsubscribe(new LoggingStateCallback<C>(log, "abort subscription"));
    244259                                }
    245260                        }
     
    253268
    254269        public void sendQueryVersion(final int version, final StateFunctor stateFunctor) {
    255                 send(new VersionRequest().version(version), new StateCallback() {
     270                send(new VersionRequest().version(version), new StateCallback<Connection>() {
    256271                        @Override
    257272                        public void call(Exception e) {
     
    266281                                        return;
    267282                                }
    268                                 send(new UseRequest().feature("request_id"), new StateCallback() {
     283                                send(new UseRequest().feature("request_id"), new StateCallback<Connection>() {
    269284                                        @Override
    270285                                        public void call(Exception e) {
     
    289304
    290305
    291         private synchronized SentQuery fetchQuery(Integer id, boolean remove) {
     306        private synchronized SentQuery<?> fetchQuery(Integer id, boolean remove) {
    292307                if (id == null) {
    293308                        if (requestIdEnabled) {
    294309                                return null;
    295310                        }
    296                         SentQuery result = currentlySentQuery;
     311                        SentQuery<?> result = currentlySentQuery;
    297312                        if (remove) {
    298313                                currentlySentQuery = null;
     
    302317                }
    303318                if (queryMap.containsKey(id)) {
    304                         SentQuery result = queryMap.get(id);
     319                        SentQuery<?> result = queryMap.get(id);
    305320                        if (remove) {
    306321                                queryMap.remove(id);
     
    312327
    313328        private int nextQueryId = 0;
    314 
    315         private Integer stashQuery(SentQuery sentQuery) {
    316                 if (!requestIdEnabled) {
    317                         currentlySentQuery = sentQuery;
    318                         return null;
    319                 }
    320                 queryMap.put(nextQueryId, sentQuery);
    321                 return nextQueryId++;
    322         }
    323329
    324330        protected void processMessage(InboundMessage inboundMessage) throws Exception {
     
    342348                        return;
    343349                }
    344                 Subscription subscription = subscriptions.get(matcher.group(1));
     350                Subscription<?> subscription = subscriptions.get(matcher.group(1));
    345351                if (subscription == null) {
    346352                        log.error("non subscribed event: " + matcher.group(1));
     
    362368
    363369                if (command.first.equals("file")) {
    364                         SentQuery sentQuery = fetchQuery(rest.first, false);
     370                        SentQuery<?> sentQuery = fetchQuery(rest.first, false);
    365371                        sentQuery.startFile(rest.second);
    366372                        processMessage(sentQuery);
     
    368374                }
    369375
    370                 SentQuery sentQuery = fetchQuery(rest.first, true);
     376                SentQuery<?> sentQuery = fetchQuery(rest.first, true);
    371377                if (sentQuery == null) {
    372378                        return;
     
    374380                log.debug("parsing response for request " + sentQuery);
    375381
    376                 final Response response = new Response(command.first.equals("ok"), rest.second, sentQuery.getFiles());
    377                 final ResponseCallback callback = sentQuery.callback;
    378 
    379                 Dispatching.invokeLaterOrNow(sentQuery.dispatcher, new Runnable() {
    380                         @Override
    381                         public void run() {
    382                                 callback.process(response);
    383                         }
    384                 });
     382                sentQuery.dispatchResponseProcess(new Response(command.first.equals("ok"), rest.second, sentQuery.getFiles()));
    385383        }
    386384
Note: See TracChangeset for help on using the changeset viewer.