public void transition(StateEvent anEvent) { Assert.assertTrue( anEvent.getResult().equals(StateEvent.Reason.NEW_LEADER) || anEvent.getResult().equals(StateEvent.Reason.VALUE)); synchronized (this) { ++_readyCount; } }
public void handleUpdate(StateEvent state) { switch (state.getStatus()) { case ConnectionManagerListener.CONNECTED: // statusLine.setSchemaName(state.getUrl().getUserHostPortServiceName()); break; case ConnectionManagerListener.DISCONNECTED: // statusLine.setSchemaName("Not logged in"); break; } }
public void handleStateChange(StateEvent event) { AimConnection conn = event.getAimConnection(); if (aimSession == null || conn != aimSession.getConnection()) { return; } State state = event.getNewState(); if (state == State.FAILED) { if (AIMAccount.this.loginListener != null) { AIMAccount.this.loginListener.loginDidFailedWithError( "Failed to connect to '" + AIMAccount.this.getUserName() + "' account"); AIMAccount.this.loginListener = null; } } if (state == State.ONLINE) { if (AIMAccount.this.loginListener != null) { AIMAccount.this.loginListener.loginDidSucceeded(); AIMAccount.this.loginListener = null; } isOnline = true; IcbmService icbmservice = conn.getIcbmService(); icbmservice.addIcbmListener( new IcbmListener() { public void buddyInfoUpdated( IcbmService service, Screenname buddy, IcbmBuddyInfo info) {} public void newConversation(IcbmService service, Conversation conv) { conv.addConversationListener( new ConversationListener() { public void sentOtherEvent( Conversation conversation, ConversationEventInfo event) { // TODO Auto-generated method stub } public void sentMessage(Conversation c, MessageInfo minfo) { // TODO Auto-generated method stub } public void gotOtherEvent( Conversation conversation, ConversationEventInfo event) { // TODO Auto-generated method stub } public void gotMessage(Conversation c, MessageInfo minfo) { Friend friend = getFriendFromBuddy(minfo.getFrom(), false); HashMap<String, Friend> friends = AccountManager.getInstance().getFriends(); Friend existingFriend = friends.get(friend.getUniqueDescriptior()); existingFriend.setUserInfo(c); Spanned messageText = Html.fromHtml(minfo.getMessage().getMessageBody()); FriendMessage friendMessage = new FriendMessage( existingFriend.getUniqueDescriptior(), AIMAccount.this.getUserName(), messageText.toString(), false); accountListener.didReceiveMessageForFriend(friendMessage, existingFriend); } public void conversationOpened(Conversation c) { // TODO Auto-generated method stub } public void conversationClosed(Conversation c) { Friend friend = getFriendFromBuddy(c.getBuddy(), false); HashMap<String, Friend> friends = AccountManager.getInstance().getFriends(); Friend existingFriend = friends.get(friend.getUniqueDescriptior()); existingFriend.setUserInfo(null); } public void canSendMessageChanged(Conversation c, boolean canSend) { // TODO Auto-generated method stub } }); } public void sendAutomaticallyFailed( IcbmService service, Message message, Set<Conversation> triedConversations) { // TODO Auto-generated method stub } }); connection .getBuddyInfoManager() .addGlobalBuddyInfoListener( new GlobalBuddyInfoListener() { @Override public void receivedStatusUpdate( BuddyInfoManager manager, Screenname buddy, BuddyInfo info) {} @Override public void newBuddyInfo( BuddyInfoManager manager, Screenname buddy, BuddyInfo info) { Friend friend = getFriendFromBuddy(buddy, false); HashMap<String, Friend> friends = AccountManager.getInstance().getFriends(); Friend existingFriend = friends.get(friend.getUniqueDescriptior()); if (existingFriend != null) { ByteBlock iconData = info.getIconData(); if (iconData != null) { Bitmap bitmap = BitmapFactory.decodeByteArray( iconData.toByteArray(), 0, iconData.getLength()); if (bitmap != null) { existingFriend.setAvatar(bitmap); } } applyInfoToFriendFromBuddyInfo(existingFriend, info); accountListener.friendStatusDidChange(existingFriend); } } @Override public void buddyInfoChanged( BuddyInfoManager manager, Screenname buddy, BuddyInfo info, PropertyChangeEvent event) { Friend friend = getFriendFromBuddy(buddy, false); HashMap<String, Friend> friends = AccountManager.getInstance().getFriends(); Friend existingFriend = friends.get(friend.getUniqueDescriptior()); if (existingFriend != null) { ByteBlock iconData = info.getIconData(); if (iconData != null) { Bitmap bitmap = BitmapFactory.decodeByteArray( iconData.toByteArray(), 0, iconData.getLength()); if (bitmap != null) { existingFriend.setAvatar(bitmap); } } applyInfoToFriendFromBuddyInfo(existingFriend, info); accountListener.friendStatusDidChange(existingFriend); } } }); connection .getInfoService() .addInfoListener( new InfoServiceListener() { @Override public void handleUserProfile( InfoService service, Screenname buddy, String userInfo) { Friend friend = getFriendFromBuddy(buddy, false); HashMap<String, Friend> friends = AccountManager.getInstance().getFriends(); Friend existingFriend = friends.get(friend.getUniqueDescriptior()); existingFriend.setName(userInfo); } @Override public void handleInvalidCertificates( InfoService service, Screenname buddy, CertificateInfo origCertInfo, Throwable ex) {} @Override public void handleDirectoryInfo( InfoService service, Screenname buddy, DirInfo dirInfo) { Friend friend = getFriendFromBuddy(buddy, false); HashMap<String, Friend> friends = AccountManager.getInstance().getFriends(); Friend existingFriend = friends.get(friend.getUniqueDescriptior()); if (dirInfo != null) { String firstName = dirInfo.getFirstname(); String nick = dirInfo.getNickname(); String lastName = dirInfo.getLastname(); String fullName = firstName != null ? firstName : "" + lastName != null ? lastName : ""; if (fullName != null && fullName.length() > 0) { existingFriend.setName(fullName); } else if (nick != null && nick.length() > 0) { existingFriend.setName(nick); } } } @Override public void handleCertificateInfo( InfoService service, Screenname buddy, BuddyCertificateInfo certInfo) {} @Override public void handleAwayMessage( InfoService service, Screenname buddy, String awayMessage) {} }); conn.getChatRoomManager() .addListener( new ChatRoomManagerListener() { public void handleInvitation( ChatRoomManager chatRoomManager, ChatInvitation ourInvitation) {} }); BuddyService buddyService = conn.getBuddyService(); buddyService.addBuddyListener( new BuddyServiceListener() { public void buddyOffline(BuddyService service, Screenname buddy) { synchronized (AIMAccount.this) { Friend friend = getFriendFromBuddy(buddy, false); friend.setIsAvailable(false); HashMap<String, Friend> friends = AccountManager.getInstance().getFriends(); Friend existingFriend = friends.get(friend.getUniqueDescriptior()); if (existingFriend == null) { existingFriend = friend; accountListener.didAddFriend(existingFriend); } else { accountListener.friendStatusDidChange(existingFriend); } } } public void gotBuddyStatus( BuddyService service, Screenname buddy, FullUserInfo info) { synchronized (AIMAccount.this) { Friend friend = getFriendFromBuddy(buddy, false); HashMap<String, Friend> friends = AccountManager.getInstance().getFriends(); Friend existingFriend = friends.get(friend.getUniqueDescriptior()); if (existingFriend == null) { existingFriend = friend; accountListener.didAddFriend(existingFriend); } applyInfoToFriend(existingFriend, info); accountListener.friendStatusDidChange(existingFriend); } } }); } else { disconnect(); } }
/** * Advances the ODE as usual, except if an event takes place. Then it finds the event point and * applies the actions * * @return The actual step taken */ public double step() { // Step from t=a to t=b(=a+dt) errorCode = ODEAdaptiveSolver.NO_ERROR; eventHappened = false; double t = 0, origDt = solver.getStepSize(); do { triggerOde.readRealState(); // Prepare the faked ODE System.arraycopy(triggerOde.getState(), 0, statea, 0, size); // Set // statea // values at b double dt = solver.step(); double[] state = triggerOde.getState(); // Find which events have happened happened.clear(); for (Enumeration<StateEvent> e = eventList.elements(); e.hasMoreElements(); ) { StateEvent evt = e.nextElement(); if (evt.evaluate(state) <= -evt.getTolerance()) { happened.add(evt); // This event actually happened! } } // Check for no event if (happened.size() == 0) { triggerOde.updateRealState(); solver.setStepSize(origDt); return dt; } eventHappened = true; // System.out.println ("An event!"); // else System.out.println // ("N of events = "+happened.size()+" First is = "+happened.elementAt(0)); /* * This is the moment of truth! We need to find the precise instant * of time for the first event */ // Go back to statea triggerOde.setState(statea); // Check first for a itself // This is to make sure that when two events happen at the same // time they will be found at the exact same instant. // This is important for accuracy of results and better performance. StateEvent eventFound = null; for (StateEvent evt : happened) { if (Math.abs(evt.evaluate(statea)) < evt.getTolerance()) { // Found // at // a // itself eventFound = evt; // System.out.println("Found at a = " + state[state.length - // 1]); break; // No need to continue } } if (eventFound == null) { // Now find by subdivision // This synchronizes our triggerOde state with the state of the // ODEInterpolatorSolver if (solver instanceof ODEInterpolationSolver) solver.initialize(solver.getStepSize()); for (int i = 0; i < MAX; i++) { // Start the subdivision // System.out.println ("Subdividing i = "+i+ // " t = "+state[state.length-1]); solver.setStepSize(dt *= 0.5); // Take half the step double c = solver.step(); state = triggerOde.getState(); StateEvent previousFound = null; for (StateEvent evt : happened) { double f_i = evt.evaluate(state); if (f_i <= -evt.getTolerance()) { previousFound = evt; break; } if (f_i < evt.getTolerance()) { eventFound = evt; // Do not break in case there is a // previous one } } if (previousFound != null) { /* * Eliminate events that may come later (This is not so * necessary) */ for (StateEvent evt : happened) { if (evt != previousFound && (evt.evaluate(state) > -evt.getTolerance())) { happened.remove(evt); } } triggerOde.setState(statea); // go back to a // This synchronizes our triggerOde state with the state // of the ODEInterpolatorSolver if (solver instanceof ODEInterpolationSolver) solver.initialize(solver.getStepSize()); } else { // Advance to new position t = t + c; System.arraycopy(state, 0, statea, 0, size); if (eventFound != null) { // We found it! // System.out.println // ("Found at "+state[state.length-1]); break; } } } // End of the subdivision scheme // The event is any of those which remain in the list of // happened if (eventFound == null) { // If this happens, the event is most // likely poorly designed! eventFound = (StateEvent) happened.elementAt(0); System.err.println( "BisectionEventSolver Warning : Event not found after " + MAX + " subdivisions!!!"); System.err.println(" Event = " + eventFound); System.err.println( " Please check your event algorithm or decrease the initial stepTime."); errorCode = ODEAdaptiveSolver.BISECTION_EVENT_NOT_FOUND; } } // System.out.println ("We are at time = "+state[state.length-1]); // Update real ODE triggerOde.updateRealState(); if (eventFound.action()) { if (solver instanceof ODEInterpolationSolver) { triggerOde.readRealState(); solver.initialize(origDt); } else solver.setStepSize(origDt); return t; } // System.out.println("t = " + t); if (solver instanceof ODEInterpolationSolver) { triggerOde.readRealState(); solver.initialize(origDt - t); } else solver.setStepSize(origDt - t); } while (t < origDt); solver.setStepSize(origDt); return t; }