- Timestamp:
- 06/30/13 12:48:20 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/remote/RemoteInstance.java
r87 r88 9 9 import com.framsticks.core.Path; 10 10 import com.framsticks.params.*; 11 import com.framsticks.params.annotations.FramsClassAnnotation; 12 import com.framsticks.params.annotations.ParamAnnotation; 11 13 import com.framsticks.params.types.EventParam; 12 14 import com.framsticks.parsers.MultiParamLoader; … … 15 17 import com.framsticks.util.dispatching.Dispatching; 16 18 import com.framsticks.util.dispatching.Future; 19 import com.framsticks.util.dispatching.Joinable; 20 import com.framsticks.util.dispatching.JoinableParent; 21 import com.framsticks.util.dispatching.JoinableState; 17 22 import com.framsticks.util.lang.Casting; 18 23 import com.framsticks.util.lang.Pair; 19 24 import com.framsticks.util.dispatching.RunAt; 20 25 21 import org.apache.commons.configuration.Configuration; 26 import java.util.*; 27 22 28 import org.apache.log4j.Logger; 23 24 import java.util.*;25 29 26 30 /** 27 31 * @author Piotr Sniegowski 28 32 */ 29 public class RemoteInstance extends Instance { 33 @FramsClassAnnotation 34 public class RemoteInstance extends Instance implements JoinableParent { 30 35 31 36 private final static Logger log = Logger.getLogger(RemoteInstance.class.getName()); … … 45 50 } 46 51 47 @Override 48 public void run() { 49 assert isActive(); 50 super.run(); 51 connection.connect(new StateFunctor() { 52 public RemoteInstance() { 53 } 54 55 @ParamAnnotation 56 public void setAddress(String address) { 57 setConnection(new ClientConnection(address)); 58 } 59 60 @ParamAnnotation 61 public String getAddress() { 62 return connection == null ? "<disconnected>" : connection.getAddress(); 63 } 64 65 public void setConnection(final ClientConnection connection) { 66 this.connection = connection; 67 this.connection.setConnectedFunctor(new StateFunctor() { 52 68 @Override 53 69 public void call(Exception e) { … … 60 76 public void call(Exception e) { 61 77 if (e != null) { 62 log.fatal("unsupported protocol version!\n minimal version is: " 63 + "nmanager protocol is: " 64 + connection.getProtocolVersion()); 65 connection.close(); 78 log.fatal("unsupported protocol version!\n minimal version is: " + "\nmanager protocol is: " + connection.getProtocolVersion()); 79 Dispatching.drop(connection, RemoteInstance.this); 66 80 fireRun(e); 67 81 return; … … 112 126 } 113 127 }); 114 } 115 116 public RemoteInstance() { 117 } 118 119 120 @Override 121 public void configure(Configuration config) { 122 super.configure(config); 123 connection = new ClientConnection(config.getString("address")); 124 } 125 126 public void setAddress(String address) { 127 setConnection(new ClientConnection(address)); 128 } 129 130 public void setConnection(ClientConnection connection) { 131 this.connection = connection; 128 132 129 } 133 130 … … 135 132 public String toString() { 136 133 assert Dispatching.isThreadSafe(); 137 return getConnection().getAddress();134 return "remote instance " + getName() + "(" + getAddress() + ")"; 138 135 } 139 136 … … 167 164 } 168 165 }); 169 */166 */ 170 167 } 171 168 … … 175 172 } 176 173 177 public void disconnect() {178 assert isActive();179 if (connection.isConnected()) {180 connection.close();181 }182 }174 // public void disconnect() { 175 // assert isActive(); 176 // if (connection.isConnected()) { 177 // Dispatching.stop(connection, this); 178 // } 179 // } 183 180 184 181 public final ClientConnection getConnection() { … … 247 244 assert response.getFiles().size() == 1; 248 245 assert path.isTheSame(response.getFiles().get(0).getPath()); 249 FramsClass framsClass = processFetchedInfo(response.getFiles().get(0)); 250 251 if (framsClass == null) { 246 FramsClass framsClass; 247 try { 248 framsClass = processFetchedInfo(response.getFiles().get(0)); 249 } catch (ConstructionException e) { 252 250 log.fatal("could not read class info"); 253 251 finishInfoRequest(name, null, new Exception("could not read class info")); 254 252 return; 255 253 } 254 256 255 CompositeParam thisParam = path.getTop().getParam(); 257 256 if (!thisParam.isMatchingContainedName(framsClass.getId())) { … … 335 334 } 336 335 337 338 336 assert path.size() >= 2; 339 337 FramsClass underFramsClass = getInfoFromCache(path.getUnder().getParam().getContainedTypeName()); 340 338 341 EventParam changedEvent = underFramsClass.getParamEntry(path.getTop().getParam().getId() + "_changed", EventParam.class); 342 if (changedEvent == null) { 339 EventParam changedEvent; 340 try { 341 changedEvent = underFramsClass.getParamEntry(path.getTop().getParam().getId() + "_changed", EventParam.class); 342 } catch (FramsticksException e) { 343 343 return; 344 344 } 345 345 346 log.debug("registering for " + changedEvent); 346 347 if (getSubscription(path) != null) { 347 348 return; … … 394 395 } 395 396 396 397 397 protected void reactToChange(final Path path, final ListChange listChange) { 398 398 assert isActive(); … … 416 416 } 417 417 418 419 418 CompositeParam childParam = Casting.tryCast(CompositeParam.class, access.getParam(listChange.getBestIdentifier())); 420 419 assert childParam != null; 421 420 switch (listChange.getAction()) { 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 421 case Add: { 422 final String p = path.getTextual() + "/" + childParam.getId(); 423 resolveAndFetch(p, new Future<Path>() { 424 @Override 425 public void result(Path result, Exception e) { 426 if (e != null) { 427 log.error("failed to add " + p + ": " + e); 428 return; 429 } 430 log.debug("added: " + result); 431 fireListChange(path, listChange); 432 } 433 }); 434 break; 435 } 436 case Remove: { 437 access.set(childParam, null); 438 fireListChange(path, listChange); 439 break; 440 } 441 case Modify: { 442 final String p = path.getTextual() + "/" + childParam.getId(); 443 resolveAndFetch(p, new Future<Path>() { 444 @Override 445 public void result(Path result, Exception e) { 446 if (e != null) { 447 log.error("failed to modify " + p + ": " + e); 448 return; 449 } 450 fireListChange(path, listChange); 451 } 452 }); 453 break; 454 } 456 455 } 457 456 } … … 473 472 }); 474 473 } 474 475 @Override 476 protected void joinableStart() { 477 Dispatching.use(connection, this); 478 super.joinableStart(); 479 } 480 481 @Override 482 protected void joinableInterrupt() { 483 Dispatching.drop(connection, this); 484 super.joinableInterrupt(); 485 } 486 487 @Override 488 protected void joinableFinish() { 489 super.joinableFinish(); 490 491 } 492 493 @Override 494 public void joinableJoin() throws InterruptedException { 495 Dispatching.join(connection); 496 super.joinableJoin(); 497 } 498 499 500 @Override 501 public void childChangedState(Joinable joinable, JoinableState state) { 502 proceedToState(state); 503 } 504 505 475 506 }
Note: See TracChangeset
for help on using the changeset viewer.