1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
|
//******************************************************************************
//
// File: ChannelGroup.java
// Package: edu.rit.mp
// Unit: Class edu.rit.mp.ChannelGroup
//
// This Java source file is copyright (C) 2009 by Alan Kaminsky. All rights
// reserved. For further information, contact the author, Alan Kaminsky, at
// ark@cs.rit.edu.
//
// This Java source file is part of the Parallel Java Library ("PJ"). PJ 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 3 of the License, or (at your option) any later version.
//
// PJ 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. See the GNU General Public License for more details.
//
// Linking this library statically or dynamically with other modules is making a
// combined work based on this library. Thus, the terms and conditions of the
// GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent modules, and
// to copy and distribute the resulting executable under terms of your choice,
// provided that you also meet, for each linked independent module, the terms
// and conditions of the license of that module. An independent module is a
// module which is not derived from or based on this library. If you modify this
// library, you may extend this exception to your version of the library, but
// you are not obligated to do so. If you do not wish to do so, delete this
// exception statement from your version.
//
// A copy of the GNU General Public License is provided in the file gpl.txt. You
// may also obtain a copy of the GNU General Public License on the World Wide
// Web at http://www.gnu.org/licenses/gpl.html.
//
//******************************************************************************
package edu.rit.mp;
import edu.rit.util.Logger;
import edu.rit.util.PrintStreamLogger;
import edu.rit.util.Range;
import edu.rit.util.Timer;
import edu.rit.util.TimerTask;
import edu.rit.util.TimerThread;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.LinkedList;
import java.util.List;
/**
* Class ChannelGroup provides a group of {@linkplain Channel}s for sending and
* receiving messages in the Message Protocol (MP).
* <P>
* <B>Creating Channels</B>
* <P>
* A channel group can be used to create channels in two ways: by accepting a
* connection request from another process, and by requesting a connection to
* another process.
* <P>
* The channel group can be configured to listen to a certain host and port for
* connection requests. Configure the host and port by by calling the
* <TT>listen()</TT> method. To start accepting connection requests, call the
* <TT>startListening()</TT> method.
* <P>
* If desired, an application can receive notification of newly created channels
* by providing a {@linkplain ConnectListener} object to the channel group's
* <TT>setConnectListener()</TT> method. Specify a connect listener object
* before calling the <TT>startListening()</TT> method, otherwise the
* application may not receive some notifications.
* <P>
* When a connection request arrives, the channel group sets up a new channel
* object for communicating over the connection. If a connect listener has been
* registered, the channel group then passes the channel to the connect
* listener's <TT>farEndConnected()</TT> method, which does whatever the
* application needs to record the new channel's presence. The channel does not
* start sending and receiving messages until after the connect listener's
* <TT>farEndConnected()</TT> method (if any) has returned.
* <P>
* The application can also call the channel group's <TT>connect()</TT> method
* to request a connection to another host and port. The channel group sets up a
* new channel object for communicating over the connection. If a connect
* listener has been registered, the channel group then passes the channel to
* the connect listener's <TT>nearEndConnected()</TT> method, which does
* whatever the application needs to record the new channel's presence. The
* channel does not start sending and receiving messages until after the connect
* listener's <TT>nearEndConnected()</TT> method (if any) has returned. The
* <TT>connect()</TT> method also returns the new channel.
* <P>
* Once a connection has been set up and a channel object has been created on
* each side, the applications can use their respective channels to send and
* receive messages.
* <P>
* If a channel group does not need to accept incoming connection requests, the
* channel group need not listen to any host and port. The channel group can
* still be used to make outgoing connection requests.
* <P>
* <B>Channel Group IDs</B>
* <P>
* Each channel group has a channel group ID. The channel group ID is an
* integer, initially 0, that can be changed by the <TT>setChannelGroupId()</TT>
* method. The channel group attaches no significance to the channel group ID;
* it is provided for the use of the application using the channel group.
* <P>
* You can query a channel group object to determine its channel group ID. You
* can also query a channel object to determine the ID of the channel group at
* the near end of the channel and the ID of the channel group at the far end of
* the channel.
* <P>
* <B>Sending Messages</B>
* <P>
* To send a message, the application creates a message buffer (class
* {@linkplain Buf}) specifying where to get the items to be sent. The
* application calls the channel group's <TT>send()</TT> method, passing the
* channel on which to send the message, the message buffer, and the message
* tag. (If the message tag is not specified, it defaults to 0.) The channel
* group extracts the items from the message buffer and sends a message over the
* channel's connection. When the <TT>send()</TT> method returns, the message
* has been fully sent, but the message may not have been fully received yet.
* <P>
* The far end application must receive the message from the channel at the
* other end of the connection. If no application is receiving the message, the
* <TT>send()</TT> method may block (because of flow control). This in turn may
* lead to a deadlock.
* <P>
* At most one outgoing message at a time may be in progress on a channel. If a
* second thread tries to send a message on a channel while a first thread is
* still sending a message on that channel, the second thread will block until
* the first thread has finished sending the message.
* <P>
* <B>Receiving Messages</B>
* <P>
* To receive a message, the application creates a message buffer (class
* {@linkplain Buf}) specifying where to put the items to be received. The
* application calls the channel group's <TT>receive()</TT> method, passing the
* channel from which to receive the message, the desired message tag, and the
* message buffer. The application can specify "any channel" instead of a
* specific channel. The application can specify a range of tags or "any tag"
* instead of a specific tag. Any number of threads can have receive requests
* pending at the same time.
* <P>
* When the channel group receives a message from a channel, the channel group
* tries to match the message with the pending receive requests. A message
* matches a receive request if (a) the message's channel is the same as the
* receive request's channel, or the receive request specified "any channel;"
* and (b) the message's tag is the same as the receive request's tag, or the
* message's tag falls within the receive request's range of tags, or the
* receive request specified "any tag;" and (c) the message's item type is the
* same as the receive request's item type (as given by the message buffer). The
* pending receive requests are maintained in FIFO order. If no receive request
* matches the message, the channel group does not read the message until such
* time as a matching receive request occurs. If more than one receive request
* matches the message, the channel group chooses the first matching receive
* request.
* <P>
* Once the channel group has matched the incoming message with the receive
* request, the channel group reads the items from the message and stores them
* into the message buffer. If there are fewer items in the message than the
* length of the message buffer, the extra items at the end of the message
* buffer are not set to anything. If there are more items in the message than
* the length of the message buffer, the extra items are read from the message
* and discarded. Once the message has been read, the <TT>receive()</TT> method
* returns a {@linkplain Status} object reporting the channel on which the
* message arrived, the message tag, and the actual number of items in the
* message (which may or may not be the same as the number of items in the
* message buffer).
* <P>
* If the receive requests do not match properly with the incoming messages, a
* deadlock may occur.
* <P>
* <B>Sending and Receiving Within the Same Process</B>
* <P>
* Each channel group has a "loopback" channel that is used to send messages
* within the same process. To obtain the loopback channel, call the
* <TT>loopbackChannel()</TT> method. Then one thread can send messages using
* the loopback channel while a different thread receives messages using the
* loopback channel. If the same thread both sends and receives using the
* loopback channel, a deadlock may occur.
* <P>
* The loopback channel uses the <TT>copy()</TT> method of class {@linkplain
* Buf} to transfer data items directly from the source buffer to the
* destination buffer. The loopback channel does not do any network
* communication.
* <P>
* <B>Non-Blocking Send and Receive Operations</B>
* <P>
* The <TT>send()</TT> method described so far does a <B>blocking send</B>
* operation; the <TT>send()</TT> method does not return until the message has
* been fully sent. There is also a <B>non-blocking send</B> operation,
* <TT>sendNoWait()</TT>, which includes an {@linkplain IORequest} argument. The
* <TT>sendNoWait()</TT> method initiates the send operation and returns
* immediately. This allows the caller to continue processing while the channel
* group sends the message in a separate thread. To wait for the message to be
* fully sent, the caller must call the IORequest object's
* <TT>waitForFinish()</TT> method.
* <P>
* Likewise, the <TT>receive()</TT> method described so far does a <B>blocking
* receive</B> operation; the <TT>receive()</TT> method does not return until
* the message has been fully received. There is also a <B>non-blocking
* receive</B> operation, <TT>receiveNoWait()</TT>, which includes an
* {@linkplain IORequest} argument. The <TT>receiveNoWait()</TT> method
* initiates the receive operation and returns immediately. This allows the
* caller to continue processing while the channel group receives the message in
* a separate thread. To wait for the message to be fully received, the caller
* must call the IORequest object's <TT>waitForFinish()</TT> method, which
* returns a {@linkplain Status} object giving the results of the receive
* operation.
*
* @author Alan Kaminsky
* @version 11-Mar-2009
*/
public class ChannelGroup
{
// Hidden data members.
// Channel group ID.
int myChannelGroupId;
// Server socket channel for accepting incoming connections, or null if not
// accepting incoming connections.
ServerSocketChannel myServerSocketChannel;
// I/O request list for matching incoming messages to receive I/O requests.
IORequestList myIORequestList;
// Alternate class loader for use when receiving objects.
ClassLoader myClassLoader;
// Loopback channel.
LoopbackChannel myLoopbackChannel;
// List of open channels.
List<Channel> myChannelList;
// Accepting thread, or null if not accepting.
AcceptThread myAcceptThread;
// Registered connect listener, or null if none.
ConnectListener myConnectListener;
// For logging error messages.
Logger myLogger;
// For timeouts during channel setup.
TimerThread myTimerThread;
// Hidden helper classes.
/**
* Class AcceptThread provides a thread that accepts incoming connections.
*
* @author Alan Kaminsky
* @version 11-Mar-2009
*/
private class AcceptThread
extends Thread
{
public AcceptThread()
{
setDaemon (true);
start();
}
public void run()
{
acceptloop : for (;;)
{
// Wait for an incoming connection.
SocketChannel connection = null;
try
{
connection = myServerSocketChannel.accept();
}
catch (ClosedChannelException exc)
{
myLogger.log
("ChannelGroup: Channel closed",
exc);
break acceptloop;
}
catch (IOException exc)
{
myLogger.log
("ChannelGroup: I/O error while accepting connection",
exc);
break acceptloop;
}
// Set up channel over connection.
if (connection != null)
{
try
{
farEndConnect (connection);
}
catch (IOException exc)
{
// Clear thread's interrupted status, otherwise accept()
// above will throw an exception.
Thread.interrupted();
myLogger.log
("ChannelGroup: I/O error while setting up channel",
exc);
try { connection.close(); } catch (IOException exc2) {}
}
}
}
}
}
// Exported constructors.
/**
* Construct a new channel group. The channel group ID is initially 0. The
* channel group will not listen for connection requests. To listen for
* connection requests at a later time, call the <TT>listen()</TT> method
* followed by the <TT>startListening</TT> method.
* <P>
* The channel group will log error messages on the standard error.
*/
public ChannelGroup()
{
this (new PrintStreamLogger());
}
/**
* Construct a new channel group. The channel group ID is initially 0. The
* channel group will listen for connection requests on the given host and
* port. To start actively listening, call the <TT>startListening()</TT>
* method.
* <P>
* The channel group will log error messages on the standard error.
*
* @param theListenAddress Host and port at which to listen.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theListenAddress</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public ChannelGroup
(InetSocketAddress theListenAddress)
throws IOException
{
this (theListenAddress, new PrintStreamLogger());
}
/**
* Construct a new channel group. The channel group ID is initially 0. The
* channel group will listen for connection requests using the given server
* socket channel. The server socket channel must be bound to a host and
* port. To start actively listening, call the <TT>startListening()</TT>
* method.
* <P>
* The channel group will log error messages on the standard error.
*
* @param theServerSocketChannel Server socket channel.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theServerSocketChannel</TT> is
* null.
* @exception IOException
* Thrown if an I/O error occurred. Thrown if
* <TT>theServerSocketChannel</TT> is not bound.
*/
public ChannelGroup
(ServerSocketChannel theServerSocketChannel)
throws IOException
{
this (theServerSocketChannel, new PrintStreamLogger());
}
/**
* Construct a new channel group. The channel group ID is initially 0. The
* channel group will not listen for connection requests. To listen for
* connection requests at a later time, call the <TT>listen()</TT> method
* followed by the <TT>startListening</TT> method.
* <P>
* The channel group will log error messages using the given logger.
*
* @param theLogger Logger for error messages.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theLogger</TT> is null.
*/
public ChannelGroup
(Logger theLogger)
{
if (theLogger == null)
{
throw new NullPointerException
("ChannelGroup(): theLogger is null");
}
myIORequestList = new IORequestList();
myLoopbackChannel = new LoopbackChannel (this);
myChannelList = new LinkedList<Channel>();
myChannelList.add (myLoopbackChannel);
myLogger = theLogger;
myTimerThread = new TimerThread();
myTimerThread.setDaemon (true);
myTimerThread.start();
}
/**
* Construct a new channel group. The channel group ID is initially 0. The
* channel group will listen for connection requests on the given host and
* port. To start actively listening, call the <TT>startListening()</TT>
* method.
* <P>
* The channel group will log error messages using the given logger.
*
* @param theListenAddress Host and port at which to listen.
* @param theLogger Logger for error messages.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theListenAddress</TT> is null.
* Thrown if <TT>theLogger</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public ChannelGroup
(InetSocketAddress theListenAddress,
Logger theLogger)
throws IOException
{
this (theLogger);
listen (theListenAddress);
}
/**
* Construct a new channel group. The channel group ID is initially 0. The
* channel group will listen for connection requests using the given server
* socket channel. The server socket channel must be bound to a host and
* port. To start actively listening, call the <TT>startListening()</TT>
* method.
* <P>
* The channel group will log error messages using the given logger.
*
* @param theServerSocketChannel Server socket channel.
* @param theLogger Logger for error messages.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theServerSocketChannel</TT> is
* null. Thrown if <TT>theLogger</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred. Thrown if
* <TT>theServerSocketChannel</TT> is not bound.
*/
public ChannelGroup
(ServerSocketChannel theServerSocketChannel,
Logger theLogger)
throws IOException
{
this (theLogger);
listen (theServerSocketChannel);
}
// Exported operations.
/**
* Set this channel group's channel group ID.
*
* @param theChannelGroupId Channel group ID.
*/
public void setChannelGroupId
(int theChannelGroupId)
{
myChannelGroupId = theChannelGroupId;
}
/**
* Obtain this channel group's channel group ID.
*
* @return Channel group ID.
*/
public int getChannelGroupId()
{
return myChannelGroupId;
}
/**
* Obtain this channel group's listen address. This is the near end host and
* port to which this channel group is listening for connection requests. If
* this channel group is not listening for connection requests, null is
* returned.
*
* @return Near end address, or null.
*/
public synchronized InetSocketAddress listenAddress()
{
return
myServerSocketChannel == null ?
null :
(InetSocketAddress)
myServerSocketChannel.socket().getLocalSocketAddress();
}
/**
* Listen for connection requests on the given host and port. To start
* actively listening, call the <TT>startListening()</TT> method.
*
* @param theListenAddress Host and port at which to listen.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theListenAddress</TT> is null.
* @exception IllegalStateException
* (unchecked exception) Thrown if listening has already started.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public synchronized void listen
(InetSocketAddress theListenAddress)
throws IOException
{
if (theListenAddress == null)
{
throw new NullPointerException
("ChannelGroup.listen(): theListenAddress is null");
}
ServerSocketChannel channel = ServerSocketChannel.open();
channel.socket().bind (theListenAddress);
listen (channel);
}
/**
* Listen for connection requests using the given server socket channel. The
* server socket channel must be bound to a host and port. To start actively
* listening, call the <TT>startListening()</TT> method.
*
* @param theServerSocketChannel Server socket channel.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theServerSocketChannel</TT> is
* null.
* @exception IllegalStateException
* (unchecked exception) Thrown if listening has already started.
* @exception IOException
* Thrown if an I/O error occurred. Thrown if
* <TT>theServerSocketChannel</TT> is not bound.
*/
public synchronized void listen
(ServerSocketChannel theServerSocketChannel)
throws IOException
{
if (theServerSocketChannel == null)
{
throw new NullPointerException
("ChannelGroup.listen(): theServerSocketChannel is null");
}
if (! theServerSocketChannel.socket().isBound())
{
throw new IOException
("ChannelGroup.listen(): theServerSocketChannel is not bound");
}
if (myAcceptThread != null)
{
throw new IllegalStateException
("ChannelGroup.listen(): Listening has already started");
}
if (myIORequestList == null)
{
throw new IOException
("ChannelGroup.listen(): Channel group closed");
}
myServerSocketChannel = theServerSocketChannel;
}
/**
* Register the given connect listener with this channel group. Thereafter,
* this channel group will report each connected channel by calling
* <TT>theConnectListener</TT>'s <TT>nearEndConnected()</TT> method (if the
* connection request originated in this process) or
* <TT>farEndConnected()</TT> method (if the connection request originated
* in another process). It is assumed that these methods will not do any
* lengthy processing and will not block the calling thread.
* <P>
* At most one connect listener may be registered. If a connect listener is
* already registered, it is replaced with the given connect listener. If
* <TT>theConnectListener</TT> is null, any registered connect listener is
* discarded, and this channel group will not report connected channels.
* <P>
* Call the <TT>setConnectListener()</TT> method before calling the
* <TT>startListening()</TT> method, otherwise the application may not
* receive some connection notifications.
*
* @param theConnectListener Connect listener, or null.
*/
public synchronized void setConnectListener
(ConnectListener theConnectListener)
{
myConnectListener = theConnectListener;
}
/**
* Start actively listening for connection requests.
*
* @exception IllegalStateException
* (unchecked exception) Thrown if a host and port or a server socket
* channel upon which to listen has not been specified. Thrown if
* listening has already started.
*/
public synchronized void startListening()
{
if (myServerSocketChannel == null)
{
throw new IllegalStateException
("ChannelGroup.startListening(): No server socket channel");
}
if (myAcceptThread != null)
{
throw new IllegalStateException
("ChannelGroup.listen(): Listening has already started");
}
myAcceptThread = new AcceptThread();
}
/**
* Create a new channel connected to the given far end host and port. In the
* far end computer, there must be a channel group listening to the given
* host and port. Once the connection is set up, if a connect listener has
* been registered, the channel group calls the connect listener's
* <TT>nearEndConnected()</TT> method to report the new channel.
*
* @param theFarEndAddress Host and port of far end channel group.
*
* @return New channel.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public Channel connect
(InetSocketAddress theFarEndAddress)
throws IOException
{
synchronized (this)
{
if (myIORequestList == null)
{
throw new IOException
("ChannelGroup.connect(): Channel group closed");
}
}
SocketChannel connection = null;
try
{
connection = SocketChannel.open (theFarEndAddress);
return nearEndConnect (connection);
}
catch (IOException exc)
{
// Clear thread's interrupted status.
Thread.interrupted();
if (connection != null)
{
try { connection.close(); } catch (IOException exc2) {}
}
throw exc;
}
}
/**
* Obtain this channel group's loopback channel. If this channel group is
* closed, null is returned.
*
* @return Loopback channel, or null.
*/
public synchronized Channel loopbackChannel()
{
return myLoopbackChannel;
}
/**
* Send a message to the given channel. The message uses a tag of 0. The
* message items come from the given item source buffer.
* <P>
* The <TT>send()</TT> method does not return until the message has been
* fully sent. (The message may not have been fully received yet.)
* <P>
* The <TT>send()</TT> method assumes that <TT>theChannel</TT> was created
* by this channel group. If not, the <TT>send()</TT> method's behavior is
* unspecified.
*
* @param theChannel Channel.
* @param theSrc Item source buffer.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theChannel</TT> is null or
* <TT>theSrc</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void send
(Channel theChannel,
Buf theSrc)
throws IOException
{
IORequest req = new IORequest();
sendNoWait (theChannel, 0, theSrc, req);
req.waitForFinish();
}
/**
* Send a message to the given channel with the given tag. The message items
* come from the given item source buffer.
* <P>
* The <TT>send()</TT> method does not return until the message has been
* fully sent. (The message may not have been fully received yet.)
* <P>
* The <TT>send()</TT> method assumes that <TT>theChannel</TT> was created
* by this channel group. If not, the <TT>send()</TT> method's behavior is
* unspecified.
*
* @param theChannel Channel.
* @param theTag Message tag.
* @param theSrc Item source buffer.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theChannel</TT> is null or
* <TT>theSrc</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void send
(Channel theChannel,
int theTag,
Buf theSrc)
throws IOException
{
IORequest req = new IORequest();
sendNoWait (theChannel, theTag, theSrc, req);
req.waitForFinish();
}
/**
* Send (non-blocking) a message to the given channel. The message uses a
* tag of 0. The message items come from the given item source buffer.
* <TT>theIORequest</TT> is the IORequest object to be associated with the
* send operation.
* <P>
* The <TT>sendNoWait()</TT> method returns immediately. To wait for the
* message to be fully sent, call <TT>theIORequest.waitForFinish()</TT>.
* <P>
* The <TT>sendNoWait()</TT> method assumes that <TT>theChannel</TT> was
* created by this channel group. If not, the <TT>sendNoWait()</TT> method's
* behavior is unspecified.
*
* @param theChannel Channel.
* @param theSrc Item source buffer.
* @param theIORequest IORequest object.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theChannel</TT> is null,
* <TT>theSrc</TT> is null, or <TT>theIORequest</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void sendNoWait
(Channel theChannel,
Buf theSrc,
IORequest theIORequest)
throws IOException
{
sendNoWait (theChannel, 0, theSrc, theIORequest);
}
/**
* Send (non-blocking) a message to the given channel with the given tag.
* The message items come from the given item source buffer.
* <TT>theIORequest</TT> is the IORequest object to be associated with the
* send operation.
* <P>
* The <TT>sendNoWait()</TT> method returns immediately. To wait for the
* message to be fully sent, call <TT>theIORequest.waitForFinish()</TT>.
* <P>
* The <TT>sendNoWait()</TT> method assumes that <TT>theChannel</TT> was
* created by this channel group. If not, the <TT>sendNoWait()</TT> method's
* behavior is unspecified.
*
* @param theChannel Channel.
* @param theTag Message tag.
* @param theSrc Item source buffer.
* @param theIORequest IORequest object.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theChannel</TT> is null,
* <TT>theSrc</TT> is null, or <TT>theIORequest</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void sendNoWait
(Channel theChannel,
int theTag,
Buf theSrc,
IORequest theIORequest)
throws IOException
{
// Note: This method is not synchronized. Synchronization happens inside
// theChannel.send().
// Verify preconditions.
if (myIORequestList == null)
{
throw new IOException
("ChannelGroup.sendNoWait(): Channel group closed");
}
if (theSrc == null)
{
throw new NullPointerException
("ChannelGroup.sendNoWait(): Source buffer is null");
}
theIORequest.initialize (theChannel, theTag, theTag, theSrc);
theChannel.send (theIORequest);
}
/**
* Receive a message from the given channel. If <TT>theChannel</TT> is null,
* a message will be received from any channel in this channel group. The
* message must have a tag of 0. The message items are stored in the given
* item destination buffer.
* <P>
* The <TT>receive()</TT> method does not return until the message has been
* fully received.
* <P>
* The <TT>receive()</TT> method assumes that <TT>theChannel</TT> was
* created by this channel group. If not, the <TT>receive()</TT> method's
* behavior is unspecified.
*
* @param theChannel Channel, or null to receive from any channel.
* @param theDst Item destination buffer.
*
* @return Status object giving the outcome of the message reception.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theDst</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public Status receive
(Channel theChannel,
Buf theDst)
throws IOException
{
IORequest req = new IORequest();
receiveNoWait (theChannel, 0, 0, theDst, req);
return req.waitForFinish();
}
/**
* Receive a message from the given channel with the given tag. If
* <TT>theChannel</TT> is null, a message will be received from any channel
* in this channel group. The message items are stored in the given item
* destination buffer.
* <P>
* The <TT>receive()</TT> method does not return until the message has been
* fully received.
* <P>
* The <TT>receive()</TT> method assumes that <TT>theChannel</TT> was
* created by this channel group. If not, the <TT>receive()</TT> method's
* behavior is unspecified.
*
* @param theChannel Channel, or null to receive from any channel.
* @param theTag Message tag.
* @param theDst Item destination buffer.
*
* @return Status object giving the outcome of the message reception.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theDst</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public Status receive
(Channel theChannel,
int theTag,
Buf theDst)
throws IOException
{
IORequest req = new IORequest();
receiveNoWait (theChannel, theTag, theTag, theDst, req);
return req.waitForFinish();
}
/**
* Receive a message from the given channel with the given range of tags. If
* <TT>theChannel</TT> is null, a message will be received from any channel
* in this channel group. If <TT>theTagRange</TT> is null, a message will be
* received with any tag. The message items are stored in the given item
* destination buffer.
* <P>
* The <TT>receive()</TT> method does not return until the message has been
* fully received.
* <P>
* The <TT>receive()</TT> method assumes that <TT>theChannel</TT> was
* created by this channel group. If not, the <TT>receive()</TT> method's
* behavior is unspecified.
*
* @param theChannel Channel, or null to receive from any channel.
* @param theTagRange Message tag range, or null to receive any tag.
* @param theDst Item destination buffer.
*
* @return Status object giving the outcome of the message reception.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theDst</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public Status receive
(Channel theChannel,
Range theTagRange,
Buf theDst)
throws IOException
{
IORequest req = new IORequest();
if (theTagRange == null)
{
receiveNoWait
(theChannel, Integer.MIN_VALUE, Integer.MAX_VALUE, theDst, req);
}
else
{
receiveNoWait
(theChannel, theTagRange.lb(), theTagRange.ub(), theDst, req);
}
return req.waitForFinish();
}
/**
* Receive (non-blocking) a message from the given channel. If
* <TT>theChannel</TT> is null, a message will be received from any channel
* in this channel group. The message must have a tag of 0. The message
* items are stored in the given item destination buffer.
* <TT>theIORequest</TT> is the IORequest object to be associated with the
* receive operation.
* <P>
* The <TT>receiveNoWait()</TT> method returns immediately. To wait for the
* message to be fully received, call <TT>theIORequest.waitForFinish()</TT>.
* <P>
* The <TT>receiveNoWait()</TT> method assumes that <TT>theChannel</TT> was
* created by this channel group. If not, the <TT>receiveNoWait()</TT>
* method's behavior is unspecified.
*
* @param theChannel Channel, or null to receive from any channel.
* @param theDst Item destination buffer.
* @param theIORequest IORequest object.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theDst</TT> is null or
* <TT>theIORequest</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void receiveNoWait
(Channel theChannel,
Buf theDst,
IORequest theIORequest)
throws IOException
{
receiveNoWait (theChannel, 0, 0, theDst, theIORequest);
}
/**
* Receive (non-blocking) a message from the given channel with the given
* tag. If <TT>theChannel</TT> is null, a message will be received from any
* channel in this channel group. The message items are stored in the given
* item destination buffer. <TT>theIORequest</TT> is the IORequest object to
* be associated with the receive operation.
* <P>
* The <TT>receiveNoWait()</TT> method returns immediately. To wait for the
* message to be fully received, call <TT>theIORequest.waitForFinish()</TT>.
* <P>
* The <TT>receiveNoWait()</TT> method assumes that <TT>theChannel</TT> was
* created by this channel group. If not, the <TT>receiveNoWait()</TT>
* method's behavior is unspecified.
*
* @param theChannel Channel, or null to receive from any channel.
* @param theTag Message tag.
* @param theDst Item destination buffer.
* @param theIORequest IORequest object.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theDst</TT> is null or
* <TT>theIORequest</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void receiveNoWait
(Channel theChannel,
int theTag,
Buf theDst,
IORequest theIORequest)
throws IOException
{
receiveNoWait (theChannel, theTag, theTag, theDst, theIORequest);
}
/**
* Receive (non-blocking) a message from the given channel with the given
* range of tags. If <TT>theChannel</TT> is null, a message will be received
* from any channel in this channel group. If <TT>theTagRange</TT> is null,
* a message will be received with any tag. The message items are stored in
* the given item destination buffer. <TT>theIORequest</TT> is the IORequest
* object to be associated with the receive operation.
* <P>
* The <TT>receiveNoWait()</TT> method returns immediately. To wait for the
* message to be fully received, call <TT>theIORequest.waitForFinish()</TT>.
* <P>
* The <TT>receiveNoWait()</TT> method assumes that <TT>theChannel</TT> was
* created by this channel group. If not, the <TT>receiveNoWait()</TT>
* method's behavior is unspecified.
*
* @param theChannel Channel, or null to receive from any channel.
* @param theTagRange Message tag range, or null to receive any tag.
* @param theDst Item destination buffer.
* @param theIORequest IORequest object.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theDst</TT> is null or
* <TT>theIORequest</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void receiveNoWait
(Channel theChannel,
Range theTagRange,
Buf theDst,
IORequest theIORequest)
throws IOException
{
if (theTagRange == null)
{
receiveNoWait
(theChannel,
Integer.MIN_VALUE,
Integer.MAX_VALUE,
theDst,
theIORequest);
}
else
{
receiveNoWait
(theChannel,
theTagRange.lb(),
theTagRange.ub(),
theDst,
theIORequest);
}
}
/**
* Receive (non-blocking) a message from the given channel with the given
* tag range. If <TT>theChannel</TT> is null, a message will be received
* from any channel in this channel group. The message items are stored in
* the given item destination buffer. <TT>theIORequest</TT> is the IORequest
* object to be associated with the receive operation.
* <P>
* The <TT>receiveNoWait()</TT> method returns immediately. To wait for the
* message to be fully received, call <TT>theIORequest.waitForFinish()</TT>.
* <P>
* The <TT>receiveNoWait()</TT> method assumes that <TT>theChannel</TT> was
* created by this channel group. If not, the <TT>receiveNoWait()</TT>
* method's behavior is unspecified.
*
* @param theChannel Channel, or null to receive from any channel.
* @param theTagLb Message tag range lower bound.
* @param theTagUb Message tag range upper bound.
* @param theDst Item destination buffer.
* @param theIORequest IORequest object.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theDst</TT> is null or
* <TT>theIORequest</TT> is null.
* @exception IOException
* Thrown if an I/O error occurred.
*/
private void receiveNoWait
(Channel theChannel,
int theTagLb,
int theTagUb,
Buf theDst,
IORequest theIORequest)
throws IOException
{
// Note: This method is not synchronized. Synchronization happens inside
// myIORequestList.add().
// Verify preconditions.
if (myIORequestList == null)
{
throw new IOException
("ChannelGroup.receiveNoWait(): Channel group closed");
}
if (theDst == null)
{
throw new NullPointerException
("ChannelGroup.receiveNoWait(): Destination buffer is null");
}
if (theChannel != null)
{
synchronized (theChannel)
{
// Check whether channel is closed.
if (theChannel.myReadState == Channel.READ_CLOSED)
{
throw new IOException
("ChannelGroup.receiveNoWait(): Channel closed");
}
}
}
theIORequest.initialize (theChannel, theTagLb, theTagUb, theDst);
myIORequestList.add (theIORequest);
}
/**
* Specify an alternate class loader for this channel group. When objects
* are received in a message via this channel group, the given class loader
* will be used to load the objects' classes. If
* <TT>setAlternateClassLoader()</TT> is never called, or if
* <TT>theClassLoader</TT> is null, an alternate class loader will not be
* used.
*
* @param theClassLoader Alternate class loader, or null.
*/
public synchronized void setAlternateClassLoader
(ClassLoader theClassLoader)
{
myClassLoader = theClassLoader;
}
/**
* Close this channel group. Any pending receive requests will fail with a
* {@linkplain ChannelGroupClosedException}.
*/
public synchronized void close()
{
// Stop listening for connections.
if (myServerSocketChannel != null)
{
try
{
myServerSocketChannel.close();
}
catch (IOException exc)
{
}
}
// Close all channels.
if (myChannelList != null)
{
while (! myChannelList.isEmpty())
{
myChannelList.get(0).close();
}
}
// Report failure to all pending receive requests.
if (myIORequestList != null)
{
myIORequestList.reportFailure
(new ChannelGroupClosedException ("Channel group closed"));
}
// Enable garbage collection of fields.
myServerSocketChannel = null;
myIORequestList = null;
myClassLoader = null;
myLoopbackChannel = null;
myChannelList = null;
myAcceptThread = null;
}
/**
* Finalize this channel group.
*/
protected void finalize()
{
close();
}
/**
* Dump the state of this channel group on the given print stream. For
* debugging.
*
* @param out Print stream.
* @param prefix String to print at the beginning of each line.
*/
public void dump
(PrintStream out,
String prefix)
{
out.println (prefix+getClass().getName()+"@"+Integer.toHexString(System.identityHashCode(this)));
out.println (prefix+"myChannelGroupId = "+myChannelGroupId);
out.println (prefix+"myServerSocketChannel = "+myServerSocketChannel);
out.println (prefix+"myIORequestList:");
myIORequestList.dump (out, prefix+"\t");
out.println (prefix+"myClassLoader = "+myClassLoader);
out.println (prefix+"myLoopbackChannel = "+myLoopbackChannel);
out.println (prefix+"myChannelList:");
out.println (prefix+"\t"+myChannelList.size()+" entries");
for (Channel c : myChannelList)
{
c.dump (out, prefix+"\t");
}
out.println (prefix+"myAcceptThread = "+myAcceptThread);
out.println (prefix+"myConnectListener = "+myConnectListener);
out.println (prefix+"myLogger = "+myLogger);
out.println (prefix+"myTimerThread = "+myTimerThread);
}
// Hidden operations.
/**
* Create a new network channel using the given socket channel. The
* connection request originated from the near end. If this channel group is
* closed, null is returned.
*
* @param theSocketChannel Socket channel.
*
* @return New channel.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
Channel nearEndConnect
(SocketChannel theSocketChannel)
throws IOException
{
// Note: This method is not synchronized. Synchronization happens inside
// createNetworkChannel().
// Turn on socket's TCP no-delay option.
Socket socket = theSocketChannel.socket();
socket.setTcpNoDelay (true);
// Send channel group ID to far end.
ByteBuffer buf = ByteBuffer.allocate (4);
buf.putInt (myChannelGroupId);
buf.flip();
if (theSocketChannel.write (buf) != 4)
{
throw new IOException
("ChannelGroup.nearEndConnect(): Cannot send channel group ID");
}
// Receive channel group ID from far end with a 30-second timeout.
buf.clear();
final Thread thread = Thread.currentThread();
Timer timer = myTimerThread.createTimer (new TimerTask()
{
public void action (Timer theTimer)
{
thread.interrupt();
}
});
timer.start (30000L);
if (theSocketChannel.read (buf) != 4)
{
throw new IOException
("ChannelGroup.nearEndConnect(): Cannot receive channel group ID");
}
timer.stop();
buf.flip();
int farChannelGroupId = buf.getInt();
// Set up channel.
Channel channel =
createNetworkChannel (theSocketChannel, farChannelGroupId);
// Inform listener if any.
if (myConnectListener != null)
{
myConnectListener.nearEndConnected (this, channel);
}
// Start the channel sending and receiving messages.
channel.start();
return channel;
}
/**
* Create a new network channel using the given socket channel. The
* connection request originated from the far end. If this channel group is
* closed, null is returned.
*
* @param theSocketChannel Socket channel.
*
* @return New channel.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
Channel farEndConnect
(SocketChannel theSocketChannel)
throws IOException
{
// Note: This method is not synchronized. Synchronization happens inside
// createNetworkChannel().
// Turn on socket's TCP no-delay option.
Socket socket = theSocketChannel.socket();
socket.setTcpNoDelay (true);
// Start a 30-second timeout for receiving channel group ID.
final Thread thread = Thread.currentThread();
Timer timer = myTimerThread.createTimer (new TimerTask()
{
public void action (Timer theTimer)
{
thread.interrupt();
}
});
timer.start (30000L);
try
{
// Receive channel group ID from far end.
ByteBuffer buf = ByteBuffer.allocate (4);
if (theSocketChannel.read (buf) != 4)
{
throw new IOException
("ChannelGroup.farEndConnect(): Cannot receive channel group ID");
}
timer.stop();
buf.flip();
int farChannelGroupId = buf.getInt();
// Send channel group ID to far end.
buf.clear();
buf.putInt (myChannelGroupId);
buf.flip();
if (theSocketChannel.write (buf) != 4)
{
throw new IOException
("ChannelGroup.farEndConnect(): Cannot send channel group ID");
}
// Set up channel.
Channel channel =
createNetworkChannel (theSocketChannel, farChannelGroupId);
// Inform listener if any.
if (myConnectListener != null)
{
myConnectListener.farEndConnected (this, channel);
}
// Start the channel sending and receiving messages.
channel.start();
return channel;
}
// Stop timer when an IOException is thrown.
catch (IOException exc)
{
timer.stop();
throw exc;
}
}
/**
* Create a new network channel using the given socket channel. If this
* channel group is closed, null is returned.
*
* @param theSocketChannel Socket channel.
* @param theFarChannelGroupId Far end channel group ID.
*
* @return New channel, or null.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
synchronized Channel createNetworkChannel
(SocketChannel theSocketChannel,
int theFarChannelGroupId)
throws IOException
{
Channel channel = null;
if (myIORequestList != null)
{
channel =
new NetworkChannel
(this, theSocketChannel, theFarChannelGroupId);
myChannelList.add (channel);
}
return channel;
}
/**
* Remove the given channel from this channel group.
*
* @param Channel.
*/
synchronized void removeChannel
(Channel channel)
{
if (myChannelList != null)
{
myChannelList.remove (channel);
}
}
}
|