001package jmri.jmrix.loconet.locormi; 002 003import jmri.SystemConnectionMemo; 004import jmri.jmrix.loconet.LnCommandStationType; 005import jmri.jmrix.loconet.LnTrafficRouter; 006import jmri.jmrix.loconet.LocoNetException; 007import jmri.jmrix.loconet.LocoNetMessage; 008import jmri.jmrix.loconet.LocoNetSystemConnectionMemo; 009import org.slf4j.Logger; 010import org.slf4j.LoggerFactory; 011 012/** 013 * Client for the RMI LocoNet server. 014 * <p> 015 * The main() in this class is for test purposes only. 016 * 017 * <hr> 018 * This file is part of JMRI. 019 * <p> 020 * JMRI is free software; you can redistribute it and/or modify it under the 021 * terms of version 2 of the GNU General Public License as published by the Free 022 * Software Foundation. See the "COPYING" file for a copy of this license. 023 * <p> 024 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY 025 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 026 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 027 * 028 * @author Alex Shepherd Copyright (c) 2002 029 * @author Bob Jacobsen 030 */ 031public class LnMessageClient extends LnTrafficRouter { 032 033 String serverName = null; 034 int pollTimeout; 035 LnMessageServerInterface lnServer = null; 036 LnMessageBufferInterface lnMessageBuffer = null; 037 LnMessageClientPollThread pollThread = null; 038 039 public LnMessageClient() { 040 super(new LocoNetSystemConnectionMemo()); 041 clientMemo = new LocoNetSystemConnectionMemo(); // client is separate? 042 } 043 044 /** 045 * Forward messages to the server. 046 */ 047 @Override 048 public void sendLocoNetMessage(LocoNetMessage m) { 049 // update statistics 050 transmittedMsgCount++; 051 052 // attempt to forward message 053 try { 054 if (lnMessageBuffer != null) { 055 lnMessageBuffer.sendLocoNetMessage(m); 056 } else { 057 log.warn("sendLocoNetMessage: no connection to server"); 058 } 059 } catch (java.rmi.RemoteException ex) { 060 log.warn("sendLocoNetMessage: Exception: ", ex); 061 } 062 } 063 064 // Messages that are received from the server should 065 // be passed to this.notify(LocoNetMessage m) 066 /** 067 * Start the connection to the server. This is invoked once. 068 * @param remoteHostName remote host name. 069 * @param timeoutSec timeout, value in seconds, not ms. 070 * @throws jmri.jmrix.loconet.LocoNetException if failed to connect to server. 071 */ 072 public void configureRemoteConnection(String remoteHostName, int timeoutSec) throws LocoNetException { 073 serverName = remoteHostName; 074 pollTimeout = timeoutSec * 1000; // convert to ms 075 076 log.debug("configureRemoteConnection: {} {}", remoteHostName, timeoutSec); 077 078 try { 079 log.debug("set interface to //{}//{}", // NOI18N 080 remoteHostName, LnMessageServer.serviceName); 081 LnMessageServerInterface lnServer = (LnMessageServerInterface) java.rmi.Naming.lookup( 082 "//" + serverName + "/" + LnMessageServer.serviceName); // NOI18N 083 084 lnMessageBuffer = lnServer.getMessageBuffer(clientMemo.getLnTrafficController()); 085 lnMessageBuffer.enable(0); 086 pollThread = new LnMessageClientPollThread(this); 087 } catch (java.rmi.NotBoundException | java.rmi.RemoteException | java.net.MalformedURLException ex) { 088 log.error("Exception while trying to connect: ", ex); // NOI18N 089 throw new LocoNetException("Failed to Connect to Server: " + serverName); // NOI18N 090 } 091 } 092 093 /** 094 * Set up all of the other objects to operate with a server connected to 095 * this application. 096 */ 097 public void configureLocalServices() { 098 // This is invoked on the LnMessageClient after it is 099 // ready to go, connection running, etc. 100 101 // create SlotManager (includes programmer) and connection memo 102 clientMemo.setLnTrafficController(this); 103 // do the common manager config 104 clientMemo.configureCommandStation(LnCommandStationType.COMMAND_STATION_DCS100, // for now, assume full capability 105 false, false, false, false, false); 106 clientMemo.configureManagers(); 107 108 // the serial connections (LocoBuffer et al) start 109 // various threads here. 110 } 111 112 LocoNetSystemConnectionMemo clientMemo; 113 114 public SystemConnectionMemo getAdapterMemo() { 115 return clientMemo; 116 } 117 118 private final static Logger log = LoggerFactory.getLogger(LnMessageClient.class); 119 120}