001/* jpvmConnectionServer.java
002 *
003 * The jpvmConnectionServer class implements the thread of control 
004 * in each jpvm program that establishes connections with other
005 * jpvm tasks that want to send data.
006 *
007 * Adam J Ferrari
008 * Sat 05-25-1996
009 *
010 * Copyright (C) 1996  Adam J Ferrari
011 * 
012 * This library is free software; you can redistribute it and/or
013 * modify it under the terms of the GNU Library General Public
014 * License as published by the Free Software Foundation; either
015 * version 2 of the License, or (at your option) any later version.
016 * 
017 * This library is distributed in the hope that it will be useful,
018 * but WITHOUT ANY WARRANTY; without even the implied warranty of
019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
020 * Library General Public License for more details.
021 * 
022 * You should have received a copy of the GNU Library General Public
023 * License along with this library; if not, write to the
024 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
025 * MA 02139, USA.
026 */
027
028package jpvm;
029
030import java.net.*;
031import java.io.*;
032import jpvm.jpvmTaskId;
033import jpvm.jpvmConnectionSet;
034import jpvm.jpvmRecvThread;
035import jpvm.jpvmMessageQueue;
036
037public
038class jpvmConnectionServer extends Thread {
039        private ServerSocket            connectionSock;
040        private int                     connectionPort;
041        private jpvmConnectionSet       connectionSet;
042        private jpvmMessageQueue        queue;
043
044        public jpvmConnectionServer(jpvmConnectionSet c, jpvmMessageQueue q) {
045                connectionSet  = c;
046                connectionSock = null;
047                connectionPort = 0;
048                queue = q;
049                try {
050                        connectionSock = new ServerSocket(0);
051                        connectionPort = connectionSock.getLocalPort();
052                }
053                catch (IOException ioe) {
054                        ioe.printStackTrace();
055                        jpvmDebug.error("jpvmConnectionServer, i/o exception");
056                }
057        }
058
059        public jpvmConnectionServer(jpvmConnectionSet c, jpvmMessageQueue q, int port) {
060                connectionSet  = c;
061                connectionSock = null;
062                connectionPort = port;
063                queue = q;
064                try {
065                        connectionSock = new ServerSocket(connectionPort);
066                        connectionPort = connectionSock.getLocalPort();
067                }
068                catch (IOException ioe) {
069                        ioe.printStackTrace();
070                        jpvmDebug.error("jpvmConnectionServer, i/o exception");
071                }
072        }
073
074        public int getConnectionPort() {
075                return connectionPort;
076        }
077
078        public void run() {
079            while(true) {
080              try {
081                jpvmDebug.note("jpvmConnectionServer, blocking on port " +
082                        connectionSock.getLocalPort());
083                Socket newConnSock = connectionSock.accept();
084                jpvmDebug.note("jpvmConnectionServer, new connection.");
085                jpvmRecvConnection nw = new jpvmRecvConnection(newConnSock);
086                connectionSet.insertRecvConnection(nw);
087
088                // Start a thread to recv on this pipe
089                jpvmRecvThread rt=new jpvmRecvThread(nw,queue);
090                rt.start();
091              }
092              catch (IOException ioe) {
093                        jpvmDebug.error("jpvmConnectionServer, run - " + 
094                                "i/o exception");
095              }
096            }
097        }
098};