
com.caucho.v5.ramp.hamp.OutHamp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of baratine Show documentation
Show all versions of baratine Show documentation
A reactive Java web server.
/*
* Copyright (c) 1998-2015 Caucho Technology -- all rights reserved
*
* This file is part of Baratine(TM)
*
* Each copy or derived work must preserve the copyright notice and this
* notice unmodified.
*
* Baratine is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Baratine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
* of NON-INFRINGEMENT. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with Baratine; if not, write to the
*
* Free Software Foundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307 USA
*
* @author Scott Ferguson
*/
package com.caucho.v5.ramp.hamp;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.caucho.v5.amp.ErrorAmp;
import com.caucho.v5.amp.spi.HeadersAmp;
import com.caucho.v5.bartender.pod.PodRef;
import com.caucho.v5.h3.H3;
import com.caucho.v5.h3.OutFactoryH3;
import com.caucho.v5.h3.OutH3;
import io.baratine.stream.ResultStream;
/**
* OutHamp writes HAMP messages to an OutputStream.
*/
public class OutHamp
{
private static final Logger log
= Logger.getLogger(OutHamp.class.getName());
private Level _level = Level.FINEST;
private OutH3 _out;
//private HessianDebugOutputStream _dOut;
private int _methodCacheIndex;
private HashMap _methodCache = new HashMap<>(256);
private MethodKey []_methodCacheRing = new MethodKey[256];
private MethodKey _methodKey = new MethodKey();
private int _toAddressCacheIndex;
private HashMap _toAddressCache = new HashMap<>(256);
private String []_toAddressCacheRing = new String[256];
private int _fromAddressCacheIndex;
private HashMap _fromAddressCache = new HashMap<>(256);
private String []_fromAddressCacheRing = new String[256];
private OutFactoryH3 _serializer;
public OutHamp()
{
_serializer = H3.newOutFactory().get();
//_out = new Hessian2Output();
//_out.getSerializerFactory().setAllowNonSerializable(true);
//_out.setUnshared(true);
/*
if (log.isLoggable(Level.FINEST)) {
_dOut = new HessianDebugOutputStream(log, Level.FINEST);
}
*/
}
protected void init(OutputStream os)
{
/*
if (_dOut != null) {
_dOut.initPacket(os);
os = _dOut;
}
_out.initPacket(os);
*/
_out = _serializer.out(os);
}
//
// message
//
/**
* Sends a message to a given address
*/
public void send(OutputStream os,
HeadersAmp headers,
String address,
String methodName,
PodRef podCaller,
Object []args)
throws IOException
{
init(os);
OutH3 out = _out;
if (out == null) {
return;
}
if (log.isLoggable(Level.FINEST)) {
log.finest("hamp-send-w " + methodName + Arrays.asList(args)
+ "\n {to:" + address + "}");
}
try {
out.writeLong(MessageTypeHamp.SEND.ordinal());
writeHeaders(out, headers);
writeMethod(out, address, methodName, podCaller);
writeArgs(out, args);
// XXX: out.flushBuffer();
} catch (Throwable e) {
log.log(Level.WARNING, e.toString(), e);
throw e;
}
}
/**
* Sends a message to a given address
*/
public void query(OutputStream os,
HeadersAmp headers,
String from,
long qId,
String address,
String methodName,
PodRef podCaller,
Object []args)
throws IOException
{
init(os);
OutH3 out = _out;
if (out == null) {
return;
}
if (log.isLoggable(Level.FINEST)) {
log.finest("hamp-query-w " + methodName + (args != null ? Arrays.asList(args) : "[]")
+ " {to:" + address + ", from:" + from + "}");
}
try {
out.writeLong(MessageTypeHamp.QUERY.ordinal());
writeHeaders(out, headers);
writeFromAddress(out, from);
out.writeLong(qId);
writeMethod(out, address, methodName, podCaller);
writeArgs(out, args);
// XXX: out.flushBuffer();
} catch (Throwable e) {
log.log(Level.WARNING, e.toString(), e);
throw e;
}
}
/**
* Sends a message to a given address
*/
public void queryResult(OutputStream os,
HeadersAmp headers,
String address,
long qId,
Object value)
throws IOException
{
init(os);
OutH3 out = _out;
if (out == null) {
return;
}
if (log.isLoggable(_level)) {
log.log(_level, "hamp-query-result-w " + value + " (in " + this + ")"
+ "\n {id:" + qId + " to:" + address + ", " + headers + "," + os + "}");
}
try {
out.writeLong(MessageTypeHamp.QUERY_RESULT.ordinal());
writeHeaders(out, headers);
writeToAddress(out, address);
out.writeLong(qId);
out.writeObject(value);
// XXX: out.flushBuffer();
// out.flush();
} catch (Throwable e) {
log.log(Level.WARNING, e.toString(), e);
throw e;
}
}
/**
* Sends a message to a given address
*/
public void queryError(OutputStream os,
HeadersAmp headers,
String address,
long qId,
Throwable exn)
throws IOException
{
init(os);
OutH3 out = _out;
if (out == null)
return;
if (log.isLoggable(_level)) {
log.log(_level, "query-error " + exn
+ " (in " + this + ")" + " {to:" + address + ", " + headers + "}");
if (log.isLoggable(Level.FINEST)) {
log.log(Level.FINEST, exn.toString(), exn);
}
}
try {
out.writeLong(MessageTypeHamp.QUERY_ERROR.ordinal());
writeHeaders(out, headers);
writeToAddress(out, address);
out.writeLong(qId);
ErrorAmp error = ErrorAmp.create(exn);
out.writeString(error.getCode().toString());
out.writeString(error.getMessage());
out.writeObject(error.getDetail());
// XXX: out.flushBuffer();
} catch (Throwable e) {
log.log(Level.WARNING, e.toString(), e);
throw e;
}
}
/**
* Sends a stream message to a given address
*/
public void stream(OutputStream os,
HeadersAmp headers,
String from,
long qId,
String address,
String methodName,
PodRef podCaller,
ResultStream> result,
Object []args)
throws IOException
{
init(os);
OutH3 out = _out;
if (out == null) {
return;
}
if (log.isLoggable(Level.FINEST)) {
log.finest("hamp-stream-w " + methodName + (args != null ? Arrays.asList(args) : "[]")
+ " {to:" + address + ", from:" + from + "}");
}
out.writeLong(MessageTypeHamp.STREAM.ordinal());
writeHeaders(out, headers);
writeFromAddress(out, from);
out.writeLong(qId);
writeMethod(out, address, methodName, podCaller);
out.writeObject(result);
writeArgs(out, args);
//out.flushBuffer();
out.flush();
}
/**
* Sends a message to a given address
*/
public void streamResult(OutputStream os,
HeadersAmp headers,
String address,
long qId,
int sequence,
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy