本文整理汇总了Java中java.nio.channels.NotYetConnectedException类的典型用法代码示例。如果您正苦于以下问题:Java NotYetConnectedException类的具体用法?Java NotYetConnectedException怎么用?Java NotYetConnectedException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NotYetConnectedException类属于java.nio.channels包,在下文中一共展示了NotYetConnectedException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendNetworkEvent
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
public void sendNetworkEvent(BaseEvent event) throws IOException {
if (this.getStatus() == Node.CLOSE) {
return;
}
if (writeTarget == null) {
throw new NotYetConnectedException();
}
if (this.status != Node.HANDSHAKE && !isHandShakeMessage(event)) {
throw new NotYetConnectedException();
}
lock.lock();
try {
byte[] data = event.serialize();
NulsMessage message = new NulsMessage(magicNumber, data);
this.writeTarget.write(message.serialize());
} finally {
lock.unlock();
}
}
示例2: broadcastToGroup
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
private BroadcastResult broadcastToGroup(NulsMessage message, String groupName, String excludeNodeId) {
List<Node> broadNodes = nodesManager.getGroupAvailableNodes(groupName, excludeNodeId);
if (broadNodes.size() <= 1) {
return new BroadcastResult(false, "no node can be broadcast");
}
int successCount = 0;
for (Node node : broadNodes) {
try {
node.sendMessage(message);
successCount++;
} catch (NotYetConnectedException | IOException e) {
Log.warn("broadcast message error , maybe the node closed ! node ip :{}, {}", node.getIp(), e.getMessage());
}
}
if (successCount == 0) {
new BroadcastResult(false, "broadcast fail", broadNodes);
}
Log.debug("成功广播给{}个节点,消息{}", successCount, message);
return new BroadcastResult(true, "OK");
}
示例3: sendMessage
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
/**
* Sends the given message to the peer. Due to the asynchronousness of network programming, there is no guarantee
* the peer will have received it. Throws NotYetConnectedException if we are not yet connected to the remote peer.
* TODO: Maybe use something other than the unchecked NotYetConnectedException here
*/
public void sendMessage(Message message) throws NotYetConnectedException {
lock.lock();
try {
if (writeTarget == null)
throw new NotYetConnectedException();
} finally {
lock.unlock();
}
// TODO: Some round-tripping could be avoided here
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
serializer.serialize(message, out);
writeTarget.writeBytes(out.toByteArray());
} catch (IOException e) {
exceptionCaught(e);
}
}
示例4: invokeMethod
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
public void invokeMethod(String method, Object paramsObject,
@Nullable PendingRequestCallback callback)
throws NotYetConnectedException {
Util.throwIfNull(method);
Long requestId = (callback != null) ? preparePendingRequest(callback) : null;
// magic, can basically convert anything for some amount of runtime overhead...
JSONObject params = mObjectMapper.convertValue(paramsObject, JSONObject.class);
JsonRpcRequest message = new JsonRpcRequest(requestId, method, params);
String requestString;
JSONObject jsonObject = mObjectMapper.convertValue(message, JSONObject.class);
requestString = jsonObject.toString();
mPeer.sendText(requestString);
}
示例5: invokeMethod
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
public void invokeMethod(String method, Object paramsObject,
@Nullable PendingRequestCallback callback)
throws NotYetConnectedException {
Util.throwIfNull(method);
Long requestId = (callback != null) ? preparePendingRequest(callback) : null;
// magic, can basically convert anything for some amount of runtime overhead...
JSONObject params = mObjectMapper.convertValue(paramsObject, JSONObject.class);
JsonRpcRequest message = new JsonRpcRequest(requestId, method, params);
String requestString;
JSONObject jsonObject = mObjectMapper.convertValue(message, JSONObject.class);
requestString = jsonObject.toString();
mPeer.sendText(requestString);
}
示例6: write
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
public long write (ByteBuffer[] srcs, int offset, int length)
throws IOException
{
if (!isConnected())
throw new NotYetConnectedException();
if ((offset < 0)
|| (offset > srcs.length)
|| (length < 0)
|| (length > (srcs.length - offset)))
throw new IndexOutOfBoundsException();
/* We are connected, meaning we will write these bytes to
* the host we connected to, so we don't need to explicitly
* give the host. */
return channel.writeGathering(srcs, offset, length);
}
示例7: exceptionCaught
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
Throwable cause = e.getCause();
// do not print exception if it is BindException.
// we are trying to search available port below 1024. It is not good to
// print a flood
// of error logs during the searching.
if (cause instanceof java.net.BindException) {
return;
}
LOG.error("Exception on connection to " + getRemoteAddress(), e.getCause());
// close the channel unless we are connecting and it is
// NotYetConnectedException
if (!((cause instanceof NotYetConnectedException)
&& _connection.getConnectionState().equals(Connection.State.CONNECTING))) {
ctx.getChannel().close();
}
}
示例8: doWriteFileRegion
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
@Override
protected void doWriteFileRegion(FileRegion region) throws Exception {
OutputStream os = this.os;
if (os == null) {
throw new NotYetConnectedException();
}
if (outChannel == null) {
outChannel = Channels.newChannel(os);
}
long written = 0;
for (;;) {
long localWritten = region.transferTo(outChannel, written);
if (localWritten == -1) {
checkEOF(region);
return;
}
written += localWritten;
if (written >= region.count()) {
return;
}
}
}
示例9: testWriteByteBufferArray_Block
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
@TestTargetNew(
level = TestLevel.PARTIAL_COMPLETE,
notes = "Doesn't verify all exceptions according to specification.",
method = "write",
args = {java.nio.ByteBuffer[].class}
)
public void testWriteByteBufferArray_Block() throws IOException {
ByteBuffer[] writeBuf = new ByteBuffer[2];
writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
InetSocketAddress ipAddr = localAddr1;
try {
this.channel1.write(writeBuf);
fail("Should throw NotYetConnectedException.");
} catch (NotYetConnectedException e) {
// correct
}
this.channel1.connect(ipAddr);
assertTrue(this.channel1.isConnected());
assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf));
// cannot be buffered again!
assertEquals(0, this.channel1.write(writeBuf));
}
示例10: testSerializationSelf
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
/**
* @tests serialization/deserialization compatibility.
*/
@TestTargets({
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Verifies serialization/deserialization compatibility.",
method = "!SerializationSelf",
args = {}
),
@TestTargetNew(
level = TestLevel.PARTIAL_COMPLETE,
notes = "Verifies serialization/deserialization compatibility.",
method = "NotYetConnectedException",
args = {}
)
})
public void testSerializationSelf() throws Exception {
SerializationTest.verifySelf(new NotYetConnectedException());
}
示例11: testSerializationCompatibility
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
/**
* @tests serialization/deserialization compatibility with RI.
*/
@TestTargets({
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Verifies serialization/deserialization compatibility.",
method = "!SerializationGolden",
args = {}
),
@TestTargetNew(
level = TestLevel.PARTIAL_COMPLETE,
notes = "Verifies serialization/deserialization compatibility.",
method = "NotYetConnectedException",
args = {}
)
})
public void testSerializationCompatibility() throws Exception {
SerializationTest.verifyGolden(this, new NotYetConnectedException());
}
示例12: testOpen
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
public void testOpen() throws IOException {
java.nio.ByteBuffer[] buf = new java.nio.ByteBuffer[1];
buf[0] = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL);
MockSocketChannel testMSChannel = new MockSocketChannel(null);
MockSocketChannel testMSChannelnotnull = new MockSocketChannel(
SelectorProvider.provider());
SocketChannel testSChannel = MockSocketChannel.open();
assertTrue(testSChannel.isOpen());
assertNull(testMSChannel.provider());
assertNotNull(testSChannel.provider());
assertEquals(SelectorProvider.provider(), testSChannel.provider());
assertNotNull(testMSChannelnotnull.provider());
assertEquals(this.channel1.provider(), testMSChannelnotnull.provider());
try {
this.channel1.write(buf);
fail("Should throw NotYetConnectedException");
} catch (NotYetConnectedException e) {
// correct
}
}
示例13: send
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
/**
* Sends <var>text</var> to server
*
* @param text
* String to send to server
*/
@JavascriptInterface
public void send(final String text) {
new Thread(new Runnable() {
@Override
public void run() {
if (instance.readyState == WEBSOCKET_STATE_OPEN) {
try {
instance._send(text);
} catch (Exception e) {
instance.onError(e);
}
} else {
instance.onError(new NotYetConnectedException());
}
}
}).start();
}
示例14: sendMessage
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
/**
* Sends the given message to the peer. Due to the asynchronousness of network programming,
* there is no guarantee
* the peer will have received it. Throws NotYetConnectedException if we are not yet
* connected to the remote peer.
* TODO: Maybe use something other than the unchecked NotYetConnectedException here
*/
public void sendMessage(Message message) throws NotYetConnectedException {
lock.lock();
try {
if (writeTarget == null) {
throw new NotYetConnectedException();
}
} finally {
lock.unlock();
}
// TODO: Some round-tripping could be avoided here
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
serializer.serialize(message, out);
writeTarget.writeBytes(out.toByteArray());
} catch (IOException e) {
exceptionCaught(e);
}
}
示例15: testWriteByteBufferArrayIntInt_Block
import java.nio.channels.NotYetConnectedException; //导入依赖的package包/类
public void testWriteByteBufferArrayIntInt_Block() throws IOException {
ByteBuffer[] writeBuf = new ByteBuffer[2];
writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
InetSocketAddress ipAddr = localAddr1;
try {
this.channel1.write(writeBuf, 0, 2);
fail("Should throw NotYetConnectedException.");
} catch (NotYetConnectedException e) {
// correct
}
this.channel1.connect(ipAddr);
assertTrue(this.channel1.isConnected());
assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2));
// cannot be buffered again!
assertEquals(0, this.channel1.write(writeBuf, 0, 1));
}