/// <summary> Returns a Connection to the given address, opening this /// Connection if necessary. The given Parser will only be used if a new Connection /// is opened, so there is no guarantee that the Connection returnd will be using the /// Parser you provide. If you need explicit access to the Parser the Connection /// is using, call <code>Connection.getParser()</code>. /// </summary> public virtual NuGenConnection attach(System.String host, int port, Parser parser, System.Type llpClass) { NuGenConnection conn = getExisting(host, port, parser.GetType(), llpClass); if (conn == null) { try { //Parser p = (Parser) parserClass.newInstance(); LowerLayerProtocol llp = (LowerLayerProtocol)System.Activator.CreateInstance(llpClass); conn = connect(host, port, parser, llp); } catch (System.InvalidCastException e) { //Log.tryToLog(, "Problem opening new connection to " + host + " port " + port); throw new NuGenHL7Exception("ClassCastException - need a LowerLayerProtocol class to get an Inititator", NuGenHL7Exception.APPLICATION_INTERNAL_ERROR, e); } catch (System.Exception e) { //Log.tryToLog(e, "Problem opening new connection to " + host + " port " + port); throw new NuGenHL7Exception("Can't connect to " + host + " port " + port + ": " + e.GetType().FullName + ": " + e.Message, NuGenHL7Exception.APPLICATION_INTERNAL_ERROR, e); } } incrementRefs(conn); return(conn); }
public static void Main(System.String[] args) { if (args.Length != 2) { System.Console.Out.WriteLine("Usage: Genetibase.NuGenHL7.app.Initiator host port"); } try { //set up connection to server System.String host = args[0]; int port = System.Int32.Parse(args[1]); Parser parser = new PipeParser(); LowerLayerProtocol llp = LowerLayerProtocol.makeLLP(); NuGenConnection connection = new NuGenConnection(parser, llp, new System.Net.Sockets.TcpClient(host, port)); NuGenInitiator initiator = connection.Initiator; System.String outText = "MSH|^~\\&|||||||ACK^^ACK|||R|2.4|\rMSA|AA"; //get a bunch of threads to send messages for (int i = 0; i < 1000; i++) { SupportClass.ThreadClass sender = new SupportClass.ThreadClass(new System.Threading.ThreadStart(new AnonymousClassRunnable(parser, outText, initiator).Run)); sender.Start(); } } catch (System.Exception e) { SupportClass.WriteStackTrace(e, Console.Error); } }
public virtual void Run() { while (Enclosing_Instance.keepRunning()) { try { System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 500)); } catch (System.Threading.ThreadInterruptedException) { } lock (service) { System.Collections.IEnumerator it = service.RemoteConnections.GetEnumerator(); System.Collections.IList remove = new System.Collections.ArrayList(); while (it.MoveNext()) { NuGenConnection conn = (NuGenConnection)it.Current; if (!conn.Open) { //service.RemoteConnections.Remove(it.Current); remove.Add(it.Current); service.notifyListeners(conn); } } foreach (object item in remove) { service.RemoteConnections.Remove(item); } } } }
/// <summary> Returns a connection to a remote host that was initiated by the given remote host. /// If the connection has not been made, this method blocks until the remote host /// connects. /// </summary> public virtual NuGenConnection getRemoteConnection(System.String IP) { NuGenConnection conn = null; while (conn == null) { //check all connections ... int c = 0; lock (this) { while (conn == null && c < connections.Count) { NuGenConnection nextConn = (NuGenConnection)connections[c]; if (nextConn.RemoteAddress.ToString().Equals(IP)) { conn = nextConn; } c++; } } if (conn == null) { try { System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 100)); } catch (System.Threading.ThreadInterruptedException) { } } } return(conn); }
/// <summary> Initially sets up server sockets and starts separate threads to accept connections /// on them. Then loops, calling this.accept() super.newConnection(). /// </summary> public override void Run() { try { AcceptThread inAccept = new AcceptThread(this, inboundPort, inSockets); AcceptThread outAccept = new AcceptThread(this, outboundPort, outSockets); SupportClass.ThreadClass inThread = new SupportClass.ThreadClass(new System.Threading.ThreadStart(inAccept.Run)); SupportClass.ThreadClass outThread = new SupportClass.ThreadClass(new System.Threading.ThreadStart(outAccept.Run)); inThread.Start(); outThread.Start(); while (keepRunning()) { NuGenConnection conn = accept(3000); if (conn != null) { newConnection(conn); } } inAccept.stop(); outAccept.stop(); } catch (System.Exception) { } }
private void discardConnection(NuGenConnection c) { lock (this) { this.connections.Remove(c); notifyListeners(c); } }
/// <summary> Opens a connection to the given address, and stores it in the /// connections Hash. /// </summary> private NuGenConnection connect(System.String host, int port, Parser parser, LowerLayerProtocol llp) { System.Net.Sockets.TcpClient s = new System.Net.Sockets.TcpClient(host, port); NuGenConnection i = new NuGenConnection(parser, llp, s); connections[makeHashKey(host, port, parser.GetType(), llp.GetType())] = i; sockets[makeHashKey(host, port, parser.GetType(), llp.GetType())] = s; return(i); }
/// <summary> Called by subclasses when a new Connection is made. Registers the /// MessageTypeRouter with the given Connection and stores it. /// </summary> public virtual void newConnection(NuGenConnection c) { lock (this) { c.Responder.registerApplication(router); this.connections.Add(c); //keep track of connections notifyListeners(c); } }
/// <summary> Informs the ConnectionHub that you are done with the given Connection - /// if no other code is using it, it will be closed, so you should not /// attempt to use a Connection after detaching from it. /// </summary> public virtual void detach(NuGenConnection c) { int refs = decrementRefs(c); if (refs == 0) { close(c); } }
/// <summary>Returns an existing connection if one exists, null otherwise </summary> private NuGenConnection getExisting(System.String host, int port, System.Type parserClass, System.Type llpClass) { NuGenConnection existing = null; System.Object o = connections[makeHashKey(host, port, parserClass, llpClass)]; if (o != null) { existing = (NuGenConnection)o; } return(existing); }
/// <summary>Updates the number of references to i - used by incrementRefs and decrementRefs </summary> private int updateRefs(NuGenConnection c, int change) { System.Int32 hashCode = (System.Int32)c.GetHashCode(); System.Object o = numRefs[hashCode]; int existingRefs = 0; if (o != null) { existingRefs = ((System.Int32)o); } System.Int32 newRefs = (System.Int32)(existingRefs + change); numRefs[hashCode] = newRefs; return(newRefs); }
/// <summary>Notifies all listeners that a Connection is new or discarded. </summary> private void notifyListeners(NuGenConnection c) { for (int i = 0; i < listeners.Count; i++) { NuGenConnectionListener cl = (NuGenConnectionListener)listeners[i]; if (c.Open) { cl.connectionReceived(c); } else { cl.connectionDiscarded(c); } } }
/// <summary>Closes the given connection & removes from hash - to be called when there are 0 references to it </summary> private void close(NuGenConnection c) { c.close(); //remove from "connections" System.Collections.IEnumerator keys = new SupportClass.HashSetSupport(connections.Keys).GetEnumerator(); bool removed = false; while (keys.MoveNext() && !removed) { System.Object key = keys.Current; System.Object val = connections[key]; if (val.GetHashCode() == c.GetHashCode()) { connections.Remove(key); numRefs.Remove(key); removed = true; } } }
/// <summary> Returns a Connection based on an inbound and outbound connection pair from /// the same remote host. This is done by looping through all the connections /// trying to match the host addresses of all posible inbound and outbound /// pairs. When a matching pair is found, both sockets are removed from the /// pending sockets lists, so there should normally be a very small number of /// sockets to search through. This method will return null if the specified /// number of milliseconds has passed, otherwise will wait until a single remote /// host has connected to both the inbound and outbound ports. /// </summary> private NuGenConnection accept(long timeoutMillis) { long startTime = (System.DateTime.Now.Ticks - 621355968000000000) / 10000; NuGenConnection conn = null; while (conn == null && (System.DateTime.Now.Ticks - 621355968000000000) / 10000 < startTime + timeoutMillis) { int i = 0; while (conn == null && i < inSockets.Count) { System.Net.Sockets.TcpClient in_Renamed = (System.Net.Sockets.TcpClient)inSockets[i]; int j = 0; while (conn == null && j < outSockets.Count) { System.Net.Sockets.TcpClient out_Renamed = (System.Net.Sockets.TcpClient)outSockets[j]; System.Net.IPEndPoint end = out_Renamed.Client.RemoteEndPoint as System.Net.IPEndPoint; System.Net.IPEndPoint endin = in_Renamed.Client.RemoteEndPoint as System.Net.IPEndPoint; if (end.Address.Equals(endin.Address)) { conn = new NuGenConnection(parser, llp, in_Renamed, out_Renamed); inSockets.RemoveAt(i); outSockets.RemoveAt(j); } j++; } i++; } try { System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 10)); } catch (System.Threading.ThreadInterruptedException) { } } return(conn); }
/// <summary> Loop that waits for a connection and starts a ConnectionManager /// when it gets one. /// </summary> public override void Run() { try { System.Net.Sockets.TcpListener temp_tcpListener; temp_tcpListener = new System.Net.Sockets.TcpListener(System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0], port); temp_tcpListener.Start(); ss = temp_tcpListener; while (keepRunning()) { try { System.Net.Sockets.TcpClient newSocket = ss.AcceptTcpClient(); NuGenConnection conn = new NuGenConnection(parser, this.llp, newSocket); newConnection(conn); } catch (System.IO.IOException) { //ignore - just timed out waiting for connection } catch (System.Exception) { } } ss.Stop(); } catch (System.Exception) { } finally { //Bug 960113: Make sure HL7Service.stop() is called to stop ConnectionCleaner thread. this.stop(); } }
/// <summary> Closes and discards the given Concection so that it can not be returned /// in subsequent calls to attach(). This method is to be used when there /// is a problem with a Connection, e.g. socket connection closed by remote host. /// </summary> public virtual void discard(NuGenConnection c) { close(c); }
/// <summary> This should be called to indicate that some party is ceasing use of the /// given Connection. /// </summary> /// <returns>s the number of times this Connection is referenced /// </returns> private int decrementRefs(NuGenConnection c) { return(updateRefs(c, -1)); }
/// <summary>Notifies all listeners that a Connection is new or discarded. </summary> private void notifyListeners(NuGenConnection c) { for (int i = 0; i < listeners.Count; i++) { NuGenConnectionListener cl = (NuGenConnectionListener) listeners[i]; if (c.Open) { cl.connectionReceived(c); } else { cl.connectionDiscarded(c); } } }
/// <summary> Creates a new instance of Initiator. </summary> /// <param name="conn">the Connection associated with this Initiator. /// </param> protected internal NuGenInitiator(NuGenConnection conn) { this.conn = conn; }
/// <summary> Opens a connection to the given address, and stores it in the /// connections Hash. /// </summary> private NuGenConnection connect(System.String host, int port, Parser parser, LowerLayerProtocol llp) { System.Net.Sockets.TcpClient s = new System.Net.Sockets.TcpClient(host, port); NuGenConnection i = new NuGenConnection(parser, llp, s); connections[makeHashKey(host, port, parser.GetType(), llp.GetType())] = i; sockets[makeHashKey(host, port, parser.GetType(), llp.GetType())] = s; return i; }
public Grunt(NuGenReceiver enclosingInstance, NuGenConnection conn, System.String message) { InitBlock(enclosingInstance); this.conn = conn; this.m = message; }
/// <summary> This should be called to indicate that some party is ceasing use of the /// given Connection. /// </summary> /// <returns>s the number of times this Connection is referenced /// </returns> private int decrementRefs(NuGenConnection c) { return updateRefs(c, - 1); }
/// <summary>Creates a new instance of Receiver, associated with the given Connection </summary> public NuGenReceiver(NuGenConnection c, HL7Reader in_Renamed) { this.conn = c; this.in_Renamed = in_Renamed; }
/// <summary>Updates the number of references to i - used by incrementRefs and decrementRefs </summary> private int updateRefs(NuGenConnection c, int change) { System.Int32 hashCode = (System.Int32) c.GetHashCode(); System.Object o = numRefs[hashCode]; int existingRefs = 0; if (o != null) { existingRefs = ((System.Int32) o); } System.Int32 newRefs = (System.Int32) (existingRefs + change); numRefs[hashCode] = newRefs; return newRefs; }