/java/src/main/java/com/incesoft/botplatform/sdk/support/DefaultRobotServer.java
Java | 827 lines | 716 code | 108 blank | 3 comment | 213 complexity | a54a98050b60e3ad3a95baf25263a4c5 MD5 | raw file
1package com.incesoft.botplatform.sdk.support; 2 3import java.io.IOException; 4import java.lang.reflect.InvocationTargetException; 5import java.lang.reflect.Method; 6import java.util.ArrayList; 7import java.util.HashMap; 8import java.util.List; 9import java.util.concurrent.ConcurrentHashMap; 10import java.util.concurrent.ScheduledExecutorService; 11import java.util.concurrent.ScheduledFuture; 12import java.util.concurrent.TimeUnit; 13 14import org.apache.commons.codec.digest.DigestUtils; 15import org.apache.commons.logging.Log; 16import org.apache.commons.logging.LogFactory; 17 18import sun.misc.BASE64Decoder; 19 20import com.incesoft.botplatform.sdk.RobotConnectionListener; 21import com.incesoft.botplatform.sdk.RobotException; 22import com.incesoft.botplatform.sdk.RobotHandler; 23import com.incesoft.botplatform.sdk.RobotMessage; 24import com.incesoft.botplatform.sdk.RobotResource; 25import com.incesoft.botplatform.sdk.RobotServer; 26import com.incesoft.botplatform.sdk.RobotSession; 27import com.incesoft.botplatform.sdk.RobotUser; 28import com.incesoft.botplatform.sdk.protocol.msg.AppMessage; 29import com.incesoft.botplatform.sdk.protocol.msg.ErrorMessage; 30import com.incesoft.botplatform.sdk.protocol.msg.FileTransEvent; 31import com.incesoft.botplatform.sdk.protocol.msg.LoginRequest; 32import com.incesoft.botplatform.sdk.protocol.msg.LoginResponse; 33import com.incesoft.botplatform.sdk.protocol.msg.Message; 34import com.incesoft.botplatform.sdk.protocol.msg.P2PEvent; 35import com.incesoft.botplatform.sdk.protocol.msg.ResourceInfo; 36import com.incesoft.botplatform.sdk.protocol.msg.SessionOpenedEvent; 37import com.incesoft.botplatform.sdk.protocol.msg.TextMessage; 38import com.incesoft.botplatform.sdk.protocol.msg.UpdateRobotRequest; 39import com.incesoft.botplatform.sdk.protocol.msg.User; 40 41/** 42 * @author LiBo 43 */ 44public class DefaultRobotServer extends RobotConnection implements RobotServer { 45 46 protected static Log log = LogFactory.getLog(DefaultRobotServer.class); 47 48 public static final int KEEP_ALIVE_TIMEOUT = 60000; 49 public static final long DEFAULT_LOGIN_TIMEOUT = 60000; 50 public static final String VERSION = "java-3.1"; 51 52 protected String host; 53 protected int port; 54 protected boolean loggedIn; 55 protected boolean reconnectedSupport = true; 56 57 protected ScheduledExecutorService scheduledExecutor = null; 58 @SuppressWarnings("unchecked") 59 protected ScheduledFuture scheduledFuture = null; 60 protected int lifeStatus = 3; 61 62 protected List<RobotConnectionListener> connectionListeners = null; 63 protected RobotHandler handler = null; 64 protected HashMap<String, Method> processors = new HashMap<String, Method>(); 65 protected ConcurrentHashMap<String, DefaultRobotSession> sessions = new ConcurrentHashMap<String, DefaultRobotSession>(); 66 67 protected LoginStatus loginStatus = new LoginStatus(); 68 protected int loginState = 0; 69 70 protected String SPID; 71 protected String SPPWD; 72 protected long timeout; 73 74 public DefaultRobotServer() { 75 Method[] methods = this.getClass().getMethods(); 76 for (Method method : methods) { 77 String name = method.getName(); 78 if (name.startsWith("process_")) { 79 processors.put(name.substring(8), method); 80 } 81 } 82 } 83 84 public void process_loginresp(String robotId, String userId, 85 String sessionId, LoginResponse resp) throws Exception { 86 if (resp.getStatus() == LoginResponse.STATUS_OK) { 87 loggedIn = true; 88 loginStatus.setStatus(LoginStatus.OK); 89 } else if (resp.getStatus() == LoginResponse.STATUS_TOKEN_REQUIRED) { 90 String data = resp.getChallenge() + getSPID() + resp.getConnId() 91 + getSPPWD(); 92 String token = DigestUtils.md5Hex(data); 93 init(token); 94 } else { 95 loggedIn = false; 96 loginStatus.setStatus(LoginStatus.FAILED); 97 } 98 } 99 100 public void process_redirect(String robotId, String userId, 101 String sessionId, String[] addrlist) throws Exception { 102 for (String addr : addrlist) { 103 int i = addr.indexOf(':'); 104 host = addr.substring(0, i); 105 port = Integer.parseInt(addr.substring(i + 1)); 106 loginStatus.setStatus(LoginStatus.REDIRECT); 107 break; // select the first one 108 } 109 } 110 111 public void process_userupdated(String robotId, String userId, 112 String sessionId, RobotUser user) throws Exception { 113 if (handler != null) 114 handler.userUpdated(robotId, user); 115 } 116 117 public void process_useradded(String robotId, String userId, 118 String sessionId, Object obj) throws Exception { 119 if (handler != null) 120 handler.userAdd(robotId, userId); 121 } 122 123 public void process_userremoved(String robotId, String userId, 124 String sessionId, Object obj) throws Exception { 125 if (handler != null) 126 handler.userRemove(robotId, userId); 127 } 128 129 public void process_userremoved(String robotId, String userId, 130 String sessionId, String psm) throws Exception { 131 if (handler != null) 132 handler.personalMessageUpdated(robotId, userId, psm); 133 } 134 135 public void process_userlist(String robotId, String userId, 136 String sessionId, User[] userlist) throws Exception { 137 if (handler != null && userlist != null) { 138 List<RobotUser> rulist = new ArrayList<RobotUser>(); 139 for (User u : userlist) { 140 rulist.add(u); 141 } 142 handler.contactListReceived(robotId, rulist); 143 } 144 } 145 146 public void process_sessionopened(String robotId, String userId, 147 String sessionId, SessionOpenedEvent event) throws Exception { 148 DefaultRobotSession session = new DefaultRobotSession( 149 DefaultRobotServer.this); 150 session.setRobot(robotId); 151 session.addUser(event.getUser()); 152 session.setSessionID(sessionId); 153 session.setOpenMode(event.getMode()); 154 sessions.put(sessionId, session); 155 if (handler != null) 156 handler.sessionOpened(session); 157 } 158 159 public void process_sessionclosed(String robotId, String userId, 160 String sessionId, Object obj) throws Exception { 161 DefaultRobotSession session = sessions.remove(sessionId); 162 if (session != null) { 163 session.setClosed(true); 164 if (handler != null){ 165 handler.sessionClosed(session); 166 } 167 } 168 } 169 170 public void process_join(String robotId, String userId, String sessionId, 171 User user) throws Exception { 172 DefaultRobotSession session = sessions.get(sessionId); 173 if (session != null) { 174 session.addUser(user); 175 if (handler != null) 176 handler.userJoined(session, user); 177 } 178 } 179 180 public void process_part(String robotId, String userId, String sessionId, 181 Object obj) throws Exception { 182 DefaultRobotSession session = sessions.get(sessionId); 183 if (session != null) { 184 session.removeUser(userId); 185 if (handler != null) 186 handler.userLeft(session, session.getUser(userId)); 187 } 188 } 189 190 public void process_msg(String robotId, String userId, String sessionId, 191 TextMessage msg) throws Exception { 192 DefaultRobotSession session = sessions.get(sessionId); 193 if (session != null) { 194 if (handler != null) 195 handler.messageReceived(session, new DefaultRobotMessage(msg)); 196 } 197 } 198 199 public void process_nudge(String robotId, String userId, String sessionId, 200 Object obj) throws Exception { 201 DefaultRobotSession session = sessions.get(sessionId); 202 if (session != null) { 203 if (handler != null) 204 handler.nudgeReceived(session); 205 } 206 } 207 208 public void process_appmsg(String robotId, String userId, String sessionId, 209 AppMessage app) throws Exception { 210 DefaultRobotSession session = sessions.get(sessionId); 211 if (session != null) { 212 if (handler != null) 213 handler.activityReceived(session, app.getData()); 214 } 215 } 216 217 public void process_appevent(String robotId, String userId, 218 String sessionId, String event) throws Exception { 219 DefaultRobotSession session = sessions.get(sessionId); 220 if (session != null) { 221 if (handler != null) { 222 if (P2PEvent.ACCEPT.equals(event)){ 223 session.setActivityOpened(true); 224 handler.activityAccepted(session); 225 } 226 else if (P2PEvent.REJECT.equals(event)) 227 handler.activityRejected(session); 228 else if (P2PEvent.READY.equals(event)) 229 handler.activityLoaded(session); 230 else if (P2PEvent.CLOSE.equals(event)){ 231 session.setActivityOpened(false); 232 handler.activityClosed(session); 233 } 234 } 235 } 236 } 237 238 public void process_fileevent(String robotId, String userId, 239 String sessionId, FileTransEvent eventObj) throws Exception { 240 DefaultRobotSession session = sessions.get(sessionId); 241 if (session != null) { 242 if (handler != null) { 243 String event = eventObj.getEvent(); 244 if (P2PEvent.ACCEPT.equals(event)) 245 handler.fileAccepted(session,eventObj); 246 else if (P2PEvent.REJECT.equals(event)) 247 handler.fileRejected(session,eventObj); 248 else if (P2PEvent.INVITE.equals(event)) 249 handler.fileInvited(session,eventObj); 250 else if (P2PEvent.CLOSE.equals(event)) 251 handler.fileTransferEnded(session,eventObj); 252 else if (P2PEvent.ERROR.equals(event)) 253 handler.fileTransferError(session,eventObj); 254 else if (P2PEvent.CANCEL.equals(event)) 255 handler.fileTransferCancelled(session,eventObj); 256 else if(P2PEvent.RECEIVE.equals(event)) 257 handler.fileReceived(robotId, userId, eventObj, eventObj.getSaveUrl()); 258 } 259 } 260 } 261 262 public void process_fileinvite(String robotId, String userId, 263 String sessionId, FileTransEvent invitation) throws RobotException { 264 DefaultRobotSession session = sessions.get(sessionId); 265 if (session != null && handler != null) { 266 handler.fileInvited(session, invitation); 267 } 268 } 269 270 public void process_webcamevent(String robotId, String userId, 271 String sessionId, String event) throws Exception { 272 DefaultRobotSession session = sessions.get(sessionId); 273 if (session != null) { 274 if (handler != null) { 275 if (P2PEvent.ACCEPT.equals(event)) 276 handler.webcamAccepted(session); 277 else if (P2PEvent.REJECT.equals(event)) 278 handler.webcamRejected(session); 279 } 280 } 281 } 282 283 public void process_error(String robotId, String userId, String sessionId, 284 ErrorMessage err) throws Exception { 285 if (log.isErrorEnabled()) 286 log.error("process_error:\r\n\terrorCode: " + err.getCode() 287 + "\r\n\terrorMessage: " + err.getMessage()); 288 289 RobotException exception = new RobotException(err.getCode(), err 290 .getMessage()); 291 loginStatus.setException(exception); 292 if (handler != null) { 293 RobotSession session = (RobotSession) sessions.get(sessionId); 294 handler.exceptionCaught(session, exception); 295 } 296 } 297 298 public void process_typing(String robotId, String userId, String sessionId, 299 Object msgBoby) throws RobotException { 300 DefaultRobotSession session = sessions.get(sessionId); 301 if (handler != null && session != null) 302 handler.typingReceived(session); 303 } 304 305 public void process_voiceclipevent(String robotId, String userId, 306 String sessionId, ResourceInfo resource) throws RobotException { 307 DefaultRobotSession session = sessions.get(sessionId); 308 if (handler != null && session != null) 309 handler.voiceclipReceived(session, resource); 310 } 311 312 public void process_winkevent(String robotId, String userId, 313 String sessionId, ResourceInfo resource) throws RobotException { 314 DefaultRobotSession session = sessions.get(sessionId); 315 if (handler != null && session != null) 316 handler.winkReceived(session, resource); 317 } 318 319 public void process_inkmsg(String robotId, String userId, String sessionId, 320 String ink) throws RobotException { 321 DefaultRobotSession session = sessions.get(sessionId); 322 if (handler != null && session != null){ 323 byte[] inkData = null; 324 try { 325 inkData = new BASE64Decoder().decodeBuffer(ink); 326 } catch (IOException e) {} 327 handler.inkReceived(session, inkData); 328 } 329 } 330 331 public void process_dpupdated(String robotId, String userId, 332 String sessionId, ResourceInfo resource) throws RobotException { 333 if (handler != null) 334 handler.displayPictureUpdated(robotId, userId, resource); 335 } 336 337 public void process_sceneupdated(String robotId, String userId, 338 String sessionId, ResourceInfo resource) throws RobotException { 339 if (handler != null) 340 handler.sceneUpdated(robotId, userId, resource); 341 } 342 343 public void process_colorupdated(String robotId, String userId, 344 String sessionId, int colorScheme) throws RobotException { 345 if (handler != null) 346 handler.colorSchemeUpdated(robotId, userId, colorScheme); 347 } 348 349 public void process_psmupdated(String robotId, String userId, 350 String sessionId, String psm) throws RobotException { 351 if (handler != null) 352 handler.personalMessageUpdated(robotId, userId, psm); 353 } 354 355 public void process_resource(String robotId, String userId, 356 String sessionId, ResourceInfo resource) { 357 if (handler != null) 358 handler.resourceReceived(robotId, userId, resource, resource.getSaveUrl()); 359 } 360 361 public void login(String spid, String sppwd) throws RobotException { 362 login(spid, sppwd, DEFAULT_LOGIN_TIMEOUT); 363 } 364 365 public void login(String spid, String sppwd, long timeout) 366 throws RobotException { 367 368 this.SPID = spid; 369 this.SPPWD = sppwd; 370 this.timeout = timeout; 371 372 loginImpl(true); 373 } 374 375 protected void loginImpl(boolean syn) throws RobotException { 376 try { 377 open(host, port); 378 if (syn) { 379 int status = loginStatus.getStatus(timeout); 380 if (status == LoginStatus.OK) { 381 if (isReconnectedSupport()) { 382 synchronized (this) { 383 if (scheduledFuture == null) { 384 scheduledFuture = scheduledExecutor 385 .scheduleAtFixedRate( 386 new KeepAliveTask(), 387 KEEP_ALIVE_TIMEOUT, 388 KEEP_ALIVE_TIMEOUT, 389 TimeUnit.MILLISECONDS); 390 } 391 } 392 } 393 } else { 394 synchronized (this) { 395 if (session != null) { 396 session.close(); 397 session = null; 398 } 399 } 400 if (status == LoginStatus.REDIRECT) { 401 loginStatus = new LoginStatus(); 402 loginState++; 403 loginImpl(true); 404 } else if (status == LoginStatus.TIMEOUT) { 405 throw new RobotException("login timeout."); 406 } else { 407 RobotException exception = loginStatus.getException(); 408 if (exception != null) 409 throw exception; 410 else 411 throw new RobotException("login faild."); 412 } 413 } 414 } 415 } catch (Exception e) { 416 throw new RobotException(e.getMessage(), e); 417 } finally { 418 loginStatus = new LoginStatus(); 419 loginState = 0; 420 } 421 } 422 423 public synchronized void logout() { 424 if (scheduledFuture != null) { 425 scheduledFuture.cancel(true); 426 scheduledFuture = null; 427 } 428 logoutImpl(); 429 430 } 431 432 public void logoutImpl() { 433 if (session != null) 434 session.close(); 435 436 } 437 438 protected void init(String token) throws Exception { 439 440 LoginRequest req = new LoginRequest(); 441 req.setSpid(SPID); 442 req.setVersion(VERSION); 443 req.setToken(token); 444 req.setState(loginState); 445 sendMessage(new Message(Message.LOGIN, req)); 446 } 447 448 public void assertAlive() { 449 lifeStatus = 3; 450 } 451 452 public void setDisplayName(String displayName) throws RobotException { 453 if (!this.isLoggedIn()) 454 throw new RobotException("robot server not logged in."); 455 if (displayName == null) 456 throw new NullPointerException(); 457 UpdateRobotRequest req = new UpdateRobotRequest(); 458 req.setDisplayName(displayName); 459 sendMessage(new Message(Message.UPDATEROBOT, req)); 460 } 461 462 public void setDisplayName(String robotAccount, String displayName) 463 throws RobotException { 464 if (!this.isLoggedIn()) 465 throw new RobotException("robot server not logged in."); 466 if (displayName == null) 467 throw new NullPointerException(); 468 UpdateRobotRequest req = new UpdateRobotRequest(); 469 req.setDisplayName(displayName); 470 sendMessage(new Message(robotAccount, null, null, Message.UPDATEROBOT, 471 req)); 472 } 473 474 public void setPersonalMessage(String personalMessage) 475 throws RobotException { 476 if (!this.isLoggedIn()) 477 throw new RobotException("robot server not logged in."); 478 if (personalMessage == null) 479 throw new NullPointerException(); 480 UpdateRobotRequest req = new UpdateRobotRequest(); 481 req.setPersonalMessage(personalMessage); 482 sendMessage(new Message(Message.UPDATEROBOT, req)); 483 } 484 485 public void setPersonalMessage(String robotAccount, String personalMessage) 486 throws RobotException { 487 if (!this.isLoggedIn()) 488 throw new RobotException("robot server not logged in."); 489 if (personalMessage == null) 490 throw new NullPointerException(); 491 UpdateRobotRequest req = new UpdateRobotRequest(); 492 req.setPersonalMessage(personalMessage); 493 sendMessage(new Message(robotAccount, null, null, Message.UPDATEROBOT, 494 req)); 495 } 496 497 public void setDisplayPicture(String displayPicture) throws RobotException { 498 setDisplayPictureEx(displayPicture, null); 499 } 500 501 public void setDisplayPicture(String robotAccount, String displayPicture) 502 throws RobotException { 503 setDisplayPictureEx(robotAccount, displayPicture, null); 504 } 505 506 public void createSession(String robot, String user) throws RobotException { 507 if (!this.isLoggedIn()) 508 throw new RobotException("robot server not logged in."); 509 if (robot == null || user == null) 510 throw new NullPointerException(); 511 sendMessage(new Message(robot, user, null, Message.CREATESESSION, null)); 512 } 513 514 public void pushMessage(String robot, String user, RobotMessage message) 515 throws RobotException { 516 if (!this.isLoggedIn()) 517 throw new RobotException("robot server not logged in."); 518 if (robot == null || user == null || message == null) 519 throw new NullPointerException(); 520 DefaultRobotMessage msg = (DefaultRobotMessage) message; 521 sendMessage(new Message(robot, user, null, Message.PUSH, msg.getProtocolMessage())); 522 } 523 524 public void requestContactList(String robot) throws RobotException { 525 if (!this.isLoggedIn()) 526 throw new RobotException("robot server not loggedin."); 527 if (robot == null) 528 throw new NullPointerException(); 529 sendMessage(new Message(robot, null, null, Message.GETUSERLIST, null)); 530 } 531 532 public void requestResource(String robot, String user, RobotResource resource, String saveUrl) throws RobotException { 533 if (!this.isLoggedIn()) 534 throw new RobotException("robot server not loggedin."); 535 if (robot == null || user == null || resource == null) 536 throw new NullPointerException(); 537 ResourceInfo info = new ResourceInfo(); 538 info.setDigest(resource.getDigest()); 539 info.setName(resource.getName()); 540 info.setSize(resource.getSize()); 541 info.setSaveUrl(saveUrl); 542 sendMessage(new Message(robot, user, null, Message.GETRESOURCE, info)); 543 } 544 545 @Override 546 protected void processOpened() throws Exception { 547 synchronized (this) { 548 init(null); 549 if (scheduledFuture == null) 550 this.fireServerConnected(); 551 else 552 this.fireServerReconnected(); 553 } 554 } 555 556 @Override 557 protected void processClosed() throws Exception { 558 synchronized (this) { 559 this.setLoggedIn(false); 560 this.fireServerDisconnected(); 561 } 562 } 563 564 @Override 565 protected void processError(Throwable e) throws RobotException { 566 if(log.isErrorEnabled()) 567 log.error("",e); 568 if (handler != null) { 569 handler.exceptionCaught(null, e); 570 } 571 } 572 573 @Override 574 protected void processMessage(Message msg) throws Exception { 575 assertAlive(); 576 Method method = processors.get(msg.getType()); 577 if (method != null) { 578 try { 579 method.invoke(this, new Object[] { msg.getRobotId(), 580 msg.getUserId(), msg.getSessionId(), msg.getBody() }); 581 } catch (InvocationTargetException e) { 582 if (handler != null) 583 handler.exceptionCaught(null, e.getTargetException()); 584 } 585 } 586 } 587 588 @Override 589 protected void processSent() throws Exception { 590 assertAlive(); 591 } 592 593 private static class LoginStatus { 594 public static int TIMEOUT = 0; 595 public static int OK = 1; 596 public static int REDIRECT = 2; 597 public static int FAILED = 3; 598 599 private int status = TIMEOUT; 600 private RobotException exception = null; 601 602 public synchronized int getStatus(long timeout) { 603 try { 604 this.wait(timeout); 605 } catch (InterruptedException e) { 606 e.printStackTrace(); 607 } 608 return status; 609 } 610 611 public synchronized void setStatus(int status) { 612 this.status = status; 613 this.notifyAll(); 614 } 615 616 public RobotException getException() { 617 return exception; 618 } 619 620 public synchronized void setException(RobotException e) { 621 this.status = FAILED; 622 this.exception = e; 623 this.notifyAll(); 624 } 625 } 626 627 private class KeepAliveTask implements Runnable { 628 629 public void run() { 630 try { 631 if (lifeStatus <= 0) { 632 DefaultRobotServer.this.logoutImpl(); 633 DefaultRobotServer.this.loginImpl(true); 634 } else { 635 lifeStatus--; 636 keepAlive(); 637 } 638 } catch (Exception e) { 639 if (log.isErrorEnabled()) 640 log.error(e.getMessage(), e); 641 } 642 } 643 644 } 645 646 public String getSPID() { 647 return SPID; 648 } 649 650 public synchronized void setReconnectedSupport(boolean b) { 651 this.reconnectedSupport = b; 652 if (isLoggedIn()) { 653 if (b) { 654 if (scheduledFuture == null) { 655 scheduledFuture = scheduledExecutor.scheduleAtFixedRate( 656 new KeepAliveTask(), KEEP_ALIVE_TIMEOUT, 657 KEEP_ALIVE_TIMEOUT, TimeUnit.MILLISECONDS); 658 } 659 } else { 660 if (scheduledFuture != null) { 661 scheduledFuture.cancel(true); 662 scheduledFuture = null; 663 } 664 } 665 } 666 } 667 668 public boolean isReconnectedSupport() { 669 return reconnectedSupport; 670 } 671 672 public void addConnectionListener(RobotConnectionListener listener) { 673 if (connectionListeners == null) 674 connectionListeners = new ArrayList<RobotConnectionListener>(); 675 connectionListeners.add(listener); 676 } 677 678 public void removeConnectionistener(RobotConnectionListener listener) { 679 if (connectionListeners != null) 680 connectionListeners.remove(listener); 681 } 682 683 public void fireServerConnected() { 684 if (connectionListeners != null) { 685 for (RobotConnectionListener listener : connectionListeners) { 686 listener.serverConnected(this); 687 } 688 } 689 } 690 691 public void fireServerReconnected() { 692 if (connectionListeners != null) { 693 for (RobotConnectionListener listener : connectionListeners) { 694 listener.serverReconnected(this); 695 } 696 } 697 } 698 699 public void fireServerDisconnected() { 700 if (connectionListeners != null) { 701 for (RobotConnectionListener listener : connectionListeners) { 702 listener.serverDisconnected(this); 703 } 704 } 705 } 706 707 public void setSPID(String spid) { 708 SPID = spid; 709 } 710 711 public String getSPPWD() { 712 return SPPWD; 713 } 714 715 public void setSPPWD(String sppwd) { 716 SPPWD = sppwd; 717 } 718 719 public boolean isLoggedIn() { 720 return loggedIn; 721 } 722 723 public void setLoggedIn(boolean loggedIn) { 724 this.loggedIn = loggedIn; 725 } 726 727 public void setRobotHandler(RobotHandler handler) { 728 this.handler = handler; 729 } 730 731 public RobotHandler getRobotHandler() { 732 return handler; 733 } 734 735 public String getHost() { 736 return host; 737 } 738 739 public void setHost(String host) { 740 this.host = host; 741 } 742 743 public int getPort() { 744 return port; 745 } 746 747 public void setPort(int port) { 748 this.port = port; 749 } 750 751 public ScheduledExecutorService getScheduledExecutor() { 752 return scheduledExecutor; 753 } 754 755 public void setScheduledExecutor(ScheduledExecutorService scheduledExecutor) { 756 this.scheduledExecutor = scheduledExecutor; 757 } 758 759 public void setColorScheme(int colorScheme) throws RobotException { 760 if (!this.isLoggedIn()) 761 throw new RobotException("robot server not logged in."); 762 UpdateRobotRequest req = new UpdateRobotRequest(); 763 req.setColorScheme(colorScheme); 764 sendMessage(new Message(null, null, null, Message.UPDATEROBOT, req)); 765 } 766 767 public void setColorScheme(String robot, int colorScheme) 768 throws RobotException { 769 if (!this.isLoggedIn()) 770 throw new RobotException("robot server not logged in."); 771 UpdateRobotRequest req = new UpdateRobotRequest(); 772 req.setColorScheme(colorScheme); 773 sendMessage(new Message(null, null, null, Message.UPDATEROBOT, req)); 774 } 775 776 public void setScene(String scene) throws RobotException { 777 if (!this.isLoggedIn()) 778 throw new RobotException("robot server not logged in."); 779 if (scene == null) 780 throw new NullPointerException(); 781 UpdateRobotRequest req = new UpdateRobotRequest(); 782 req.setScene(scene); 783 sendMessage(new Message(null, null, null, Message.UPDATEROBOT, req)); 784 } 785 786 public void setScene(String robotAccount, String scene) 787 throws RobotException { 788 if (!this.isLoggedIn()) 789 throw new RobotException("robot server not logged in."); 790 if (scene == null) 791 throw new NullPointerException(); 792 UpdateRobotRequest req = new UpdateRobotRequest(); 793 req.setScene(scene); 794 sendMessage(new Message(robotAccount, null, null, Message.UPDATEROBOT, 795 req)); 796 } 797 798 public void setDisplayPictureEx(String displayPicture, String deluxePicture) 799 throws RobotException { 800 if (!this.isLoggedIn()) 801 throw new RobotException("robot server not logged in."); 802 UpdateRobotRequest req = new UpdateRobotRequest(); 803 req.setDisplayPicture(displayPicture); 804 req.setLargePicture(deluxePicture); 805 sendMessage(new Message(Message.UPDATEROBOT, req)); 806 } 807 808 public void setDisplayPictureEx(String robotAccount, String displayPicture, 809 String deluxePicture) throws RobotException { 810 if (!this.isLoggedIn()) 811 throw new RobotException("robot server not logged in."); 812 UpdateRobotRequest req = new UpdateRobotRequest(); 813 req.setDisplayPicture(displayPicture); 814 req.setLargePicture(deluxePicture); 815 sendMessage(new Message(robotAccount, null, null, Message.UPDATEROBOT, 816 req)); 817 } 818 819 public RobotMessage createMessage() { 820 return new DefaultRobotMessage(); 821 } 822 823 public void addUser(String robot, String user, String inviteMessage) { 824 sendMessage(new Message(robot, user, null, Message.ADDUSER, inviteMessage)); 825 } 826 827}