com.alachisoft.ncache.client.internal.communication.ResponseIntegrator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ncache-professional-client Show documentation
Show all versions of ncache-professional-client Show documentation
NCache Professional client for java.
package com.alachisoft.ncache.client.internal.communication;
import Alachisoft.NCache.Common.Net.Address;
import com.alachisoft.ncache.common.protobuf.FragmentedResponseProtocol;
import com.alachisoft.ncache.common.protobuf.ResponseProtocol;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
// Copyright (c) 2020 Alachisoft
//
// 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
class ResponseIntegrator implements java.util.Comparator {
private java.util.HashMap>> _serverMap = new java.util.HashMap<>();
public final ResponseProtocol.Response AddResponseFragment(Address server, FragmentedResponseProtocol.FragmentedResponse responseFragment) {
java.util.ArrayList responseList = null;
java.util.HashMap> resposeTable = null;
synchronized (this) {
if (!_serverMap.containsKey(server)) {
_serverMap.put(server, new java.util.HashMap<>());
}
resposeTable = _serverMap.get(server);
if (!resposeTable.containsKey(responseFragment.getMessageId())) {
resposeTable.put(responseFragment.getMessageId(), new java.util.ArrayList<>());
}
}
responseList = resposeTable.get(responseFragment.getMessageId());
responseList.add(responseFragment);
if (responseList.size() == responseFragment.getTotalFragments()) {
synchronized (this) {
resposeTable.remove(responseFragment.getMessageId());
}
responseList.sort(this);
ResponseProtocol.Response finalResponse = null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
for (FragmentedResponseProtocol.FragmentedResponse fragment : responseList) {
stream.write(fragment.getMessage().toByteArray(), 0, fragment.getMessage().toByteArray().length);
}
InputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
try {
finalResponse = ResponseProtocol.Response.parseFrom(inputStream);
} catch (IOException e) {
}
} finally {
try {
stream.close();
} catch (IOException e) {
}
}
return finalResponse;
}
return null;
}
public final void RemoveServer(Address server) {
synchronized (this) {
if (_serverMap.containsKey(server)) {
_serverMap.remove(server);
}
}
}
@Override
public int compare(FragmentedResponseProtocol.FragmentedResponse o1, FragmentedResponseProtocol.FragmentedResponse o2) {
if (o1.getFragmentNo() < o2.getFragmentNo()) {
return -1;
}
if (o1.getFragmentNo() > o2.getFragmentNo()) {
return 1;
}
return 0;
}
}