
net.sf.eBus.client.MessageKeyStore Maven / Gradle / Ivy
//
// Copyright 2015, 2016 Charles W. Rapp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package net.sf.eBus.client;
import java.util.HashMap;
import java.util.Map;
import net.sf.eBus.client.sysmessages.KeyMessage;
import net.sf.eBus.client.sysmessages.SystemMessageType;
import net.sf.eBus.messages.EMessageHeader;
import net.sf.eBus.messages.EMessageKey;
/**
* This class is responsible for mapping message classes to a
* unique identifier (uniqueness scoped to within this JVM). This
* mapping is shared with remote eBus applications upon
* successful connection, where upon the remote eBus connection
* maps the class identifier with a message reader. This message
* reader contains both the message class de-serializer and
* method handle to the message destination.
*
* Using a class identifier in message transmission minimizes the
* {@code ByteBuffer} de-serialization time and forwarding the
* message to its ultimate destination. Without this feature,
* eBus would have to insert the fully-qualified class name into
* each message which takes more time and space to serialize and
* de-serialize.
*
*
* @author Charles W. Rapp
*/
/* package */ final class MessageKeyStore
{
//---------------------------------------------------------------
// Member data.
//
//-----------------------------------------------------------
// Locals.
//
/**
* This message class store belongs to this eBus remote
* application connection. Forward newly defined message
* class identifiers on this connection.
*/
private final ERemoteApp mConnection;
/**
* Maps message key to its unique integer identifier. This
* identifier is used in encoded eBus message header rather
* than the class name and subject allowing the header to be
* a fixed size.
*/
private final Map mIds;
/**
* Assign the next new message key this identifier. Starts at
* zero.
*/
private int mNextId;
//---------------------------------------------------------------
// Member methods.
//
//-----------------------------------------------------------
// Constructors.
//
/**
* Creates the message class-to-identifier store for the
* given eBus remote application connection.
* @param connection remote connection.
*/
/* package */ MessageKeyStore(final ERemoteApp connection)
{
mConnection = connection;
mIds = new HashMap<>();
mNextId = 0;
} // end of MessageKeyStore(ERemoteApp)
//
// end of Constructors.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Get Methods.
//
/**
* Returns the identifier associated with the given message
* class. If the message class is not in the message store,
* then it is assigned the next available identifier and that
* pairing is stored.
* @param key return this message key.
* @return unique message class identifier.
*/
/* package */ synchronized int findOrCreate(final EMessageKey key)
{
int retval;
if (mIds.containsKey(key))
{
retval = mIds.get(key);
}
else
{
retval = mNextId;
mNextId++;
mIds.put(key, retval);
// Forward this new message class, identifier pair on
// the remote eBus connection.
mConnection.send(
new EMessageHeader(
(SystemMessageType.KEY_UPDATE).keyId(),
0,
0,
(KeyMessage.builder()).keyId(retval)
.messageKey(key)
.build()));
}
return (retval);
} // end of findOrCreate(EMessageKey)
//
// end of Get Methods.
//-----------------------------------------------------------
} // end of class MessageKeyStore
© 2015 - 2025 Weber Informatics LLC | Privacy Policy