Changeset - aa7efaf3fd9b
[Not reviewed]
4 8 0
Christopher Esterhuyse - 5 years ago 2020-04-29 13:16:54
christopher.esterhuyse@gmail.com
finished example 6
12 files changed with 110 insertions and 67 deletions:
0 comments (0 inline, 0 general)
examples/1_socketlike/amy.c
Show inline comments
 
#include <stdio.h>
 
#include <string.h>
 
#include "../../reowolf.h"
 
#include "../utility.c"
 

	
 
int main() { // AMY
 
int main() {
 
	
 
	// amy hard-codes her protocol.
 
	char* pdl =
 
	"primitive forward(in i, out o) {"
 
	"	while(true) synchronous {"
 
	"		put(o, get(i));"
 
	"	}"
 
	"}"
 
	;
 
	
 
	// fill a buffer with the user's message to send.
 
	char msg_buf[128];
 
	memset(msg_buf, 0, 128);
 
	
 
	printf("input a message to send:");
 

	
 
	check("fgets", fgets(msg_buf, 128-1, stdin) == NULL);
 
	int msg_len = strlen(msg_buf);
 
	msg_buf[msg_len-1] = 0;
 
	printf("will send msg `%s`\n", msg_buf);
 
	
 
	// create a connector with one outgoing network channel.
 
	Connector* c = connector_new();
 
	printf("configuring...\n");
 
	check("config ", connector_configure(c, pdl, "forward"));
 
	check("bind 0 ", connector_bind_native(c, 0));
 
	check("bind 1 ", connector_bind_passive(c, 1, "127.0.0.1:7000"));
 
	printf("connecting...\n");
 
	check("connect", connector_connect(c, 5000));
 
	
 
	// send the user-provided message three times
 
	int i;
 
	for (i = 0; i < 3; i++) {
 
		check("put ", connector_put(c, 0, msg_buf, msg_len));
 
		check("sync", connector_sync(c, 1000));
 
		printf("Sent one message!\n");
 
	}
 
	
 
	// clean up
 
	printf("destroying...\n");
 
	connector_destroy(c);
 
	printf("exiting...\n");
 
	return 0;
 
}
 
\ No newline at end of file
examples/1_socketlike/bob.c
Show inline comments
 
#include <stdio.h>
 
#include "../../reowolf.h"
 
#include "../utility.c"
 

	
 
int main() { // BOB!
 
	
 
int main() {
 

	
 
	// bob hard-codes his protocol.
 
	char* pdl =
 
	"primitive forward(in i, out o) {"
 
	"	while(true) synchronous {"
 
	"		put(o, get(i));"
 
	"	}"
 
	"}"
 
	;
 
	
 
	// BOB
 
	// setup a connector with one incoming network channel.
 
	Connector* c = connector_new();
 
	printf("configuring...\n");
 
	check("config ", connector_configure(c, pdl, "forward"));
 
	check("bind 0 ", connector_bind_active(c, 0, "127.0.0.1:7000"));
 
	check("bind 1 ", connector_bind_native(c, 1));
 
	printf("connecting...\n");
 
	check("connect", connector_connect(c, 5000));
 
	
 
	// receive a message and print it to stdout three times
 
	int i;
 
	for (i = 0; i < 3; i++) {
 
		check("get ", connector_get(c, 0));
 
		check("sync", connector_sync(c, 1000));
 

	
 
		int msg_len;
 
		const unsigned char * msg;
 
		check("read", connector_gotten(c, 0, &msg, &msg_len));
 

	
 
		printf("Received one message `%s`!\n", msg);
 
	}
 
	
 

	
 
	// cleanup
 
	printf("destroying...\n");
 
	connector_destroy(c);
 
	printf("exiting...\n");
 
	return 0;
 
}
 
\ No newline at end of file
examples/2_dynamic_pdl/amy.c
Show inline comments
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <string.h>
 
#include "../../reowolf.h"
 
#include "../utility.c"
 

	
 
int main() { // AMY
 
int main() {
 

	
 
	// amy's protocol is loaded at runtime from a file.
 
	char * pdl = buffer_pdl("eg_protocols.pdl");
 
	
 
	char msg_buf[128];
 
	memset(msg_buf, 0, 128);
 
	
 
	printf("input a message to send:");
 

	
 
	check("fgets", fgets(msg_buf, 128-1, stdin) == NULL);
 
	int msg_len = strlen(msg_buf);
 
	msg_buf[msg_len-1] = 0;
 
	printf("will send msg `%s`\n", msg_buf);
 
	
 
	Connector* c = connector_new();
examples/2_dynamic_pdl/bob.c
Show inline comments
 
#include <stdio.h>
 
#include "../../reowolf.h"
 
#include "../utility.c"
 

	
 
int main() { // BOB!
 
int main() {
 

	
 
	// bob's behavior is loaded from a file at runtime
 
	char * pdl = buffer_pdl("eg_protocols.pdl");
 
	
 
	// BOB
 
	Connector* c = connector_new();
 
	printf("configuring...\n");
 
	check("config ", connector_configure(c, pdl, "forward"));
 
	check("bind 0 ", connector_bind_active(c, 0, "127.0.0.1:7000"));
 
	check("bind 1 ", connector_bind_native(c, 1));
 
	printf("connecting...\n");
examples/3_nondeterminism/amy.c
Show inline comments
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <string.h>
 
#include "../../reowolf.h"
 
#include "../utility.c"
 

	
 
int main() { // AMY
 
int main() {
 
	char * pdl = buffer_pdl("eg_protocols.pdl");
 
	
 
	Connector* c = connector_new();
 
	printf("configuring...\n");
 

	
 
	check("config ", connector_configure(c, pdl, "sync"));
 
	check("bind 0 ", connector_bind_native(c, 0));
 
	check("bind 1 ", connector_bind_passive(c, 1, "127.0.0.1:7000"));
 
	printf("connecting...\n");
 
	check("connect", connector_connect(c, 5000));
 

	
 
	// amy offers a message to her peer.
 
	// the message is the number of messages the peer previously received.
 

	
 
	int send_next = 0;
 
	char msg_buf[32];
 
	int code;
 
	int i;
 
	for (i = 0; 1; i++) {
 
		itoa(send_next, msg_buf, 10);
 
		
 
		printf("\nround %d. Will send msg `%s` next", i, msg_buf);
 
		
 
		
 
		// batch 0: no messages sent
 
		// option (a): no messages sent
 
		check("next_batch ", connector_next_batch(c));
 
		
 
		// batch 1: put 0
 
		// option (b): one message sent
 
		check("put ", connector_put(c, 0, msg_buf, strlen(msg_buf) + 1));
 
		code = connector_sync(c, 3000);
 
		
 
		// reflect on the outcome of the exchange
 
		if (code == 0) printf("Sent no message!");
 
		else if (code == 1) {
 
			printf("Sent message `%s`!", msg_buf);
 
			send_next++;
 
		} else {
 
			printf(
examples/3_nondeterminism/bob.c
Show inline comments
 
#include <stdio.h>
 
#include <time.h>
 
#include "../../reowolf.h"
 
#include "../utility.c"
 

	
 
int main() { // BOB!
 
int main() {
 
	char * pdl = buffer_pdl("eg_protocols.pdl");
 
	Connector* c = connector_new();
 

	
 
	Connector* c = connector_new();
 
	printf("configuring...\n");
 
	check("config ", connector_configure(c, pdl, "sync"));
 

	
 
	check("bind 0 ", connector_bind_active(c, 0, "127.0.0.1:7000"));
 
	check("bind 1 ", connector_bind_native(c, 1));
 

	
 
	printf("connecting...\n");
 
	check("connect", connector_connect(c, 5000));
 

	
 
	int msg_len;
 
	int msg_len, i;
 
	const unsigned char * msg;
 

	
 
	int i;
 
	srand(time(NULL));
 
	for (i = 0; i < 10; i++) {
 
		printf("\nround %d...\n", i);
 
		int random = rand() % 2;
 
		if (random == 0) {
 
			printf("I don't want a message!\n");
examples/5_recovery/amy.exe
Show inline comments
 
deleted file
 
binary diff not shown
examples/5_recovery/bob.exe
Show inline comments
 
deleted file
 
binary diff not shown
examples/6_constraint_solve/THIS IS TODO.txt
Show inline comments
 
deleted file
examples/6_constraint_solve/main.c
Show inline comments
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <string.h>
 
#include <time.h>
 
#include <assert.h>
 
#include "../../reowolf.h"
 
#include "../utility.c"
 

	
 
#define N 4
 

	
 
typedef struct PeerInfo {
 
	int id;
 
	bool puts; // true iff the channel to this peer is INCOMING.
 
} PeerInfo;
 

	
 
// return the index of (i,j) in the lexicographic ordering of set {(i,j) : i<j, j<N}
 
// for convenience, swaps (i,j) if i>j 
 
int combination_index(int i, int j) {
 
	if (i > j) {
 
		// swap!
 
		i ^= j;
 
		j ^= i;
 
		i ^= j;
 
	}
 
	assert(0 <= i);
 
	assert(i < j);
 
	assert(j < N);
 
		
 
	int idx_in_square = i*N + j;
 
	int skipped = ((i+1) * (i+2)) / 2;
 
	return idx_in_square - skipped;
 
}
 

	
 
// initializes the given 3-element array,
 
// breaking symmetry with put-get direction.
 
void init_peer_infos(PeerInfo * peer_infos, int my_id) {
 
	int i;
 
	for (i = 0; i < 3; i++) {
 
		PeerInfo * pi = &peer_infos[i];
 
		pi->puts = i < my_id;
 
		pi->id = i < my_id ? i : i+1;
 
		printf("info %d puts=%d id=%d\n", i, pi->puts, pi->id);
 
	}
 
}
 

	
 
const char* addrs[] = {
 
	"127.0.0.1:7000",
 
	"127.0.0.1:7001",
 
	"127.0.0.1:7002",
 
	"127.0.0.1:7003",
 
	"127.0.0.1:7004",
 
	"127.0.0.1:7005",
 
};
 
int main(int arg_c, char * argv[]) {
 
	int index;
 
	int my_id, peer_id, i;
 
	if (arg_c != 2) {
 
		printf("Expected one arg: which peer I am in 0..4");
 
		return 1;
 
	}
 
	index = atoi(argv[1]);
 
	printf("I am peer %d\n", index);
 
	my_id = atoi(argv[1]);
 
	assert(0 <= my_id && my_id < N);
 
	printf("I have id %d\n", my_id);
 
	
 
	const char* addrs[] = {
 
		"127.0.0.1:7000",
 
		"127.0.0.1:7001",
 
		"127.0.0.1:7002",
 
		"127.0.0.1:7003",
 
		"127.0.0.1:7004",
 
		"127.0.0.1:7005",
 
	};
 

	
 
	char * pdl = buffer_pdl("eg_protocols.pdl");
 
	
 
	Connector* c = connector_new();
 
	printf("configuring...\n");
 

	
 
	check("config ", connector_configure(c, pdl, "xor_three"));
 
	int i, j;
 
	int addr_index = 0;
 
	int port = 0;
 
	for (i = 0; i < 4; i++) {
 
		for (j = i+1; j < 4; j++) {
 
			if (i==index) {
 
				printf("ports %d and %d are for a passive channel to peer %d over addr %s\n", port, port+1, j, addrs[addr_index]);
 
				check("bind an ", connector_bind_native(c, port));
 
				port++;
 
				check("bind a  ", connector_bind_active(c, port, addrs[addr_index]));
 
				port++;
 
			} else if (j==index) {
 
				printf("ports %d and %d are for an active channel to peer %d over addr %s\n", port, port+1, i, addrs[addr_index]);
 
				check("bind p  ", connector_bind_passive(c, port, addrs[addr_index]));
 
				port++;
 
				check("bind pn ", connector_bind_native(c, port));
 
				port++;
 
			}
 
			addr_index++;
 
	
 
	PeerInfo peer_infos[3];
 
	init_peer_infos(peer_infos, my_id);
 
	
 
	// for every native port, bind native and protocol port.
 
	for (i = 0; i < 3; i++) {
 
		PeerInfo * pi = &peer_infos[i];
 
		int addr_idx = combination_index(my_id, pi->id);
 
		if (pi->puts) {
 
			check("bind to putter ",
 
				connector_bind_passive(c, i*2, addrs[addr_idx]));
 
			check("bind native ", connector_bind_native(c, i*2 + 1));
 
		} else {
 
			check("bind native ", connector_bind_native(c, i*2));
 
			check("bind to putter ",
 
				connector_bind_active(c, i*2 + 1, addrs[addr_idx]));
 
		}
 
	}
 
	printf("connecting...\n");
 
	check("connect", connector_connect(c, 5000));
 
	
 
	for (i = 0; i < 4; i++) {
 
		if (i == index) continue;
 
		// another batch
 
		for (j = 0; j < 4; j++) {
 
			
 
		}
 
	// for every native port, create a singleton batch
 
	for (i = 0; i < 3; i++) {
 
		if (i > 0) check("next ", connector_next_batch(c));
 
		PeerInfo * pi = &peer_infos[i];
 
		check("op ", pi->puts?
 
			connector_get(c, i):
 
			connector_put(c, i, NULL, 0));
 
	}
 
	connector_sync();
 
	// solve!
 
	int batch_idx = connector_sync(c, 3000);
 
	if (batch_idx < 0) printf("Error code on sync! %d\n", batch_idx);
 
	else printf("I was paired with peer %d\n", peer_infos[batch_idx].id);
 
	
 
	printf("destroying...\n");
 
	connector_destroy(c);
 
	printf("exiting...\n");
 
	free(pdl);
 
	return 0;
examples/6_constraint_solve/main.exe
Show inline comments
 
deleted file
 
binary diff not shown
src/runtime/communication.rs
Show inline comments
 
@@ -97,14 +97,15 @@ impl Controller {
 
            // assign FALSE for all in interface not assigned true
 
            predicate.batch_assign_nones(all_channel_ids.clone(), false);
 

	
 
            if branches.contains_key(&predicate) {
 
                // TODO what do I do with redundant predicates?
 
                unimplemented!(
 
                    "Having multiple batches with the same
 
                    predicate requires the support of oracle boolean variables"
 
                    "Duplicate predicate {:#?}!\nHaving multiple batches with the same
 
                    predicate requires the support of oracle boolean variables",
 
                    &predicate,
 
                )
 
            }
 
            let branch = BranchN { to_get: gets, gotten: Default::default(), sync_batch_index };
 
            for (ekey, payload) in puts {
 
                log!(
 
                    &mut self.inner.logger,
0 comments (0 inline, 0 general)