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
/*
 * pos.cpp
 *
 *  Created on: Oct 2, 2015
 *      Author: A.Mulawa
 */

#include <algorithm>
#include <string>
#include <iostream>

#include <vector>

#include "message.h"
#include "poszukiwania.h"

using std::cout;
using std::endl;

class pos
{
public:
    long long S;
    long long M;
    int node_id;
    int instances;
    int msg_counter;
    int max_p;
    int step;

    pos()
    {
        S = SignalLength();
        M = SeqLength();
        node_id = MyNodeId();
        instances = NumberOfNodes();
        msg_counter = 0;
        max_p = M - S;
        step = (max_p / instances / 2) - 1;
        if (step < 0)
            step = 1;
    }

    void send(int receiver, long msg)
    {
        msg_counter++;
        PutLL(receiver, msg);
        Send(receiver);
    }

    void server(int counter_p)
    {
        bool loop = true;

        int client = 1;

        int started = 0;
        int result = 0;
        while (client < instances && counter_p < max_p)
        {
            send(client, counter_p);
            started++;
            counter_p += step;
            client++;
        }
        while (loop)
        {
            client = Receive(-1);
            long msg = GetLL(client);
            started--;
            if (msg > 0)
            {
                result+=msg;
            }
            if (counter_p < max_p)
            {
                started++;
                send(client, counter_p);
                counter_p += step;

            }
            if (started == 0)
            {
                loop = false;
            }
        }
        client = 1;
        while (client < instances)
        {
            send(client, -1);
            client++;
        }

        cout << result << endl;
    }

    void client()
    {
        bool loop = true;
        long msg;
        while (loop)
        {
            int server = Receive(-1);
            msg = GetLL(server);
            if (msg == -1)
            {
                loop = false;
            }
            else
            {
                bool found;
                long counter = 0;

                int middle = S / 2 + 1;
                int end = S;
                int kend = msg + step;

                for (int i = msg; i < kend; i++)
                {
                    if (i >= M)
                    {
                        break;
                    }
                    if ( (SignalAt(1) == SeqAt(i + 1)))
                      if(SignalAt(middle) == SeqAt(i + middle))
                         if(SignalAt(end) == SeqAt(i + end))
                         {
				  		   found = true;
			               for (int j = 1; j <= S; j++)
			               {
		                       if (SignalAt(j) != SeqAt(i+j))
		                       {
	                                found = false;
  	                                break;
		                       }
		                   }
		                   if (found)
                           {
                              counter++;
                           }
						 }
                }
                send(server, counter > 0 ? counter : -1);
            }
        }
    }

    void search()
    {
        if (node_id == 0)
        {
            server(0);
        }
        else
        {
            client();
        }
    }

    ~pos()
    {
    }
};

int main(int argc, char **argv)
{
    pos s;
    s.search();
    return 0;
}