Пример #1
0
//  Worker using REQ socket to do LRU routing
//
static void *
worker_thread (void *args) {
    void *context = zmq_init (1);
    void *worker = zmq_socket (context, ZMQ_REQ);
    s_set_id (worker);          //  Makes tracing easier
    zmq_connect (worker, "ipc://backend.ipc");

    //  Tell broker we're ready for work
    s_send (worker, "READY");

    while (1) {
        //  Read and save all frames until we get an empty frame
        //  In this example there is only 1 but it could be more
        char *address = s_recv (worker);
        char *empty = s_recv (worker);
        assert (*empty == 0);
        free (empty);

        //  Get request, send reply
        char *request = s_recv (worker);
        printf ("Worker: %s\n", request);
        free (request);

        s_sendmore (worker, address);
        s_sendmore (worker, "");
        s_send     (worker, "OK");
        free (address);
    }
    zmq_close (worker);
    zmq_term (context);
    return NULL;
}
Пример #2
0
static void *
zap_handler (void *zap)
{
    char *version = s_recv (zap);
    char *sequence = s_recv (zap);
    char *domain = s_recv (zap);
    char *address = s_recv (zap);
    char *mechanism = s_recv (zap);
    char *client_key = s_recv (zap);
    
    assert (streq (version, "1.0"));
    assert (streq (mechanism, "CURVE"));

    s_sendmore (zap, version);
    s_sendmore (zap, sequence);
    s_sendmore (zap, "200");
    s_sendmore (zap, "OK");
    s_sendmore (zap, "anonymous");
    s_send (zap, "");
    
    free (version);
    free (sequence);
    free (domain);
    free (address);
    free (mechanism);
    free (client_key);
    
    int rc = zmq_close (zap);
    assert (rc == 0);

    return NULL;
}
Пример #3
0
//  We will do this all in one thread to emphasize the sequence
//  of events...
int main () {
    zmq::context_t context(1);

    zmq::socket_t client (context, ZMQ_XREP);
   client.bind("ipc://routing.ipc");

    zmq::socket_t worker (context, ZMQ_REP);
    worker.setsockopt(ZMQ_IDENTITY, "A", 1);
    worker.connect("ipc://routing.ipc");

    //  Wait for sockets to stabilize
    sleep (1);

    //  Send papa address, address stack, empty part, and request
    s_sendmore (client, "A");
    s_sendmore (client, "address 3");
    s_sendmore (client, "address 2");
    s_sendmore (client, "address 1");
    s_sendmore (client, "");
    s_send     (client, "This is the workload");

    //  Worker should get just the workload
    s_dump (worker);

    //  We don't play with envelopes in the worker
    s_send (worker, "This is the reply");

    //  Now dump what we got off the XREP socket...
    s_dump (client);

    return 0;
}
Пример #4
0
//  We will do this all in one thread to emphasize the sequence
//  of events...
int main () {
    void *context = zmq_init (1);

    void *client = zmq_socket (context, ZMQ_XREP);
    zmq_bind (client, "ipc://routing");

    void *worker = zmq_socket (context, ZMQ_REP);
    zmq_setsockopt (worker, ZMQ_IDENTITY, "A", 1);
    zmq_connect (worker, "ipc://routing");

    //  Wait for sockets to stabilize
    sleep (1);

    //  Send papa address, address stack, empty part, and request
    s_sendmore (client, "A");
    s_sendmore (client, "address 3");
    s_sendmore (client, "address 2");
    s_sendmore (client, "address 1");
    s_sendmore (client, "");
    s_send     (client, "This is the workload");

    //  Worker should get just the workload
    s_dump (worker);

    //  We don't play with envelopes in the worker
    s_send (worker, "This is the reply");

    //  Now dump what we got off the XREP socket...
    s_dump (client);

    zmq_term (context);
    return 0;
}
Пример #5
0
//recRequest by one of the Satellites, reply with number of stored msgs and the oldest stored msg
void ReefServer::recRequest(std::string aka){
	//find satellite by alias in the map
	auto search = satMsgControlMap.find(aka);

	if (search != satMsgControlMap.end()) { //if sat has been found
		//get the Controlnumbers for this satellite
		satelliteMsgControl& msgControl = search->second;
		int msgCount = msgControl.control[1]; //number of stored Messages for this Satellite

		if (!msgCount){
			s_send(rep, std::to_string(msgCount)); //send the number of messages to the satellite
		}
		else{
			s_sendmore(rep, std::to_string(msgCount)); //send the number of messages to the satellite			
			int msgPosition = msgControl.control[0]; //find position of Message Queue in the vector of all Queues

			std::pair<std::string, std::string> msg = satelliteMsgs[msgPosition][0];

			s_sendmore(rep, msg.first); //send the Message to the satellite
			s_send(rep, msg.second);

			satelliteMsgs[msgPosition].erase(satelliteMsgs[msgPosition].begin()); //erase the Message from the Queue
			msgControl.control[1]--; //lower the count of stored Messages by 1	
			CUR_MESSAGES--;
		}
	}
	else{
		s_send(rep, "-1");
	}


}
Пример #6
0
//  Worker using REQ socket to do LRU routing
//
static void *
worker_thread (void *arg) {
	zmq::context_t context(1);
    zmq::socket_t worker (context, ZMQ_REQ);
    s_set_id (worker);          //  Makes tracing easier
    worker.connect("ipc://backend.ipc");

    //  Tell backend we're ready for work
    s_send (worker, "READY");

    while (1) {
        //  Read and save all frames until we get an empty frame
        //  In this example there is only 1 but it could be more
        std::string address = s_recv (worker);
        {
            std::string empty = s_recv (worker);
            assert (empty.size() == 0);
        }

        //  Get request, send reply
        std::string request = s_recv (worker);
        std::cout << "Worker: " << request << std::endl;

        s_sendmore (worker, address);
        s_sendmore (worker, "");
        s_send     (worker, "OK");
    }
    return (NULL);
}
Пример #7
0
static void *
worker_task (void *args)
{
    void *context = zmq_ctx_new ();
    void *worker = zmq_socket (context, ZMQ_REQ);
    s_set_id (worker);          //  Set a printable identity
    zmq_connect (worker, "ipc://backend.ipc");

    //  Tell broker we're ready for work
    s_send (worker, "READY");

    while (1) {
        //  Read and save all frames until we get an empty frame
        //  In this example there is only 1 but it could be more
        char *identity = s_recv (worker);
        char *empty = s_recv (worker);
        assert (*empty == 0);
        free (empty);

        //  Get request, send reply
        char *request = s_recv (worker);
        printf ("Worker: %s\n", request);
        free (request);

        s_sendmore (worker, identity);
        s_sendmore (worker, "");
        s_send     (worker, "OK");
        free (identity);
    }
    zmq_close (worker);
    zmq_ctx_destroy (context);
    return NULL;
}
Пример #8
0
// pubRequest by one of the Satellites, this Server just forwards it by publishing it
bool ReefServer::pubRequest(RMessage& msg){
	msg = RMessage();
	bool retBool;
	std::string tagsStr = s_recv(rep); //receive message tags
	std::string bodyStr = s_recv(rep); //receive messagebody
	CJsonArray tagsArray = jsonToArray(tagsStr); //parse Tags
	tagsInitMessage(msg, tagsArray); //initiate msg with the tags
	tagsArray.Clear();

	retBool = checkInterestAndProcess(msg, bodyStr);
	s_sendmore(publisher, ""); //send empty envelope
	s_sendmore(publisher, tagsStr);	//receive tag-part of message and publish it
	s_send(publisher, bodyStr); //receive body-part of message and publish it
	return retBool;
}
Пример #9
0
int main (void)
{
    void *context = zmq_ctx_new ();
    void *worker = zmq_socket (context, ZMQ_DEALER);
    s_set_id (worker);          //  Set a printable identity
    zmq_connect (worker, "tcp://localhost:5671");

    int total = 0;
    while (1) {
        //  Tell the broker we're ready for work
        s_sendmore (worker, "");
        s_send (worker, "Hi Boss");

        //  Get workload from broker, until finished
        // free (s_recv (worker));     //  Envelope delimiter
        // char *workload = s_recv (worker);
        // int finished = (strcmp (workload, "Fired!") == 0);
        // free (workload);
        // if (finished) {
            // printf ("Completed: %d tasks\n", total);
            // break;
        // }
        total++;

        //  Do some random work
        s_sleep (randof (500) + 1);
    }
    zmq_close (worker);
    zmq_ctx_destroy (context);
    return 0;
}
Пример #10
0
int main() {
    //  Prepare our context and publisher
    zmq::context_t context(1);
    zmq::socket_t publisher(context, ZMQ_REP);
    publisher.bind("tcp://*:5556");

    // Create the fake walk 
    std::string walk[800];

    int i;
    for (i = 0; i < 800; i++) {
        walk[i] = std::to_string(i) + " " + std::to_string(i);
    }

    i = 0;
    while (true) {
        // Just repeat the walk
        if (i >= 800) i = 0;

        // Send envelope 
        s_sendmore(publisher, "pos");
        s_send(publisher, walk[i]);

        // Synchronize with client 
        usleep(10);

        i++;
    }

    return 0;
}
 bool ServerZMQ::_onUpdate()
 {
     if (s_interrupted)
     {
         return false;
     } 
     
     int result;
     const TString& subscriberId = this->getSubscribeId();
     result = s_sendmore(m_socket, subscriberId.c_str());
     if (result == -1)
     {
         DMSG_LOGGER("Error occurred during send subscribe id");
         return false;
     }
     
     const TString& message = getMessageString();
     //TString msg = message;
     //std::replace(msg.begin(), msg.end(), '{', ')');
     //DMSG_LOGGER(message.c_str());
     
     result = s_send(m_socket, message.c_str());
     if (result == -1)
     {
         zmqlog("Error occurred during send message ");
         DMSG_LOGGER("Error sending %s ", message.c_str());
         return false;
     }
     
     //sleep for 1 to 2 seconds
     int sleeptime = (rand() % 1000) + 1000;
     s_sleep(sleeptime);
     
     return true;
 }
