Created
December 10, 2019 00:02
-
-
Save monjasa/d63c9eb5685d8e33b53c1088d54226e0 to your computer and use it in GitHub Desktop.
OS-11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.mappedbus.sample.object; | |
public class Book { | |
private String title; | |
public Book(String title) { | |
this.title = title; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.mappedbus.sample.object; | |
import io.mappedbus.MappedBusMessage; | |
import io.mappedbus.MappedBusReader; | |
import java.io.File; | |
import java.io.PrintWriter; | |
import java.util.ArrayList; | |
import java.util.concurrent.Semaphore; | |
public class BookShop { | |
private static final int SHOP_CAPACITY = 10; | |
private static final Semaphore SEMAPHORE = new Semaphore(1, true); | |
private static ArrayList<Book> books; | |
public static void main(String[] args) { | |
BookShop server = new BookShop(); | |
server.books = new ArrayList<>(SHOP_CAPACITY); | |
server.run(); | |
} | |
public void run() { | |
books.add(new Book("Biased")); | |
books.add(new Book("Never again")); | |
books.add(new Book("Never again II")); | |
try { | |
PrintWriter writer = new PrintWriter("messages", "UTF-8"); | |
writer.close(); | |
MappedBusReader reader = new MappedBusReader("messages", 2000000L, 100); | |
reader.open(); | |
DemandedBook demandedBook = new DemandedBook(); | |
BookToSell bookToSell = new BookToSell(); | |
MappedBusMessage message = null; | |
while (true) { | |
if (reader.next()) { | |
int type = reader.readType(); | |
switch (type) { | |
case DemandedBook.TYPE: | |
message = demandedBook; | |
reader.readMessage(message); | |
String bookTitle = ((DemandedBook) message).getTitle(); | |
new Thread(new BookSeller(bookTitle)).start(); | |
break; | |
case BookToSell.TYPE: | |
message = bookToSell; | |
reader.readMessage(message); | |
Book book = ((BookToSell) message).getBook(); | |
new Thread(new BookAdder(book)).start(); | |
break; | |
default: | |
throw new RuntimeException("Unknown type: " + type); | |
} | |
} | |
} | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
class BookAdder implements Runnable { | |
Book book; | |
public BookAdder(Book book) { | |
this.book = book; | |
} | |
@Override | |
public void run() { | |
try { | |
System.out.println(String.format("Author is trying to sell their book entitled as \"%s\"...", book.getTitle())); | |
BookShop.SEMAPHORE.acquire(); | |
if (BookShop.books.size() == SHOP_CAPACITY) { | |
System.out.println("All bookshelves are full. Please, try to add \"" + book.getTitle() + "\" again later..."); | |
} else { | |
BookShop.books.add(book); | |
System.out.println("The book \"" + book.getTitle() + "\" has been successfully added to the range of the book store!"); | |
} | |
BookShop.SEMAPHORE.release(); | |
System.out.println("Author left the book store.\n"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
class BookSeller implements Runnable { | |
String bookTitle; | |
public BookSeller(String bookTitle) { | |
this.bookTitle = bookTitle; | |
} | |
@Override | |
public void run() { | |
try { | |
System.out.println(String.format("Customer is looking for \"%s\"...", bookTitle)); | |
BookShop.SEMAPHORE.acquire(); | |
Book book = null; | |
for (Book bookFromShop : books) { | |
if (bookFromShop.getTitle().equals(bookTitle)) { | |
book = bookFromShop; | |
break; | |
} | |
} | |
if (book == null) { | |
System.out.println("There's no such book as \"" + bookTitle + "\" in the range available."); | |
} else { | |
System.out.println("There's a book entitled as \"" + bookTitle + "\". Selling it to the customer..."); | |
books.remove(book); | |
System.out.println("The book \"" + book.getTitle() + "\" has been sold."); | |
} | |
BookShop.SEMAPHORE.release(); | |
System.out.println("Customer left the book store.\n"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.mappedbus.sample.object; | |
import io.mappedbus.MappedBusMessage; | |
import io.mappedbus.MemoryMappedFile; | |
public class BookToSell implements MappedBusMessage { | |
public static final int TYPE = 2; | |
private Book book; | |
public BookToSell() { | |
this.book = new Book("Untitled Book"); | |
} | |
public BookToSell(Book book) { | |
this.book = book; | |
} | |
public int type() { | |
return TYPE; | |
} | |
public Book getBook() { | |
return book; | |
} | |
public void setBook(Book book) { | |
this.book = book; | |
} | |
@Override | |
public String toString() { | |
return book.getTitle(); | |
} | |
@Override | |
public void write(MemoryMappedFile mem, long pos) { | |
String title = book.getTitle(); | |
mem.putInt(pos, title.length()); | |
for (int i = 0; i < title.length(); i++) | |
mem.putByte(pos + 4 + i, (byte) title.charAt(i)); | |
} | |
@Override | |
public void read(MemoryMappedFile mem, long pos) { | |
int length = mem.getInt(pos); | |
byte[] bytes = new byte[length]; | |
for (int i = 0; i < length; i++) { | |
bytes[i] = mem.getByte(pos + 4 + i); | |
} | |
String title = new String(bytes); | |
book = new Book(title); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.mappedbus.sample.object; | |
import io.mappedbus.MappedBusWriter; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
public class Customer { | |
public static void main(String[] args) { | |
Customer customer = new Customer(); | |
customer.run(); | |
} | |
public void run() { | |
try { | |
MappedBusWriter writer = new MappedBusWriter("messages", 2000000L, 100); | |
writer.open(); | |
DemandedBook demandedBook = new DemandedBook(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
String title = reader.readLine(); | |
while (title.length() > 0) { | |
demandedBook.setTitle(title); | |
writer.write(demandedBook); | |
title = reader.readLine(); | |
} | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.mappedbus.sample.object; | |
import io.mappedbus.MappedBusMessage; | |
import io.mappedbus.MemoryMappedFile; | |
public class DemandedBook implements MappedBusMessage { | |
public static final int TYPE = 1; | |
private String title; | |
public DemandedBook() { | |
this.title = "Untitled Book"; | |
} | |
public DemandedBook(String title) { | |
this.title = title; | |
} | |
public int type() { | |
return TYPE; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
@Override | |
public String toString() { | |
return title; | |
} | |
@Override | |
public void write(MemoryMappedFile mem, long pos) { | |
mem.putInt(pos, title.length()); | |
for (int i = 0; i < title.length(); i++) | |
mem.putByte(pos + 4 + i, (byte) title.charAt(i)); | |
} | |
@Override | |
public void read(MemoryMappedFile mem, long pos) { | |
int length = mem.getInt(pos); | |
byte[] bytes = new byte[length]; | |
for (int i = 0; i < length; i++) { | |
bytes[i] = mem.getByte(pos + 4 + i); | |
} | |
title = new String(bytes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment