public override bool Equals(object obj) { if (Object.ReferenceEquals(obj, this)) { return(true); } Haiku haiku = obj as Haiku; if (haiku == null) { return(false); } if (_phrases.Length != haiku._phrases.Length) { return(false); } for (int i = 0; i < _phrases.Length; i++) { if (!String.Equals(_phrases[i], haiku._phrases[i])) { return(false); } } return(true); }
public void Validate(Haiku haiku) { String[] phrases = haiku.Phrases; for (int i = 0; i < phrases.Length; i++) { String phrase = phrases[i]; int count = PhraseUtilities.CountSyllablesInPhrase(phrase); if (count != SYLLABLE_COUNTS[i]) { throw new InvalidHaikuException(i + 1, phrase, count, SYLLABLE_COUNTS[i]); } } }
static void Main(string[] args) { /* * ReuseBuffer needs to be false since we have a ExecutorFilter before * ProtocolCodecFilter which processes incoming IoBuffer. */ IoAcceptor acceptor = new AsyncSocketAcceptor() { ReuseBuffer = false }; acceptor.FilterChain.AddLast("logger", new LoggingFilter()); acceptor.FilterChain.AddLast("executor", new ExecutorFilter()); acceptor.FilterChain.AddLast("to-string", new ProtocolCodecFilter( new TextLineCodecFactory(Encoding.UTF8))); acceptor.FilterChain.AddLast("to-haiki", new ToHaikuIoFilter()); acceptor.ExceptionCaught += (s, e) => e.Session.Close(true); HaikuValidator validator = new HaikuValidator(); acceptor.MessageReceived += (s, e) => { Haiku haiku = (Haiku)e.Message; try { validator.Validate(haiku); e.Session.Write("HAIKU!"); } catch (InvalidHaikuException ex) { e.Session.Write("NOT A HAIKU: " + ex.Message); } }; acceptor.Bind(new IPEndPoint(IPAddress.Any, port)); Console.WriteLine("Listening on " + acceptor.LocalEndPoint); Console.ReadLine(); }