Пример #12
0
int main () {
    //  Prepare our context and publisher
    void *context = zmq_init (1);
    void *publisher = zmq_socket (context, ZMQ_PUB);
    zmq_bind (publisher, "tcp://*:5563");

    while (1) {
        //  Write two messages, each with an envelope and content
        s_sendmore (publisher, "A");
        s_send (publisher, "We don't want to see this");
        s_sendmore (publisher, "B");
        s_send (publisher, "We would like to see this");
        sleep (1);
    }
    zmq_term (context);
    return 0;
}
Пример #13
0
void emit(char *jsonstring)
{
    if (config.emit_verbose)
    	printf("%s\n", jsonstring);

    s_sendmore(config.zpublisher, config.ztopic);
    s_send(config.zpublisher, jsonstring);
}
Пример #14
0
void ReefServer::connectRequest(std::string aka, std::string ownIp, std::string reefIp){
	std::string ip_str = "tcp://" + reefIp;
	req.connect(ip_str.c_str());	//connect to 1 Server-Coral of Reef
	s_sendmore(req, "0");
	s_sendmore(req, aka.c_str());	//send wished alias
	s_send(req, ownIp.c_str());		//send adress of own publisher

	//receive the reply
	zmq::message_t reply;
	req.recv(&reply);

	//get the json-string with adress-list of Reef-Corals from the reply
	std::string json = std::string(static_cast<char*>(reply.data()), reply.size());

	//parse the json to a JsonObject and save it as the new adr_list
	adr_list = *CJsonParser::Execute((jstring)json);
}
Пример #15
0
static void 
zap_handler (void *ctx)
{
    //  Create and bind ZAP socket
    void *zap = zmq_socket (ctx, ZMQ_REP);
    assert (zap);
    int rc = zmq_bind (zap, "inproc://zeromq.zap.01");
    assert (rc == 0);

    //  Process ZAP requests forever
    while (true) {
        char *version = s_recv (zap);
        if (!version)
            break;          //  Terminating
            
        char *sequence = s_recv (zap);
        char *domain = s_recv (zap);
        char *address = s_recv (zap);
        char *identity = s_recv (zap);
        char *mechanism = s_recv (zap);

        assert (streq (version, "1.0"));
        assert (streq (mechanism, "NULL"));
        //  TODO: null_mechanism.cpp issues ZAP requests for connections other 
        //  than the expected one. In these cases identity is not set, and the
        //  test fails. We'd expect one ZAP request per real client connection.
//         assert (streq (identity, "IDENT"));

        s_sendmore (zap, version);
        s_sendmore (zap, sequence);
        s_sendmore (zap, "200");
        s_sendmore (zap, "OK");
        s_sendmore (zap, "anonymous");
        s_send (zap, "");
        
        free (version);
        free (sequence);
        free (domain);
        free (address);
        free (identity);
        free (mechanism);
    }
    rc = zmq_close (zap);
    assert (rc == 0);
}
Пример #16
0
void str_send_to (void *s_, const char *content_, const char *address_)
{
    //  Send the address part
    int rc = s_sendmore (s_, address_);
    assert (rc > 0);

    rc = s_send (s_, content_);
    assert (rc > 0);
}
Пример #17
0
//  .split main task
//  While this example runs in a single process, that is just to make
//  it easier to start and stop the example. Each thread has its own
//  context and conceptually acts as a separate process.
int main() {
    zmq::context_t context(1);
    zmq::socket_t broker(context, ZMQ_ROUTER);

    broker.bind("tcp://*:5671");
    srandom((unsigned)time(NULL));

    const int NBR_WORKERS = 10;
    pthread_t workers[NBR_WORKERS];
    for (int worker_nbr = 0; worker_nbr < NBR_WORKERS; ++worker_nbr) {
        pthread_create(workers + worker_nbr, NULL, worker_task, (void *)(intptr_t)worker_nbr);
    }


    //  Run for five seconds and then tell workers to end
    int64_t end_time = s_clock() + 5000;
    int workers_fired = 0;
    while (1) {
        //  Next message gives us least recently used worker
        std::string identity = s_recv(broker);
        {
            s_recv(broker);     //  Envelope delimiter
            s_recv(broker);     //  Response from worker
        }

        s_sendmore(broker, identity);
        s_sendmore(broker, "");

        //  Encourage workers until it's time to fire them
        if (s_clock() < end_time)
            s_send(broker, "Work harder");
        else {
            s_send(broker, "Fired!");
            if (++workers_fired == NBR_WORKERS)
                break;
        }
    }

    for (int worker_nbr = 0; worker_nbr < NBR_WORKERS; ++worker_nbr) {
        pthread_join(workers[worker_nbr], NULL);
    }

    return 0;
}
Пример #18
0
static void
zap_handler (void *ctx)
{
    //  Create and bind ZAP socket
    void *zap = zmq_socket (ctx, ZMQ_REP);
    assert (zap);
    int rc = zmq_bind (zap, "inproc://zeromq.zap.01");
    assert (rc == 0);

    //  Process ZAP requests forever
    while (true) {
        char *version = s_recv (zap);
        if (!version)
            break;          //  Terminating
        char *sequence = s_recv (zap);
        char *domain = s_recv (zap);
        char *address = s_recv (zap);
        char *identity = s_recv (zap);
        char *mechanism = s_recv (zap);
        char *username = s_recv (zap);
        char *password = s_recv (zap);

        assert (streq (version, "1.0"));
        assert (streq (mechanism, "PLAIN"));
        assert (streq (identity, "IDENT"));

        s_sendmore (zap, version);
        s_sendmore (zap, sequence);
        if (streq (username, "admin")
        &&  streq (password, "password")) {
            s_sendmore (zap, "200");
            s_sendmore (zap, "OK");
            s_sendmore (zap, "anonymous");
            s_send (zap, "");
        }
        else {
            s_sendmore (zap, "400");
            s_sendmore (zap, "Invalid username or password");
            s_sendmore (zap, "");
            s_send (zap, "");
        }
        free (version);
        free (sequence);
        free (domain);
        free (address);
        free (identity);
        free (mechanism);
        free (username);
        free (password);
    }
    rc = zmq_close (zap);
    assert (rc == 0);
}
Пример #19
0
int main(void) {
    void *context = zmq_init(1);
    void *client = zmq_socket(context, ZMQ_ROUTER);
    zmq_bind(client, "ipc://routing.ipc");
    srandom((unsigned) time(NULL));

    int worker_nbr;
    for (worker_nbr = 0; worker_nbr < NBR_WORKERS; worker_nbr++) {
        pthread_t worker;
        pthread_create(&worker, NULL, worker_task, NULL);
    }
    int task_nbr;
    for (task_nbr = 0; task_nbr < NBR_WORKERS * 10; task_nbr++) {
        // 最近最少使用的worker就在消息队列中
        char *address = s_recv(client);
        char *empty = s_recv(client);
        free(empty);
        char *ready = s_recv(client);
        free(ready);

        s_sendmore(client, address);
        s_sendmore(client, "");
        s_send(client, "This is the workload");
        free(address);
    }
    // 通知所有REQ套接字结束工作
    for (worker_nbr = 0; worker_nbr < NBR_WORKERS; worker_nbr++) {
        char *address = s_recv(client);
        char *empty = s_recv(client);
        free(empty);
        char *ready = s_recv(client);
        free(ready);

        s_sendmore(client, address);
        s_sendmore(client, "");
        s_send(client, "END");
        free(address);
    }
    zmq_close(client);
    zmq_term(context);
    return 0;
}
Пример #20
0
int main () {
    void *context = zmq_init (1);
    void *client = zmq_socket (context, ZMQ_XREP);
    zmq_bind (client, "ipc://routing.ipc");
    srandom ((unsigned) time (NULL));

    int worker_nbr;
    for (worker_nbr = 0; worker_nbr < NBR_WORKERS; worker_nbr++) {
        pthread_t worker;
        pthread_create (&worker, NULL, worker_thread, context);
    }
    int task_nbr;
    for (task_nbr = 0; task_nbr < NBR_WORKERS * 10; task_nbr++) {
        //  LRU worker is next waiting in queue
        char *address = s_recv (client);
        char *empty = s_recv (client);
        free (empty);
        char *ready = s_recv (client);
        free (ready);

        s_sendmore (client, address);
        s_sendmore (client, "");
        s_send (client, "This is the workload");
        free (address);
    }
    //  Now ask mamas to shut down and report their results
    for (worker_nbr = 0; worker_nbr < NBR_WORKERS; worker_nbr++) {
        char *address = s_recv (client);
        char *empty = s_recv (client);
        free (empty);
        char *ready = s_recv (client);
        free (ready);

        s_sendmore (client, address);
        s_sendmore (client, "");
        s_send (client, "END");
        free (address);
    }
    sleep (1);              //  Give 0MQ/2.0.x time to flush output
    zmq_term (context);
    return 0;
}
Пример #21
0
int main () {
    zmq::context_t context(1);
    zmq::socket_t client (context, ZMQ_XREP);
    client.bind("ipc://routing.ipc");

    int worker_nbr;
    for (worker_nbr = 0; worker_nbr < NBR_WORKERS; worker_nbr++) {
        pthread_t worker;
        pthread_create (&worker, NULL, worker_thread, &context);
    }
    int task_nbr;
    for (task_nbr = 0; task_nbr < NBR_WORKERS * 10; task_nbr++) {
        //  LRU worker is next waiting in queue
        std::string address = s_recv (client);
        {
            // receiving and discarding'empty' message
            s_recv (client);
            // receiving and discarding 'ready' message
            s_recv (client);
        }

        s_sendmore (client, address);
        s_sendmore (client, "");
        s_send (client, "This is the workload");
    }
    //  Now ask mamas to shut down and report their results
    for (worker_nbr = 0; worker_nbr < NBR_WORKERS; worker_nbr++) {
        std::string address = s_recv (client);
        {
            // receiving and discarding'empty' message
            s_recv (client);
            // receiving and discarding 'ready' message
            s_recv (client);
        }

        s_sendmore (client, address);
        s_sendmore (client, "");
        s_send (client, "END");
    }
    sleep (1);              //  Give 0MQ/2.0.x time to flush output
    return 0;
}
Пример #22
0
int main (void)
{
    void *context = zmq_ctx_new ();
    void *responder = zmq_socket (context, ZMQ_ROUTER);
    int rc = zmq_bind (responder, "tcp://*:5555");
    assert (rc == 0);
    
    while (1)
	{
        char identity[10]={0};
        //recv client`s request msg
        //the 1st received frame is identity frame
        char *frame1=s_recv(responder);
        printf("[recv] frame1 = %s\n",frame1);
        memcpy(identity, frame1, strlen(frame1)+1);  //save the identity to char array identity
        free(frame1);
        //the 2nd frame received is empty delimiter frame
        char *frame2= s_recv(responder);
        printf("[recv] frame2 = %s\n", frame2);
        assert(frame2[0] == 0);
        free(frame2);
        //the 3rd frame received is data frame
        char *frame3=s_recv(responder);
        printf("[recv] frame3 = %s\n", frame3);
        free(frame3);

        sleep (1);   //Do some work

        //send respond msg to client
        printf("[send] frame1 = %s\n", identity);
        printf("[send] frame2 = %s\n", "");
        printf("[send] frame3 = %s\n\n", "World");
        s_sendmore(responder,identity);  //send identity frame
        s_sendmore(responder,"");        //send empty delimiter frame
        s_send (responder, "World");     //send data frame
        memset(identity, 0, sizeof(identity));  //clear up identity array
    }

    zmq_close(responder);
    zmq_ctx_destroy(context);
    return 0;
}
Пример #23
0
int main (void)
{
    //  Prepare our context and publisher
    void *context = zmq_ctx_new ();
    void *publisher = zmq_socket (context, ZMQ_PUB);
    zmq_bind (publisher, "tcp://*:5563");

    while (1) {
        //  Write two messages, each with an envelope and content
        s_sendmore (publisher, "A");
        s_send (publisher, "We don't want to see this");
        s_sendmore (publisher, "B");
        s_send (publisher, "We would like to see this");
        sleep (1);
    }
    //  We never get here but clean up anyhow
    zmq_close (publisher);
    zmq_ctx_destroy (context);
    return 0;
}
Пример #24
0
//  We will do this all in one thread to emphasize the sequence
//  of events...
int main (void) 
{
    void *context = zmq_init (1);

    void *client = zmq_socket (context, ZMQ_ROUTER);
    zmq_bind (client, "ipc://routing.ipc");

    void *worker = zmq_socket (context, ZMQ_REP);
    zmq_setsockopt (worker, ZMQ_IDENTITY, "A", 1);
    zmq_connect (worker, "ipc://routing.ipc");

    //  Wait for the worker to connect so that when we send a message
    //  with routing envelope, it will actually match the worker...
    sleep (1);

    //  Send papa address, address stack, empty part, and request
    s_sendmore (client, "A");
    s_sendmore (client, "address 3");
    s_sendmore (client, "address 2");
    s_sendmore (client, "address 1");
    s_sendmore (client, "");
    s_send     (client, "This is the workload");

    //  Worker should get just the workload
	printf("begine dump worker\n");
    s_dump (worker);

    //  We don't play with envelopes in the worker
    s_send (worker, "This is the reply");

    //  Now dump what we got off the ROUTER socket...
	printf("begine dump client\n");
    s_dump (client);

    zmq_close (client);
    zmq_close (worker);
    zmq_term (context);
    return 0;
}
Пример #25
0
// .split worker task
// While this example runs in a single process, that is just to make
// it easier to start and stop the example. Each thread has its own
// context and conceptually acts as a separate process.
// This is the worker task, using a REQ socket to do load-balancing.
// Because s_send and s_recv can't handle 0MQ binary identities, we
// set a printable text identity to allow routing.
static void *worker_task(void *args)
{
    void *context = zmq_ctx_new();
    void *worker = zmq_socket(context, ZMQ_REQ);

#if (defined (WIN32))
    s_set_id(worker, (intptr_t)args);
    zmq_connect(worker, "tcp://localhost:5673"); // backend
#else
    s_set_id(worker);
    zmq_connect(worker, "ipc://backend.ipc");
#endif

    // Tell broker we're ready for work
    s_send(worker, "READY");

    while (1) {
        // Read and save all frames until we get an empty frame
        // In this example there is only 1, but there could be more
        char *identity = s_recv(worker);
        char *empty = s_recv(worker);
        assert(*empty == 0);
        free(empty);

        // Get request, send reply
        char *request = s_recv(worker);
        printf("Worker: %s from %s\n", request, identity);
        free(request);

        s_sendmore(worker, identity);
        s_sendmore(worker, "");
        s_send(worker, "OK");
        free(identity);
    }

    zmq_close(worker);
    zmq_ctx_destroy(context);
    return NULL;
}
Пример #26
0
static void zap_handler (void *handler)
{
    //  Process ZAP requests forever
    while (true) {
        char *version = s_recv (handler);
        if (!version)
            break;          //  Terminating

        char *sequence = s_recv (handler);
        char *domain = s_recv (handler);
        char *address = s_recv (handler);
        char *identity = s_recv (handler);
        char *mechanism = s_recv (handler);
        uint8_t client_key [32];
        int size = zmq_recv (handler, client_key, 32, 0);
        assert (size == 32);

        char client_key_text [40];
        zmq_z85_encode (client_key_text, client_key, 32);

        assert (streq (version, "1.0"));
        assert (streq (mechanism, "CURVE"));
        assert (streq (identity, "IDENT"));

        s_sendmore (handler, version);
        s_sendmore (handler, sequence);

        if (streq (client_key_text, client_public)) {
            s_sendmore (handler, "200");
            s_sendmore (handler, "OK");
            s_sendmore (handler, "anonymous");
            s_send     (handler, "");
        }
        else {
            s_sendmore (handler, "400");
            s_sendmore (handler, "Invalid client public key");
            s_sendmore (handler, "");
            s_send     (handler, "");
        }
        free (version);
        free (sequence);
        free (domain);
        free (address);
        free (identity);
        free (mechanism);
    }
    zmq_close (handler);
}
Пример #27
0
/*
 *  ZAP request handling loop.
 */
