use of org.apache.zookeeper.proto.ReplyHeader in project zookeeper by apache.
the class ZooKeeperServer method processPacket.
public void processPacket(ServerCnxn cnxn, ByteBuffer incomingBuffer) throws IOException {
// We have the request, now process and setup for next
InputStream bais = new ByteBufferInputStream(incomingBuffer);
BinaryInputArchive bia = BinaryInputArchive.getArchive(bais);
RequestHeader h = new RequestHeader();
h.deserialize(bia, "header");
// Through the magic of byte buffers, txn will not be
// pointing
// to the start of the txn
incomingBuffer = incomingBuffer.slice();
if (h.getType() == OpCode.auth) {
LOG.info("got auth packet " + cnxn.getRemoteSocketAddress());
AuthPacket authPacket = new AuthPacket();
ByteBufferInputStream.byteBuffer2Record(incomingBuffer, authPacket);
String scheme = authPacket.getScheme();
ServerAuthenticationProvider ap = ProviderRegistry.getServerProvider(scheme);
Code authReturn = KeeperException.Code.AUTHFAILED;
if (ap != null) {
try {
authReturn = ap.handleAuthentication(new ServerAuthenticationProvider.ServerObjs(this, cnxn), authPacket.getAuth());
} catch (RuntimeException e) {
LOG.warn("Caught runtime exception from AuthenticationProvider: " + scheme + " due to " + e);
authReturn = KeeperException.Code.AUTHFAILED;
}
}
if (authReturn == KeeperException.Code.OK) {
if (LOG.isDebugEnabled()) {
LOG.debug("Authentication succeeded for scheme: " + scheme);
}
LOG.info("auth success " + cnxn.getRemoteSocketAddress());
ReplyHeader rh = new ReplyHeader(h.getXid(), 0, KeeperException.Code.OK.intValue());
cnxn.sendResponse(rh, null, null);
} else {
if (ap == null) {
LOG.warn("No authentication provider for scheme: " + scheme + " has " + ProviderRegistry.listProviders());
} else {
LOG.warn("Authentication failed for scheme: " + scheme);
}
// send a response...
ReplyHeader rh = new ReplyHeader(h.getXid(), 0, KeeperException.Code.AUTHFAILED.intValue());
cnxn.sendResponse(rh, null, null);
// ... and close connection
cnxn.sendBuffer(ServerCnxnFactory.closeConn);
cnxn.disableRecv();
}
return;
} else {
if (h.getType() == OpCode.sasl) {
Record rsp = processSasl(incomingBuffer, cnxn);
ReplyHeader rh = new ReplyHeader(h.getXid(), 0, KeeperException.Code.OK.intValue());
// not sure about 3rd arg..what is it?
cnxn.sendResponse(rh, rsp, "response");
return;
} else {
Request si = new Request(cnxn, cnxn.getSessionId(), h.getXid(), h.getType(), incomingBuffer, cnxn.getAuthInfo());
si.setOwner(ServerCnxn.me);
// Always treat packet from the client as a possible
// local request.
setLocalSessionFlag(si);
submitRequest(si);
}
}
cnxn.incrOutstandingRequests(h);
}
use of org.apache.zookeeper.proto.ReplyHeader in project zookeeper by apache.
the class ClientTest method testNonExistingOpCode.
/**
* We create a perfectly valid 'exists' request, except that the opcode is wrong.
* @return
* @throws Exception
*/
@Test
public void testNonExistingOpCode() throws Exception {
final CountDownLatch clientDisconnected = new CountDownLatch(1);
Watcher watcher = new Watcher() {
@Override
public synchronized void process(WatchedEvent event) {
if (event.getState() == KeeperState.Disconnected) {
clientDisconnected.countDown();
}
}
};
TestableZooKeeper zk = new TestableZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher);
final String path = "/m1";
RequestHeader h = new RequestHeader();
// This code does not exists
h.setType(888);
ExistsRequest request = new ExistsRequest();
request.setPath(path);
request.setWatch(false);
ExistsResponse response = new ExistsResponse();
ReplyHeader r = zk.submitRequest(h, request, response, null);
Assert.assertEquals(r.getErr(), Code.UNIMPLEMENTED.intValue());
// Sending a nonexisting opcode should cause the server to disconnect
Assert.assertTrue("failed to disconnect", clientDisconnected.await(5000, TimeUnit.MILLISECONDS));
}
use of org.apache.zookeeper.proto.ReplyHeader in project zookeeper by apache.
the class NettyServerCnxn method process.
@Override
public void process(WatchedEvent event) {
ReplyHeader h = new ReplyHeader(-1, -1L, 0);
if (LOG.isTraceEnabled()) {
ZooTrace.logTraceMessage(LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, "Deliver event " + event + " to 0x" + Long.toHexString(this.sessionId) + " through " + this);
}
// Convert WatchedEvent to a type that can be sent over the wire
WatcherEvent e = event.getWrapper();
try {
sendResponse(h, e, "notification");
} catch (IOException e1) {
if (LOG.isDebugEnabled()) {
LOG.debug("Problem sending to " + getRemoteSocketAddress(), e1);
}
close();
}
}
use of org.apache.zookeeper.proto.ReplyHeader in project zookeeper by apache.
the class UnimplementedRequestProcessor method processRequest.
public void processRequest(Request request) throws RequestProcessorException {
KeeperException ke = new KeeperException.UnimplementedException();
request.setException(ke);
ReplyHeader rh = new ReplyHeader(request.cxid, request.zxid, ke.code().intValue());
try {
request.cnxn.sendResponse(rh, null, "response");
} catch (IOException e) {
throw new RequestProcessorException("Can't send the response", e);
}
request.cnxn.sendCloseSession();
}
use of org.apache.zookeeper.proto.ReplyHeader in project zookeeper by apache.
the class ZooKeeper method removeWatches.
private void removeWatches(int opCode, String path, Watcher watcher, WatcherType watcherType, boolean local, VoidCallback cb, Object ctx) {
PathUtils.validatePath(path);
final String clientPath = path;
final String serverPath = prependChroot(clientPath);
WatchDeregistration wcb = new WatchDeregistration(clientPath, watcher, watcherType, local, watchManager);
RequestHeader h = new RequestHeader();
h.setType(opCode);
Record request = getRemoveWatchesRequest(opCode, watcherType, serverPath);
cnxn.queuePacket(h, new ReplyHeader(), request, null, cb, clientPath, serverPath, ctx, null, wcb);
}
Aggregations