|
| 1 | +/* |
| 2 | + * $Id$ |
| 3 | + * |
| 4 | + * Copyright (c) 2016, Simsilica, LLC |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions |
| 9 | + * are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | + * notice, this list of conditions and the following disclaimer in |
| 16 | + * the documentation and/or other materials provided with the |
| 17 | + * distribution. |
| 18 | + * |
| 19 | + * 3. Neither the name of the copyright holder nor the names of its |
| 20 | + * contributors may be used to endorse or promote products derived |
| 21 | + * from this software without specific prior written permission. |
| 22 | + * |
| 23 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 26 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 27 | + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 28 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 30 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 32 | + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 34 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | + */ |
| 36 | + |
| 37 | +package example.net.client; |
| 38 | + |
| 39 | +import java.util.List; |
| 40 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 41 | + |
| 42 | +import org.slf4j.*; |
| 43 | + |
| 44 | +import com.jme3.network.service.AbstractClientService; |
| 45 | +import com.jme3.network.service.ClientServiceManager; |
| 46 | +import com.jme3.network.service.rmi.RmiClientService; |
| 47 | + |
| 48 | +import example.net.AccountSession; |
| 49 | +import example.net.AccountSessionListener; |
| 50 | + |
| 51 | +/** |
| 52 | + * Provides super-basic account services like logging in. This could |
| 53 | + * be expanded to be more complicated based on a real game's needs. |
| 54 | + * The basics have been included here as a minimal example that includes |
| 55 | + * the basic types of communication necessary. |
| 56 | + * |
| 57 | + * @author Paul Speed |
| 58 | + */ |
| 59 | +public class AccountClientService extends AbstractClientService |
| 60 | + implements AccountSession { |
| 61 | + |
| 62 | + static Logger log = LoggerFactory.getLogger(AccountClientService.class); |
| 63 | + |
| 64 | + private RmiClientService rmiService; |
| 65 | + private AccountSession delegate; |
| 66 | + |
| 67 | + private String playerName; |
| 68 | + |
| 69 | + private AccountSessionCallback sessionCallback = new AccountSessionCallback(); |
| 70 | + private List<AccountSessionListener> listeners = new CopyOnWriteArrayList<>(); |
| 71 | + |
| 72 | + public AccountClientService() { |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String getServerInfo() { |
| 77 | + return delegate.getServerInfo(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void login( String playerName ) { |
| 82 | + this.playerName = playerName; |
| 83 | + delegate.login(playerName); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Adds a listener that will be notified about account-related events. |
| 88 | + * Note that these listeners are called on the networking thread and |
| 89 | + * as such are not suitable for modifying the visualization directly. |
| 90 | + */ |
| 91 | + public void addAccountSessionListener( AccountSessionListener l ) { |
| 92 | + listeners.add(l); |
| 93 | + } |
| 94 | + |
| 95 | + public void removeAccountSessionListener( AccountSessionListener l ) { |
| 96 | + listeners.remove(l); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + protected void onInitialize( ClientServiceManager s ) { |
| 101 | + log.debug("onInitialize(" + s + ")"); |
| 102 | + this.rmiService = getService(RmiClientService.class); |
| 103 | + if( rmiService == null ) { |
| 104 | + throw new RuntimeException("AccountClientService requires RMI service"); |
| 105 | + } |
| 106 | + log.debug("Sharing session callback."); |
| 107 | + rmiService.share(sessionCallback, AccountSessionListener.class); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Called during connection setup once the server-side services have been initialized |
| 112 | + * for this connection and any shared objects, etc. should be available. |
| 113 | + */ |
| 114 | + @Override |
| 115 | + public void start() { |
| 116 | + log.debug("start()"); |
| 117 | + super.start(); |
| 118 | + this.delegate = rmiService.getRemoteObject(AccountSession.class); |
| 119 | + log.debug("delegate:" + delegate); |
| 120 | + if( delegate == null ) { |
| 121 | + throw new RuntimeException("No account session found during connection setup"); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Shared with the server over RMI so that it can notify us about account |
| 127 | + * related stuff. |
| 128 | + */ |
| 129 | + private class AccountSessionCallback implements AccountSessionListener { |
| 130 | + |
| 131 | + @Override |
| 132 | + public void notifyLoginStatus( boolean loggedIn ) { |
| 133 | + log.trace("notifyLoginStatus(" + loggedIn + ")"); |
| 134 | + for( AccountSessionListener l : listeners ) { |
| 135 | + l.notifyLoginStatus(loggedIn); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | + |
0 commit comments