static void zap_handler(void *handler)
{
    for (;;) {
        char *version, *sequence, *domain, *address, *identity, *mechanism;

        /* Receive request. */
        if(!(version = s_tryrecv(handler)))
            break;
        sequence = s_recvmore(handler);
        domain = s_recvmore(handler);
        address = s_recvmore(handler);
        identity = s_recvmore(handler);
        mechanism = s_recvmore(handler);
        assert(!has_more(handler));

        assert(!strcmp(version, "1.0"));
        assert(!strcmp(mechanism, "NULL"));

        printf("ZAP REQUEST %s %s %s %s %s %s\n", version, sequence, domain,
                address, identity, mechanism);
        
        /* Send reply. */
        s_sendmore(handler, version);
        s_sendmore(handler, sequence);
        if(!strcmp(domain, "test")) {
            s_sendmore(handler, "200");
            s_sendmore(handler, "OK");
            s_sendmore(handler, "anonymous");
            printf("ZAP REPLY 1.0 %s 200 OK anonymous\n", sequence);
        } else {
            s_sendmore(handler, "500");
            s_sendmore(handler, "Denied");
            s_sendmore(handler, "");
            printf("ZAP REPLY 1.0 %s 200 Denied\n", sequence);
        }
        s_send(handler, "");

        /* Cleanup. */
        free(version);
        free(sequence);
        free(domain);
        free(address);
        free(identity);
        free(mechanism);
    }
    zmq_close(handler);
}
Пример #28
0
int main (void)
{
    void *context = zmq_init (1);
    void *client = zmq_socket (context, ZMQ_ROUTER);
    zmq_bind (client, "ipc://routing.ipc");

    pthread_t worker;
    pthread_create (&worker, NULL, worker_task_a, NULL);
    pthread_create (&worker, NULL, worker_task_b, NULL);

    //  Wait for threads to connect, since otherwise the messages
    //  we send won't be routable.
    sleep (1);

    //  Send 10 tasks scattered to A twice as often as B
    int task_nbr;
    srandom ((unsigned) time (NULL));
    for (task_nbr = 0; task_nbr < 10; task_nbr++) {
        //  Send two message parts, first the address...
        if (randof (3) > 0)
            s_sendmore (client, "A");
        else
            s_sendmore (client, "B");

        //  And then the workload
        s_send (client, "This is the workload");
    }
    s_sendmore (client, "A");
    s_send     (client, "END");

    s_sendmore (client, "B");
    s_send     (client, "END");

    zmq_close (client);
    zmq_term (context);
    return 0;
}
Пример #29
0
static void
zap_handler (void *handler)
{
    uint8_t metadata [] = {
        5, 'H', 'e', 'l', 'l', 'o',
        0, 0, 0, 5, 'W', 'o', 'r', 'l', 'd'
    };

    //  Process ZAP requests forever
    while (true) {
        char *version = s_recv (handler);
        if (!version)
            break;          //  Terminating

        char *sequence = s_recv (handler);
        char *domain = s_recv (handler);
        char *address = s_recv (handler);
        char *routing_id = s_recv (handler);
        char *mechanism = s_recv (handler);

        assert (streq (version, "1.0"));
        assert (streq (mechanism, "NULL"));

        s_sendmore (handler, version);
        s_sendmore (handler, sequence);
        if (streq (domain, "DOMAIN")) {
            s_sendmore (handler, "200");
            s_sendmore (handler, "OK");
            s_sendmore (handler, "anonymous");
            zmq_send (handler, metadata, sizeof (metadata), 0);
        }
        else {
            s_sendmore (handler, "400");
            s_sendmore (handler, "BAD DOMAIN");
            s_sendmore (handler, "");
            s_send     (handler, "");
        }
        free (version);
        free (sequence);
        free (domain);
        free (address);
        free (routing_id);
        free (mechanism);
    }
    close_zero_linger (handler);
}
Пример #30
0
int main22 () {

    zmq::context_t context(1);

    zmq::socket_t client (context, ZMQ_ROUTER);
    client.bind("tcp://*:5555");

    pthread_t worker;
    pthread_create (&worker, NULL, worker_a, &context);
    pthread_create (&worker, NULL, worker_b, &context);

    //  Wait for threads to stabilize
    s_sleep (1000);

    //  Send 10 tasks scattered to A twice as often as B
    int task_nbr;
    srand ((unsigned) time (NULL));
    for (task_nbr = 0; task_nbr < 100; task_nbr++) {
        //  Send two message parts, first the address...
        if (within (3) > 0)
            s_sendmore (client, "A");
        else
            s_sendmore (client, "B");

        //  And then the workload
        s_send (client, "This is the workload");
    }
    s_sendmore (client, "A");
    s_send     (client, "END");

    s_sendmore (client, "B");
    s_send     (client, "END");

    s_sleep (1000);              //  Give 0MQ/2.0.x time to flush output
    return 0;
}