PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/java/src/main/java/com/incesoft/botplatform/sdk/support/DefaultRobotServer.java

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