public static void Main(string[] args) { TupleSpace ts = new TupleSpace("localhost", 2525, new String[] {"TestSpace"}); Tuple t; Console.WriteLine("Connected to TS " + ts); t = new Tuple(new Field[] { new Field("Hallo"), new Field("Welt") }); ts.Write(t); Console.WriteLine("Written tuple " + t); //t = new Tuple(new Field[] {new Field("Hello"), new Field("World")}); //ts.Write(t); //Console.WriteLine ("Written tuple " + t); //t = new Tuple(new Field[] {new Field("Czesc"), new Field("Swiat")}); //ts.Write(t); //Console.WriteLine ("Written tuple " + t); Tuple template = new Tuple(new Field[] {new Field(typeof(String)), new Field(typeof(String))}); //Take //t = ts.Take(template); //Console.WriteLine("Tuple Read: " + t); //Ler tudo //Tuple[] tups = ts.ReadAll(template); //foreach (Tuple tu in tups) //{ // Console.WriteLine("---> Tuple Read: " + tu); //} //Read t = ts.Read(template); Console.WriteLine("Tuple Read: " + t); int num = ts.Count(template); Console.WriteLine (num + " tuples found!"); //bool deleted = ts.Delete(template); //Console.WriteLine ("Deleted one tuple? " + deleted); //num = ts.Count(template); //Console.WriteLine (num + " tuples found!"); //int deletedSomething = ts.DeleteAll(template); //Console.WriteLine ("Deleted something? " + deletedSomething); Console.WriteLine ("BYE"); }
public bool Delete(Tuple tuple, string[] spaces) { return Delete(tuple, spaces, true); }
public object Clone() { Field[] newFields = new Field[fields.Length]; for (int i = 0; i < fields.Length; i++) { newFields[i] = (Field)fields[i].Clone(); } Tuple t = new Tuple(newFields); t.Expiration = expiration; return t; }
public TupleID Write(Tuple tuple, String space) { int spaceid = int.MinValue; for (int i = 0; i < spaces.Length && spaceid == int.MinValue; i++) { if (spaces[i].Name.Equals(space)) { spaceid = spaces[i].Id; } } if (spaceid == int.MinValue) { throw new TupleSpaceException("not connected to space '" + space + "'"); } TSResponse resp = SendReceive(new WriteCommand(tuple, spaceid)); if (resp.Type.Equals(TSResponse.ResponseType.answer)) { long idStr = (long)resp.GetTuple(0).GetField(0).Value; return new TupleID(idStr); } else { return null; } }
public Tuple WaitToTake(Tuple t, long timeout, string[] spaces) { int[] spaceIds = GetSpaceIds(spaces); TSResponse resp = SendReceive(new QueryCommand(t, false, true, true, timeout, spaceIds)); if (resp.Type.Equals(TSResponse.ResponseType.answer) && resp.NumberOfTuples != 0) { return resp.GetTuple(0); } else { return null; } }
public Tuple WaitToRead(Tuple t, string[] spaces) { return WaitToRead(t, 0, spaces); }
public bool Update(TupleID tupleId, Tuple tuple) { TSResponse resp = SendReceive(new UpdateCommand(tupleId, tuple)); return resp.Type.Equals(TSResponse.ResponseType.ok); }
public Tuple[] TakeAll(Tuple t, string[] spaces) { int[] spaceIds = GetSpaceIds(spaces); TSResponse resp = SendReceive(new QueryCommand(t, true, true, false, 0, spaceIds)); if (resp.Type.Equals(TSResponse.ResponseType.answer)) { return resp.Tuples; } else { return null; } }
public Tuple Read(Tuple t) { return Read(t, GetSpaceNames(), true); }
public int EventRegister(Callback.Command command, Tuple template, int major, int minor, int version, String user, Callback cback, bool newThread) { TSResponse resp = SendReceive( new CallbackCommand(command, template, null, major, minor, version, user, int.MinValue, CallbackCommand.CallbackType.event_register)); if (resp.Type.Equals(TSResponse.ResponseType.answer)) { int seq = (int) resp.GetTuple(0).GetField(0).Value; responseThread.addCallback(cback, seq, newThread); return seq; } else { return int.MinValue; } }
public int EventRegister(Callback.Command command, Tuple template, Callback cback, bool newThread) { if (spaces.Length == 1) { return EventRegister(command, template, spaces[0].Version.Major, spaces[0].Version.Minor, null, cback, newThread); } else { throw new TupleSpaceException("no space given (connected to multiple spaces)"); } }
public int EventRegister(Callback.Command command, Tuple template, int major, int minor, String user, Callback cback, bool newThread) { return EventRegister(command, template, spaces[0].Version.Major, spaces[0].Version.Minor, int.MinValue, null, cback, newThread); }
public int DeleteAll(Tuple tuple, string[] spaces) { int[] spaceIds = GetSpaceIds(spaces); TSResponse resp = SendReceive(new QueryCommand(tuple, true, true, false, 0, spaceIds, false, false)); if (resp.Type.Equals(TSResponse.ResponseType.answer) && resp.NumberOfTuples != 0) { Tuple r = resp.GetTuple(0); if (r != null && r.GetField(0) != null && r.GetField(0).Value.GetType().Equals(typeof (int))) { return (int) r.GetField(0).Value; } return int.MinValue; } else { return int.MinValue; } }
public int DeleteAll(Tuple tuple) { return DeleteAll(tuple, GetSpaceNames()); }
public bool Delete(Tuple tuple, bool randomize) { return Delete(tuple, GetSpaceNames(), randomize); }
public Tuple Take(Tuple t, string[] spaces, bool randomize) { int[] spaceIds = GetSpaceIds(spaces); TSResponse resp = SendReceive(new QueryCommand(t, false, true, false, 0, spaceIds, randomize, true)); if (resp.Type.Equals(TSResponse.ResponseType.answer) && resp.NumberOfTuples != 0) { Tuple r = resp.GetTuple(0); return r; } else { return null; } }
public Tuple[] TakeAll(Tuple t) { return TakeAll(t, GetSpaceNames()); }
public Tuple Read(Tuple t, bool randomize) { return Read(t, GetSpaceNames(), randomize); }
public Tuple TakeTupleById(TupleID id) { Tuple t = new Tuple(new Field[] {}); t.TupleID = id; return Take(t); }
public Tuple Read(Tuple t, string[] spaces) { return Read(t, spaces, true); }
public Tuple WaitToRead(Tuple t) { return WaitToRead(t, 0, GetSpaceNames()); }
public Tuple[] ReadAll(Tuple t) { return ReadAll(t, GetSpaceNames()); }
public Tuple WaitToTake(Tuple t) { return WaitToTake(t, 0, GetSpaceNames()); }
public Tuple ReadTupleById(TupleID id) { Tuple t = new Tuple(new Field[] {}); t.TupleID = id; return Read(t); }
public TupleID Write(Tuple tuple) { if (spaces.Length == 1) { return Write(tuple, spaces[0].Name); } else { throw new TupleSpaceException("no space given to write into (connected to multiple spaces)"); } }
public Tuple Take(Tuple t) { return Take(t, true); }
public Tuple Take(Tuple t, bool randomize) { return Take(t, GetSpaceNames(), randomize); }
public Tuple Take(Tuple t, string[] spaces) { return Take(t, spaces, true); }
public bool Matches(Tuple t) { if (fields.Length == 0 || t.NumerOfFields == 0) { // nulltuple always matches return true; } if (fields.Length != t.NumerOfFields) { return false; } else { for (int i = 0; i < fields.Length; i++) { if (!fields[i].Matches(t.GetField(i))) { return false; } } } return true; }
public bool Delete(Tuple tuple) { return Delete(tuple, true); }